Eclipse插件:实现快速修复提议,将鼠标悬停在错误上

时间:2016-06-02 09:16:11

标签: java eclipse eclipse-plugin eclipse-rcp

我有什么:我在编辑器中将错误显示为红色下划线并在问题视图中显示错误。我使用标记,但也通过此代码创建注释:

@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;
}

有了这个,我可以直接点击代码中的错误并选择QuickFix,这将导致出现一个Box,显示我的快速修复提案。

我想要什么:每当我将鼠标悬停在错误之上时,如何让这个Box出现? 提前致谢

2 个答案:

答案 0 :(得分:0)

回答我自己的问题: 我用这个替换了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 ;
                  }
              };
          } 
    };
}

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

答案 1 :(得分:0)

选择的答案解决了我的问题,但除了问题解决方案之外,我还需要提供文本悬停帮助。文本帮助和问题解决方案提案需要不同的悬停实现。

这是我的委托悬停控件的实现,如果用户将鼠标悬停在问题标记上并且解决方案提议可用,则会调用JDT Annotation悬停控件。否则,它会调用ITextHover实现,如果可用,它将提供文本帮助。

这松散地基于BestMatchHover的JDT实现,这是JDT通过依次检查已配置的悬停并使用返回信息的第一个来委托不同的悬停的方式。从选定的悬停中获取正确的演示文稿时遇到了一些麻烦,JDT实现有助于展示如何提供IInformationControlCreator的适当实现。

/**
 * This class provides a TextHover implementation which delegates to one of two different kinds of
 * TextHovers, depending on the current hover contents.
 * 
 * If the mouse is hovering over text for which
 * a problem was detected by the parser and a resolution proposal is available, presentation of the
 * hover info will be delegated to the JDT Hover control which presents the problem description and
 * Hyperlinks for the available proposals. Otherwise an ITextHover implementation will provide textual
 * hover help..
 * 
 * The JDT Hover implementation is non-API, and could therefore break in future RCP versions.
 */

public class DelegatingTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {

    private ICompletionProposal[] proposals = new ICompletionProposal[0];

    protected IRegion currentHoverRegion;

    //Provides help info as text
    private ITextHover helpHover = new ITextHover() {

        @Override
        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
            return DelegatingTextHover.this.getHoverRegion(textViewer, offset);
        }


        @Override
        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
            String hoverHelp = ...
            return hoverHelp;

    };


    @SuppressWarnings("restriction")
    private AbstractAnnotationHover quickFixHover = new AbstractAnnotationHover(true) {

        @Override
        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
            return null;
        }

        @Override
        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
            return DelegatingTextHover.this.getHoverRegion(textViewer, offset);
        }

        @Override
        public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
            currentHoverRegion = hoverRegion;
            IMarker marker = ((MarkerResolutionProposal) proposals[0]).getMarker();
            String msg;
            try {
                msg = (String) marker.getAttribute(IMarker.MESSAGE);
            } catch (CoreException e) {
                msg = "Parsing Error";
            }
            return new AnnotationInfo(new Annotation(IMarker.PROBLEM, true, msg),
                    new Position(hoverRegion.getOffset(), msg.length()),
                    textViewer) {
                @Override
                public ICompletionProposal[] getCompletionProposals() {
                    return proposals;
                }
            };
        }

        @Override
        public IInformationControlCreator getHoverControlCreator() {
            // if we're using the annotation hover, delegate to superclass. Otherwise, use the default
            if (hoveringOverProblemAnnotation(currentHoverRegion)) {
                return super.getHoverControlCreator();
            }
            return new IInformationControlCreator() {
                @Override
                public IInformationControl createInformationControl(Shell shell) {
                    return new DefaultInformationControl(shell, true);
                }
            };
        }

    };

    @SuppressWarnings({ "restriction", "deprecation" })
    @Override
    public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
        proposals = ...
        if (hoveringOverProblemAnnotation(hoverRegion)) {
            return quickFixHover.getHoverInfo2(textViewer, hoverRegion);
        }
        currentHoverRegion = hoverRegion;
        return helpHover.getHoverInfo(textViewer, hoverRegion);
    }

    @SuppressWarnings("restriction")
    @Override
    public IInformationControlCreator getHoverControlCreator() {
        if (hoveringOverProblemAnnotation(currentHoverRegion)) {
            return quickFixHover.getHoverControlCreator();
        }
        return null;
    }

    public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
        Point selection= textViewer.getSelectedRange();
        if (selection.x <= offset && offset < selection.x + selection.y)
            return new Region(selection.x, selection.y);
        return new Region(offset, 0);
    }

    @Override
    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
        return null;
    }

    public boolean hoveringOverProblemAnnotation(IRegion hoverRegion) {
        List<Point> ranges = new ArrayList<>();
        for (ICompletionProposal proposal : proposals) {
            //MarkerResolutionProposal is an implementation of ICompletionProposal which wraps a Marker
            if (proposal instanceof MarkerResolutionProposal) {
                MarkerResolutionProposal markerProposal = (MarkerResolutionProposal) proposal;
                IMarker marker = markerProposal.getMarker();
                try {
                    Integer begin =  (Integer) marker.getAttribute(IMarker.CHAR_START);
                    Integer end = (Integer) marker.getAttribute(IMarker.CHAR_END);
                    ranges.add(new Point(begin, end));
                } catch (CoreException e) {
                    //PASS
                }
            }
        }
        boolean found = false;
        for (Point p : ranges) {
            if (hoverRegion.getOffset() >= p.x && hoverRegion.getOffset() <= p.y) {
                found = true;
            }
        }
        return proposals.length > 0 && found;
    }

}