【问题标题】:Eclipse Plugin development: How to jump to Marker in the Source View from Problems View?Eclipse插件开发:如何从Problems View跳转到Source View中的Marker?
【发布时间】:2016-05-28 13:09:19
【问题描述】:

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

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

  }

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

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

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

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

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

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

    【讨论】:

    • 太棒了,谢谢!现在它在问题视图中显示每个错误两次。对此有什么想法吗?
    • 不确定,但请记住标记是永久性的,您可能需要使用IResource.deleteMarkers 删除旧标记。
    猜你喜欢
    • 1970-01-01
    • 2014-01-23
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多