【问题标题】:setDefaultReturnToken messes up syntax highlightingsetDefaultReturnToken 搞乱了语法高亮
【发布时间】:2016-03-28 02:39:29
【问题描述】:

我正在开发一个带有自定义文本编辑器的 Eclipse 插件,我想为其提供语法高亮。因此,我实现了自己的 RuleBasedPartitionScanner 和各自的 SourceViewerConfiguration
当我不弄乱 PartitionScanner 的 defaultReturnToken 但是当我尝试设置默认的 defaultReturnToken 时,一切工作正常,语法突出显示消失了。

我的分区扫描仪:

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 类型

【问题讨论】:

    标签: java eclipse eclipse-plugin syntax-highlighting jface


    【解决方案1】:

    编辑

    我认为问题可能是您需要在新的 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 上。

    【讨论】:

    • 当我取消注释这些行时,仍然没有语法突出显示重新出现......即使我注释了将损坏器和修复器设置为 SQFPartitionScanner.SQF_CODE 类型的两行时@
    • 所以我的假设(再次)是错误的。你确定你用你添加的类型修改了SourceViewerConfiguration中的getConfiguredContentTypes()吗?
    • 试过这个,但getConfiguredContentTypes() 需要一个字符串数组而不是 IToken 数组,因此我不能在那里使用 IToken 常量,只使用 SQFPartitionScanner 中的常量没有帮助跨度>
    • 我在getConfiguredContentTypes() 中添加了我的新内容类型,我什至尝试删除DEFAULT_CONTENT_TYPE 或其他两个之一,但结果在所有情况下都是相同的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多