【发布时间】: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