2015-12-21 72 views
0

我正在開發一個帶有自定義文本編輯器的eclipse插件,爲此我想提供語法高亮顯示。因此我實施了我自己的RuleBasedPartitionScanner和各自的SourceViewerConfiguration
一切工作正常,當我不亂用PartitionScanner的defaultReturnToken,但是當我嘗試設置默認defaultReturnToken語法突出顯示消失。setDefaultReturnToken使語法突出顯示

我PartitionScanner:

public class SQFPartitionScanner extends RuleBasedPartitionScanner { 

    public static final String SQF_COMMENT = "__sqf_Comment"; 
    public static final String SQF_CODE = "__sqf_Code"; 

    public SQFPartitionScanner() { 
     IToken comment = new Token(SQF_COMMENT); 
     IToken code = new Token(SQF_CODE); 

     IPredicateRule[] rules = { 
       //rule for multiLine comments 
       new MultiLineRule("/*", "*/", comment), 
       //rule for singleLine comments 
       new EndOfLineRule("//", comment) 
     }; 

     this.setPredicateRules(rules); 

     this.setDefaultReturnToken(code); 
    } 

} 

本作的製作令牌不再類型IDocument.DEFAULT_CONTENT_TYPE但不是SQFPartitioScanner.CODE型我改變了我的SourceViewerConfiguration爲其次(我只是改變了getPresentationReconciler() - 方法):

public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { 
     PresentationReconciler reconciler = new PresentationReconciler(); 

     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(this.getKeywordScanner()); 
//  reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); 
//  reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); 
     reconciler.setDamager(dr, SQFPartitionScanner.SQF_CODE); 
     reconciler.setRepairer(dr, SQFPartitionScanner.SQF_CODE); 

     return reconciler; 
    } 

還有什麼我不得不改變語法突出顯示使用defaultReturnToken設置爲SQFPartitionScanner.SQF_CODE

編輯:
當我取消註釋行

//  reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); 
//  reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); 

和評論

reconciler.setDamager(dr, SQFPartitionScanner.SQF_CODE); 
reconciler.setRepairer(dr, SQFPartitionScanner.SQF_CODE); 

我仍然沒有得到語法高亮。
正因爲如此,我懷疑的是,默認的令牌不正確創建,因爲很明顯它是IDocument.DEFAULT_CONTENT_TYPE類型不再,但它不是SQFPartitionScanner.SQF_CODE類型既不

回答

0

編輯

我認爲這個問題可能是你需要將這兩種新的內容類型添加到新的SourceViewerConfiguration中的getConfiguredContentTypes方法中。我認爲你需要改變SQFPartitionScanner有頂部以下行:

public static final IToken SQF_Comment_Type = new Token(SQF_COMMENT); 
public static final IToken SQF_Code_Type = new Token(SQF_CODE); 

更改getConfigurationContentTypes到:

public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { 
    return new String[] { IDocument.DEFAULT_CONTENT_TYPE, 
     SQFPartitionScanner.SQF_Comment_Type, SQFPartitionScanner.SQF_Code_type }; 
} 

,也拆除commentcode變量並與最終的靜態替換它們在SQFPartitionScanner中,所以你總是引用相同的實例。

--------------------------------------------- -----------

似乎有一些事情默認爲IDocument.DEFAULT_CONTENT_TYPE和contentType的許多依賴關係。例如,在PresentationReconciler中,它使用分區的內容類型獲取損害者和調解者。破壞者根據您使用修理者設置的contentType進行存儲。他們根據類型的分區報告,並在某些情況下默認爲IDocument.DEFAULT_CONTENT_TYPE檢索:

if (document instanceof IDocumentExtension3) { 
    IDocumentExtension3 extension3= (IDocumentExtension3) document; 
    try { 
     return extension3.getPartition(partitioning, offset, preferOpenPartitions); 
    } catch (BadPartitioningException x) { 
    return new TypedRegion(0, document.getLength(), IDocument.DEFAULT_CONTENT_TYPE); 
    } 
} 

所以我懷疑,如果你將有你需要通過去解決它自己的內容類型整個系統正常。這是我認爲你可能有一些不一致的地方,其中一些內容認爲它是默認類型。

查看發生了什麼的一種方法是將相關eclipse插件的源jar添加到您的環境中,並逐步查看PresentationReconciler在運行時關於內容類型所發生的情況。

另一種方法是從上面暫時取消以下行:

//  reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); 
//  reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); 

所以,你必須爲兩種類型的註冊醫生,看看是否突出效益。如果確實如此,則確認問題出在文檔的contentType附近。

+0

當我取消註釋這些行時,仍然沒有語法突出顯示再次出現......即使當我評論兩條線時,我將損傷器和修理器設置爲'SQFPartitionScanner.SQF_CODE'類型 – Raven

+0

所以我的假設是錯誤的(再次) 。您是否確定使用已添加的類型修改了'SourceViewerConfiguration'中的'getConfiguredContentTypes()'? –

+0

@Raven,我用一個新建議編輯了我的答案。 –