我正在 Eclipse 中编写自定义编辑器,并且刚刚集成了自定义错误识别功能。现在我面临一个奇怪的问题:我可以将标记添加到我的编辑器中,这些标记可以正常显示,我也可以在编辑器运行时删除它们。
什么不起作用:当我关闭我的编辑器时,我希望标记消失/被删除。
我现在正在做的是
创建具有 transient 属性集的标记,如下所示:
marker.setAttribute(IMarker.TRANSIENT, true);
这似乎并没有改变任何东西。尝试通过源查看器annotation-model 删除所有注释。这不起作用,因为当我尝试连接到我的编辑器
dispose()
方法或将DisposeListener
添加到我的 sourceviewers textwidget 时, sourceviewer 已被释放,getSourceViewer().getAnnotationModel();
返回null
。
我的deleteMarkers
方法:
private void deleteMarkers() {
IAnnotationModel anmod = getSourceViewer().getAnnotationModel();
Iterator<Annotation> it = anmod.getAnnotationIterator();
while (it.hasNext()) {
SimpleMarkerAnnotation a = (SimpleMarkerAnnotation) it.next();
anmod.removeAnnotation(a);
try {
a.getMarker().delete();
} catch (CoreException e) {
e.printStackTrace();
}
}
}
感谢任何帮助^^
最佳答案
连接到您的编辑器关闭事件,为编辑器获取对 IResource 的引用(我相信您可以在 IEditorInput 上获得)并在相关资源上调用 IResource#deleteMarkers(),这将在您关闭编辑器时删除它们。按照设计,当编辑器关闭时,eclipse 不会删除标记。
关于java - 如何在编辑器关闭后删除 IMarkers(或者为什么 IMarker.TRANSIENT 属性不起作用)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918717/