Editor的語法著色原理:Eclipse使用damage ,repair和reconcile模型來實現語法著色功能,當用戶每次修改了document,Reconciler就會去計算文檔中哪些可視區域是無效的以及如何去repair之:這里的damage表示需要重新加以顯示的文本,repair則表示重新進行顯示的方式,而reconcile則是這個重新顯示的過程.為了定制damage和repair,需要給TextEditor配置自定義的SourceViewerConfigration,TextEditor中的主要組成部分叫SourceViewer,默認情況下SourceViewer不支持語法著色功能,而通過擴展SourceViewerConfiguration就可以對SourceViewer進行定制,通過復寫SourceViewerConfiguration的getPresentationReconciler()方法就可以定制我們自己的Reconciler,定制Reconciler主要是給其配置相關的Damager和Repairer,而這兩個東東又會跟RuleScanner關聯:
- PresentationReconciler reconciler = new PresentationReconciler();
- DefaultDamagerRepairer dr = new DefaultDamagerRepairer (getTagScanner());
- reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
- reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
RuleScanner定義了各種用于區分各種Token的規則以及相關的TextAttribute(顏色屬性就定義在里面)
內容輔助功能的實現:需要改寫SourceViewerConfiguration.getContentAssistance()方法,而自動完成行為的設置通過定義不同的CompletionProcessor來實現, CompletionProcessor需要復寫getCompletionProposalAutoActivationCharacters()來告訴編輯器在輸入何種內容時給出自動提示的內容,同時還需要實現computeCompletionProposals()方法來告訴要顯示的提示內容(completion proposal)
安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢】