簡體   English   中英

如何在自定義Eclipse編輯器中為語法錯誤創建錯誤懸停

[英]How to create an Error Hover for Syntax Errors in a custom Eclipse Editor

我目前正在為自定義轉換語言編寫Eclipse編輯器插件。 我已經可以通過如下所示的錯誤下方的紅色粗線顯示語法錯誤: 1 ,它們也顯示在PRoblems視圖中。 現在,我正在嘗試為語法錯誤實現懸停,例如: 2 您有任何想法或教程,如何做到這一點? 將確切的行號添加到標記是否是強制性的? 我知道那里可能有幾十個教程,但是沒有一個教程詳細地解釋了它,所以請幫我!=)當前顯示語法錯誤的代碼是這個:

package de.se_rwth.langeditor.texteditor.errorhighlighting;

import java.io.IOException;
import java.util.Set;

import javax.annotation.Nullable;

import org.antlr.v4.runtime.misc.Interval;
import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.swt.widgets.Display;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

import de.se_rwth.langeditor.injection.TextEditorScoped;
import de.se_rwth.langeditor.modelstates.ModelState;
import de.se_rwth.langeditor.modelstates.ObservableModelStates;

@TextEditorScoped
public class ErrorHighlighter {

  private final IAnnotationModel annotationModel;

  private String content = "";


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

  private static final String MARKERID  = "org.eclipse.rwth.syntaxerror";

  @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;
    IWorkspace workspace = ResourcesPlugin.getWorkspace(); 
    try { //Remove all problem Markers when rebuilding the Model
       problems = workspace.getRoot().findMarkers(IMarker.PROBLEM, 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 =
        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);
        int lineNumber = 0;
        if(!content.isEmpty()){ //Convert StartIndex to Line Number
            String[] lines = content.substring(0, startIndex).split("\r\n|\r|\n");
            lineNumber = lines.length;
        }
        marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
    } catch (CoreException e) {
        e.printStackTrace();
    }

  }
}

要回答我自己的問題:我錯過了覆蓋

ITextHover getTextHover(ISourceViewer sourceViewer, String contentType)

方法。 所以在插入之后

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

進入SourceViewerConfiguration,將出現懸停。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM