我目前正在为我自己的语言开发一个编辑器。我使用以下代码将错误标记添加到我的源 View :
private void displayError(Interval interval, String message) {
int startIndex = interval.a;
int stopIndex = interval.b + 1;
Annotation annotation =
new Annotation("org.eclipse.ui.workbench.texteditor.error", false, message);
annotations.add(annotation);
annotationModel.addAnnotation(annotation, new Position(startIndex, stopIndex - startIndex));
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IMarker marker;
try { //create Marker to display Syntax Errors in Problems View
marker = workspace.getRoot().createMarker(MARKERID);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.CHAR_START, startIndex);
marker.setAttribute(IMarker.CHAR_END, stopIndex);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
//marker.setAttribute(IMarker.LOCATION, workspace.getRoot().getLocationURI().toString());
MarkerUtilities.setCharStart(marker, startIndex);
MarkerUtilities.setCharEnd(marker, stopIndex);
int lineNumber = 0;
if(!content.isEmpty() && content.length()>=stopIndex){ //Convert StartIndex to Line Number
String[] lines = content.substring(0, stopIndex).split("\r\n|\r|\n");
lineNumber = lines.length;
}
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
marker.setAttribute(IMarker.TEXT, message);
marker.setAttribute(IDE.EDITOR_ID_ATTR, "de.se_rwth.langeditor");
MarkerAnnotation ma = new MarkerAnnotation("org.eclipse.ui.workbench.texteditor.error", marker);
annotationModel.addAnnotation(ma, new Position(startIndex, stopIndex - startIndex));
annotations.add(ma);
} catch (CoreException e) {
e.printStackTrace();
}
}
标记正确显示在问题 View 中。 Bt 如果我双击问题标记,它不会跳转到代码中的正确位置。此外,如果我右键单击问题标记,“转到”选项将被禁用。我的编辑器类扩展了 TextEditor,也实现了 IGotoMarker 接口(interface)。 gotoMarker方法我是这样实现的:
public void gotoMarker(IMarker marker) {
IDE.gotoMarker(this, marker);
}
getAdapter 方法如下所示:
public Object getAdapter(Class adapter) {
if (IContentOutlinePage.class.equals(adapter)) {
return contentOutlinePage;
}
return super.getAdapter(adapter);
}
如果有人能帮助我,那就太好了!
最佳答案
您正在工作区根资源上创建标记:
marker = workspace.getRoot().createMarker(MARKERID);
这是错误的,您必须在您正在编辑的实际 IFile
上创建标记。
关于java - Eclipse插件开发: How to jump to Marker in the Source View from Problems View?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499393/