Eclipse插件开发:如何从问题视图中跳转到源视图中的标记?

时间:2016-05-28 13:09:20

标签: java eclipse eclipse-plugin

我目前正在为自己的语言开发编辑器。我使用以下代码将错误标记添加到源视图中:

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();
    }

  }

标记在问题视图中正确显示。但是,如果我双击问题标记,它就不会跳到代码中的正确位置。此外,如果我右键单击问题标记,选项"转到"被禁用。扩展TextEditor的My Editor类也实现了IGotoMarker接口。 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);
  }

如果有人可以帮助我,那就太好了!

1 个答案:

答案 0 :(得分:1)

您正在工作区根资源上创建标记:

marker = workspace.getRoot().createMarker(MARKERID);

这是错误的,您必须在正在编辑的实际IFile上创建标记。