java - Eclipse插件: Implement Quick Fix proposals on hovering over errors

标签 java eclipse eclipse-plugin eclipse-rcp

我所拥有的:我在编辑器中将错误显示为红色下划线,并在问题 View 中显示为其他错误。我为此使用标记,但也通过以下代码创建注释:

@TextEditorScoped
public class ErrorHighlighter {

  private final IAnnotationModel annotationModel;

  private String content = "";


  private final Set<Annotation> annotations = Sets.newConcurrentHashSet();

  private static final String ERRORMARKERID  = "org.eclipse.rwth.syntaxerror";
  private static final String WARNINGMARKERID = "org.eclipse.rwth.syntaxwarning"; 

  @Inject
  public ErrorHighlighter(@Nullable IAnnotationModel annotationModel, IStorage storage,
      ObservableModelStates observableModelStates) {
    this.annotationModel = annotationModel;
    if (annotationModel != null) {
      observableModelStates.getModelStates().stream()
          .filter(modelState -> modelState.getStorage().equals(storage))
          .forEach(this::acceptModelState);
      observableModelStates.addStorageObserver(storage, this::acceptModelState);
    }
  }

  public void acceptModelState(ModelState modelState) {
    for (Annotation annotation : annotations) {
      annotationModel.removeAnnotation(annotation);
      annotations.remove(annotation);
    }
    IMarker[] problems = null;
    int depth = IResource.DEPTH_INFINITE;
    IFile file = Misc.getEditorInput(modelState.getStorage()).getAdapter(IFile.class);
    try { //Remove all problem Markers when rebuilding the Model
       problems = file.findMarkers(ERRORMARKERID, true, depth);
       for(IMarker m: problems){
           m.delete();
       }
       problems = file.findMarkers(WARNINGMARKERID, true, depth);
       for(IMarker m: problems){
           m.delete();
       }
    } catch (CoreException e) {
       e.printStackTrace();
    }
    try {
        content = IOUtils.toString(modelState.getStorage().getContents(), "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    displaySyntaxErrors(modelState);
    displayAdditionalErrors(modelState);
  }

  private void displaySyntaxErrors(ModelState modelState) {
    ImmutableMultimap<Interval, String> syntaxErrors = modelState.getSyntaxErrors();
    for (Interval interval: syntaxErrors.keys()) {
      for (String message : syntaxErrors.get(interval)) {
        Display.getDefault().asyncExec(() -> displayError(interval, message));
      }
    }
  }

  private void displayAdditionalErrors(ModelState modelState) {
    Multimap<Interval, String> additionalErrors = modelState.getAdditionalErrors();
    for (Interval interval: additionalErrors.keys()) {
      for (String message : additionalErrors.get(interval)) {
        Display.getDefault().asyncExec(() -> displayError(interval, message));
      }
    }
  }

  private void displayError(Interval interval, String message) {
    int startIndex = interval.a;
    int stopIndex = interval.b + 1;
    Annotation annotation = null;
//    Annotation annotation =
//        new Annotation("org.eclipse.ui.workbench.texteditor.error", false, message);
//    annotations.add(annotation);
//    annotationModel.addAnnotation(annotation, new Position(startIndex, stopIndex - startIndex));
    IMarker marker = null;
    try { //create Marker to display Syntax Errors in Problems View
        IFile file = (IFile) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
        if (file != null) {
            if(message.charAt(message.length()-1) == 'W'){
                marker = file.createMarker(WARNINGMARKERID);
                marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
            } else {
                marker = file.createMarker(ERRORMARKERID);
                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);
            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);
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
    IMarker[] problems = null;
    int depth = IResource.DEPTH_INFINITE;
    IFile file = (IFile) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    try { //Remove all problem Markers when rebuilding the Model
       problems = file.findMarkers(ERRORMARKERID, true, depth);
       for(IMarker m: problems){
           for(IMarker n: problems){
               if(MarkerUtilities.getCharStart(m) == MarkerUtilities.getCharStart(n) && m != n && MarkerUtilities.getMessage(m).equals(MarkerUtilities.getMessage(n))){
                   m.delete();
               }
           }
       }
    } catch (CoreException e) {
       e.printStackTrace();
    }
    if(marker != null){
        Annotation a = new MarkerAnnotation(marker);
        annotations.add(a);
        annotationModel.addAnnotation(a, new Position(startIndex, stopIndex - startIndex));
    }

  }
}

在我的 SourceViewerConfiguration 中,我使用以下代码覆盖 getTextHover 和 getAnnotationHover:

@Override
  public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
      return new DefaultAnnotationHover(true);
  }

  public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
      return new DefaultTextHover(sourceViewer);
  }

我还用以下代码覆盖 getQuickAssistAssistant:

public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
      IQuickAssistAssistant quickAssist = new QuickAssistAssistant();
      quickAssist.setQuickAssistProcessor(new TFQuickAssistProcessor());
      quickAssist.setInformationControlCreator(getInformationControlCreator(sourceViewer));
      return quickAssist;
}

有了这个,我可以右键单击代码中的错误并选择“快速修复”,这将导致出现一个框,其中显示我的快速修复建议。

我想要什么:当我将鼠标悬停在错误上方时,如何使此框出现? 提前致谢

最佳答案

回答我自己的问题: 我用以下方法替换了 getTextHover 方法:

public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {      
      return new AbstractAnnotationHover(true) {
          public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
              IAnnotationModel model = ((SourceViewer) textViewer).getAnnotationModel();
              @SuppressWarnings("unchecked")
              Iterator<Annotation> parent = 
                      ((IAnnotationModelExtension2)model).getAnnotationIterator(hoverRegion.getOffset(), 
                              hoverRegion.getLength(), true, true);
              Iterator<?> iter = new JavaAnnotationIterator(parent, false); 
              Annotation annotation = null;
              Position position = null;
              while (iter.hasNext()) {
                  Annotation a = (Annotation) iter.next();
                  Position p = model.getPosition(a);
                  annotation = a;
                  position = p;
              }
              return new AnnotationInfo(annotation, position, textViewer) {
                  public ICompletionProposal[] getCompletionProposals() {
                      ICompletionProposal proposal1 = null;
                      IMarkerResolution [] resolutions = null;
                      ICompletionProposal [] com = null;
                      if (annotation instanceof MarkerAnnotation) {
                          resolutions = new ErrorResolution().getResolutions(((MarkerAnnotation) annotation).getMarker());
                          if(resolutions.length != 0){
                              proposal1 = new MarkerResolutionProposal(resolutions[0], 
                                      ((MarkerAnnotation) annotation).getMarker());
                              return new ICompletionProposal[] { proposal1 };
                          }
                      }
                      return com ;
                  }
              };
          } 
    };
}

这将导致错误上方出现一个悬停框,提供快速修复。希望这有帮助!

关于java - Eclipse插件: Implement Quick Fix proposals on hovering over errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587545/

相关文章:

java - Eclipse插件: Show window with scrollable text

java - PhantomJS:page.onError - 消息:TypeError:未定义不是函数

java - Eclipse 中的 Maven 原生依赖

java - 为父类的可见属性生成委托(delegate)方法

java - 将项目 checkin 到不同的 SVN 存储库位置

java - Eclipse 集成开发环境设置

java - 如何获取用户输入(来自扫描仪)并将整个输入转换为 ascii

Java:PrintStream 和 PrintWriter 之间的区别

java - 使用 JSOUP Java 更改 CSS

java - 用于插件项目的 SWT 或 Swing