java - Eclipse PDE : StyleRange background colour disappears after a second

标签 java eclipse eclipse-plugin background-color pde

我目前正在开发一个 Eclipse 插件,它可以为代码行以及大括号等着色。

我让一切都动态工作,即,如果代码发生更改,线条颜色也会相应更新。

但是,一旦线条着色,大约一秒钟后它就会“重置”并删除所有颜色。

这是检查用户的源代码是否已被修改的事件监听器。

    private void textModifiedListener() {
    for (IEditorPart editorPart : getCurrentEditorParts()) {
        editorText = editorPart.getAdapter(Control.class);
        if (editorText instanceof StyledText) {
            styledText = (StyledText) editorText;
            styledText.addModifyListener(new ModifyListener() {
                @Override
                public void modifyText(ModifyEvent event) {
                    //Source Code Modified.
                    Colour();
                }
            });
        }
    }
}

然后我为单个字符着色,如下所示:

    public void ColourCharacter(ColourObject colourOject, int characterIndex) {
    /* Given the colour and the character index, colour the given brace. */
    if (isColouringEnabled) {
        System.out.println("[ColourCode]:\tColourCharacter.");
        style = new StyleRange();
        style.start = characterIndex;
        style.length = 1;
        style.background = new Color(Display.getCurrent(), colourOject.getRed(), colourOject.getGreen(), colourOject.getBlue());
        if (editorText instanceof StyledText) {
            styledText.setStyleRange(style);
        }
    }
}

这些方法用于获取 IEditorParts。

    /* Obtain the current workbench window. */
public synchronized static IWorkbenchWindow getActiveWorkbenchWindow() {
    return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}

/* Obtain the current editor reference. */
public synchronized static IEditorReference[] getCurrentEditorReferences() {
    return getActiveWorkbenchWindow().getActivePage().getEditorReferences();
}

/* Obtain the current editor parts. */
public synchronized List<IEditorPart> getCurrentEditorParts() {
    List<IEditorPart> editorParts = new ArrayList<IEditorPart>();
    for (IEditorReference editorReference : getCurrentEditorReferences()) {
        IEditorPart editor = editorReference.getEditor(true);
        if (editor != null) {
            editorParts.add(editor);
        }
    }
    return editorParts;
}

我已经被这个问题困扰了一整天了。任何帮助将不胜感激。

最佳答案

大多数现有编辑器都有自己的系统来设计他们管理的文本样式。这通常使用 JFace SourceViewerConfigurationReconcilerPresentationReconcilerIPresentationDamagerIPresentationRepairer 类除其他外。

你不能像这样尝试覆盖样式。编辑器完全控制文本的样式,并将忽略您所做的任何更改。例如,Reconciler 在后台运行,每半秒更新一次。

您必须查看特定的编辑器,看看它提供了哪些功能来添加到其样式系统中。

如果您只想标记错误和警告,则可以使用 IMarker 界面。

关于java - Eclipse PDE : StyleRange background colour disappears after a second,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323666/

相关文章:

eclipse - 调试eclipse插件项目——PermGen空间,内存不足错误

eclipse - 使用注释时获取 EL 表达式的 JSF Managed Bean 实例

java - Spark : Going reverse in dataframe until a condition met

java.lang.UnsatisfiedLinkError - 加载多个 lib 文件?

java - 如何在 Eclipse 的“打开资源”对话框中隐藏 .class 文件?

android - 在 Eclipse 中转换为 Maven 项目导致 ClassNotFoundException

java - 从自定义比较器获取 boolean 值答案

java - Amazon SQS 长轮询不返回所有消息

Eclipse Indigo - 无法升级 m2e

eclipse - 如何在文件保存时更新装饰器?