简体   繁体   English

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

[英]Eclipse Plugin development: How to jump to Marker in the Source View from Problems View?

I'm currently developing an editor for my own language. 我目前正在为自己的语言开发编辑器。 I add error Marker to my Source View with this code: 我使用以下代码将错误标记添加到我的源代码视图:

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

  }

The Marker are correctly displayed in the Problems View. 标记已正确显示在“问题”视图中。 Bt if I double click on the Problem Marker it won't jump to the correct position in the code. 但是,如果我双击问题标记,它将不会跳到代码中的正确位置。 Also if I right click on the Problem Marker, the Option "Go to" is disabled. 另外,如果我右键单击问题标记,则“转到”选项将被禁用。 My Editor class, which extends the TextEditor, also implements the IGotoMarker Interface. 我的Editor类扩展了TextEditor,还实现了IGotoMarker接口。 The gotoMarker method is implemented by me like this: gotoMarker方法由我这样实现:

public void gotoMarker(IMarker marker) {
        IDE.gotoMarker(this, marker);
    }

The getAdapter Method looks like this: getAdapter方法如下所示:

public Object getAdapter(Class adapter) {
      if (IContentOutlinePage.class.equals(adapter)) {
      return contentOutlinePage;
    }
    return super.getAdapter(adapter);
  }

It would be great, if someone can help me! 如果有人可以帮助我,那就太好了!

You are creating the marker on the workspace root resource with: 您正在使用以下命令在工作区根资源上创建标记:

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

This is wrong, you must create the marker on the actual IFile that you are editing. 这是错误的,您必须在要编辑的实际IFile上创建标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM