如何在自定义Eclipse编辑器中为语法错误创建错误悬停

诺尔特

我目前正在为自定义转换语言编写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,将出现悬停。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Geany文本编辑器的“代码段”中创建自定义日期?

来自分类Dev

GAS脚本编辑器中的语法错误

来自分类Dev

Umbraco 7自定义属性编辑器错误

来自分类Dev

Umbraco 7自定义属性编辑器错误

来自分类Dev

使用 Javafx 中的 ui 组件为图形编辑器创建自定义形状

来自分类Dev

如何在Windows工作流中为自定义活动指定属性编辑器?

来自分类Dev

如何在Power BI查询编辑器中为最大日期添加自定义列?

来自分类Dev

如何在不使用HTML5,Silverlight或内置表单编辑器的CRM中创建自定义表单

来自分类Dev

生成的编辑器中的异常:Xtext错误或语法错误?

来自分类Dev

如何在设计时从自定义TComponentEditor中显示ActionList编辑器

来自分类Dev

如何在PyQt5中自定义Qtreewidget项目编辑器?

来自分类Dev

如何在magento的自定义类别属性中显示编辑器?

来自分类Dev

如何在 Python 中创建自定义错误消息

来自分类Dev

编辑器中的Eclipse JSP错误

来自分类Dev

在spring4中,当我设置自定义编辑器属性时,CustomEditorConfigurer引发错误

来自分类Dev

如何在代码编辑器中打印致命错误?

来自分类Dev

将onclick事件设置为自定义函数会导致语法错误

来自分类Dev

自定义数据注释语法错误

来自分类Dev

自定义委托项编辑器小部件出现在错误的位置

来自分类Dev

使用SharedPreferences自定义ArrayList-适配器或编辑器错误?

来自分类Dev

自定义Eclipse文本编辑器中的注释不显示

来自分类Dev

在自定义Eclipse文本编辑器插件中打开之前处理文件

来自分类Dev

Shopify:如何使自定义模板在主题编辑器中可编辑?

来自分类Dev

如何在PropertyGrid自定义集合编辑器中的“添加”按钮下拉菜单中自定义名称

来自分类Dev

如何为VS2015创建自定义编辑器?

来自分类Dev

在 PropertyGrid 中为我无法修改的类型使用自定义编辑器

来自分类Dev

如何在Eclipse编辑器中悬停时更改背景颜色

来自分类Dev

自定义指令中的ng-repeat:语法错误:令牌'$ index'是意外的

来自分类Dev

自定义指令中的ng-repeat:语法错误:令牌'$ index'是意外的

Related 相关文章

  1. 1

    如何在Geany文本编辑器的“代码段”中创建自定义日期?

  2. 2

    GAS脚本编辑器中的语法错误

  3. 3

    Umbraco 7自定义属性编辑器错误

  4. 4

    Umbraco 7自定义属性编辑器错误

  5. 5

    使用 Javafx 中的 ui 组件为图形编辑器创建自定义形状

  6. 6

    如何在Windows工作流中为自定义活动指定属性编辑器?

  7. 7

    如何在Power BI查询编辑器中为最大日期添加自定义列?

  8. 8

    如何在不使用HTML5,Silverlight或内置表单编辑器的CRM中创建自定义表单

  9. 9

    生成的编辑器中的异常:Xtext错误或语法错误?

  10. 10

    如何在设计时从自定义TComponentEditor中显示ActionList编辑器

  11. 11

    如何在PyQt5中自定义Qtreewidget项目编辑器?

  12. 12

    如何在magento的自定义类别属性中显示编辑器?

  13. 13

    如何在 Python 中创建自定义错误消息

  14. 14

    编辑器中的Eclipse JSP错误

  15. 15

    在spring4中,当我设置自定义编辑器属性时,CustomEditorConfigurer引发错误

  16. 16

    如何在代码编辑器中打印致命错误?

  17. 17

    将onclick事件设置为自定义函数会导致语法错误

  18. 18

    自定义数据注释语法错误

  19. 19

    自定义委托项编辑器小部件出现在错误的位置

  20. 20

    使用SharedPreferences自定义ArrayList-适配器或编辑器错误?

  21. 21

    自定义Eclipse文本编辑器中的注释不显示

  22. 22

    在自定义Eclipse文本编辑器插件中打开之前处理文件

  23. 23

    Shopify:如何使自定义模板在主题编辑器中可编辑?

  24. 24

    如何在PropertyGrid自定义集合编辑器中的“添加”按钮下拉菜单中自定义名称

  25. 25

    如何为VS2015创建自定义编辑器?

  26. 26

    在 PropertyGrid 中为我无法修改的类型使用自定义编辑器

  27. 27

    如何在Eclipse编辑器中悬停时更改背景颜色

  28. 28

    自定义指令中的ng-repeat:语法错误:令牌'$ index'是意外的

  29. 29

    自定义指令中的ng-repeat:语法错误:令牌'$ index'是意外的

热门标签

归档