我目前正在开发一个 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 SourceViewerConfiguration
、Reconciler
、PresentationReconciler
、IPresentationDamager
、IPresentationRepairer
类除其他外。
你不能像这样尝试覆盖样式。编辑器完全控制文本的样式,并将忽略您所做的任何更改。例如,Reconciler
在后台运行,每半秒更新一次。
您必须查看特定的编辑器,看看它提供了哪些功能来添加到其样式系统中。
如果您只想标记错误和警告,则可以使用 IMarker
界面。
关于java - Eclipse PDE : StyleRange background colour disappears after a second,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323666/