【问题标题】:Eclipse PDE: StyleRange background colour disappears after a secondEclipse PDE:StyleRange 背景颜色在一秒钟后消失
【发布时间】:2015-10-24 21:39:57
【问题描述】:

我目前正在开发一个 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;
}

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

【问题讨论】:

  • 您在此代码中泄漏了Color 对象。如果您创建一个Color,您必须在完成后安排到dispose。您还应该为特定颜色重用 Color 对象,因为操作系统可能会限制 Color 对象的数量。
  • 您看到的 ColourObjects 是我创建的对象。还是您指的是其他Color 对象?
  • style.background = new Color(....
  • 谢谢。但是,大约一秒钟后,我仍然遇到同样的颜色消失问题:/
  • 这并不能解决我只是指出代码中的一个缺陷(这就是为什么它是一个评论)的问题。

标签: java eclipse eclipse-plugin background-color pde


【解决方案1】:

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

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

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

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

【讨论】:

  • 非常感谢您的回复。我认为问题出在这些方面...您是否有一个示例(或指向示例的链接)来说明我如何动态突出显示代码?
  • 正如我所说,这取决于编辑器。对于 Java 编辑器this question 有一些答案。
  • eclipse.org/articles/Article-Mark%20My%20Words/… 。请记住,标记用于磁盘上的文件和内容,注释仅用于编辑器中就可以了。
【解决方案2】:

您可能想改用Annotations extension point 中的Annotations。至少those are used by the Eclemma plugin 可以在Java 编辑器中显示不同颜色的背景。

【讨论】:

  • 感谢您的回复!注释是否允许对单个语法字符进行着色?我一直在尝试使用 IMarkers,但由于缺乏可用的在线信息,我无法让它们做我想做的事情。
  • 1 个字符的范围肯定是可能的。关于可用的自定义我不确定。但是你可以自己试试。打开“注释”首选项页面(在“文本编辑器”下方)。选择您知道并且可以在 IDE 中轻松重现的注释之一,然后使用首选项页面右侧的“text as”下拉框来查看可用的样式。
猜你喜欢
  • 2014-06-23
  • 2018-06-06
  • 2021-06-10
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多