Eclipse SourceVIew没有使用JavaSourceViewerConfiguration高亮显示注释

时间:2014-08-21 15:38:58

标签: eclipse

我开发Eclipse插件。 我想在我的ViewPart中显示突出显示的java代码。

我尝试使用集成的Eclipse类(SourceViewerJavaSourceViewerConfiguration)。这是我的代码:

@Override
public void createPartControl(Composite parent) {
    String code = "int a = 5;\n" + 
            "//not-working comment\n" +
            "/* not working single line comment */ \n" +
            "not-working multi-line comment\n";

    JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
    SourceViewer sv = new SourceViewer(parent, null, SWT.NONE);

    JavaSourceViewerConfiguration config = 
            new JavaSourceViewerConfiguration(
            tools.getColorManager(),
            JavaPlugin.getDefault().getCombinedPreferenceStore(),
            null,
            null
            ); 
    sv.configure(config);
    Document d = new Document();
    d.set(code);
    sv.setDocument(d);
}

它成功突出显示了所有代码,但并未突出显示任何注释。我做错了什么?

UPD: 我不确定它是否重要,但是根据需要在清单文件中标记了插件/包的列表:

Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.jdt.ui,
 org.eclipse.ui.workbench.texteditor,
 org.eclipse.jface.text
Import-Package: org.eclipse.jface.text.source

2 个答案:

答案 0 :(得分:0)

看起来您需要将分区参数(JavaSourceViewerConfiguration构造函数的最后一个参数)指定为IJavaPartitions.JAVA_PARTITIONING

答案 1 :(得分:0)

您必须为文档定义分区程序:

IDocumentPartitioner partitioner = new FastPartitioner(new FastJavaPartitionScanner(), new String[] { 
        IJavaPartitions.JAVA_DOC,
        IJavaPartitions.JAVA_MULTI_LINE_COMMENT, 
        IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
        IJavaPartitions.JAVA_STRING, IJavaPartitions.JAVA_CHARACTER });

然后连接它们:

d.setDocumentPartitioner(partitioner);
partitioner.connect(d);