You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse自定义编辑器语法错误下划线缺失问题:已实现语法检查与问题视图标记,如何在编辑器内显示错误下划线

Eclipse自定义编辑器错误下划线不显示问题排查

我刚实现了一款带有语法检查功能的自定义Eclipse编辑器,目前保存编辑器时标记(Marker)的创建功能正常,且标记能在问题视图(Problem View)中显示。但遇到一个问题:无法在编辑器内为错误添加下划线标记,请问我遗漏了哪些配置或代码逻辑?

我的配置与代码片段

配置片段(plugin.xml)

<extension point="org.eclipse.ui.editors.markerAnnotationSpecification">
 <specification 
    icon = "icons/fail.gif" 
    annotationType="com.customeditor" 
    verticalRulerPreferenceKey="highlight.rulers.vertical" 
    textPreferenceKey="highlight.text" 
    colorPreferenceKey="highlight.color" 
    highlightPreferenceKey="highlight.background" 
    textPreferenceValue="true" 
    textStylePreferenceValue="PROBLEM_UNDERLINE" 
    overviewRulerPreferenceKey="highlight.rulers.overview" 
    presentationLayer="4" 
    highlightPreferenceValue="true" 
    label="DiffSiteB" 
    symbolicIcon="error" 
    colorPreferenceValue="255,0,0" 
    verticalRulerPreferenceValue="true" 
    overviewRulerPreferenceValue="true" 
    textStylePreferenceKey="highlight.text.style" >
 </specification >
</extension>

代码片段

public IMarker createMarker(IResource res, String location, String message, int start, int end) throws CoreException {
 IMarker marker = null;
 if (res != null) {
 marker = res.createMarker("com.customedtior");
 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
 marker.setAttribute(IMarker.CHAR_START, start);
 marker.setAttribute(IMarker.CHAR_END, end);
 marker.setAttribute(IMarker.LOCATION, location);
 marker.setAttribute(IMarker.MESSAGE, message);
 SimpleMarkerAnnotation ann = new SimpleMarkerAnnotation(marker);
 ann.setType(JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE);
 getVerticalRuler().getModel().addAnnotation(ann, new Position(start, end));
 getVerticalRuler().update();
 }
 return marker;
}

问题排查与修复方案

从你的配置和代码来看,主要有几个关键遗漏点:

1. 标记类型与注解规范不匹配

你创建Marker时用的类型是com.customedtior,但markerAnnotationSpecification里的annotationType是com.customeditor,而且代码里硬编码了JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE——这会导致Eclipse无法将你的自定义Marker关联到正确的注解样式,自然不会显示下划线。

2. 缺失注解类型的注册

你只配置了注解的显示规范,但没有通过org.eclipse.ui.editors.annotationTypes扩展点注册自定义注解类型。Eclipse需要这个注册来识别注解和Marker的关联关系。

3. 手动操作注解的方式错误

你直接往垂直标尺模型里添加注解,但编辑器内的文本下划线是由AnnotationPainter处理的,应该通过编辑器的AnnotationModel来管理,而不是手动操作标尺。而且手动创建SimpleMarkerAnnotation并指定Java的错误注解类型,会覆盖你的自定义配置。


具体修复步骤

第一步:补充注解类型注册

在plugin.xml里添加annotationTypes扩展,确保Marker类型和注解类型关联:

<extension point="org.eclipse.ui.editors.annotationTypes">
  <type
      markerType="com.customeditor.marker" <!-- 和你创建Marker时用的类型保持一致 -->
      name="com.customeditor.errorAnnotation" <!-- 这个要和markerAnnotationSpecification里的annotationType对应 -->
      super="org.eclipse.ui.workbench.texteditor.error"/>
</extension>

第二步:修正markerAnnotationSpecification配置

把annotationType改成上面注册的注解类型,同时确保逻辑一致:

<extension point="org.eclipse.ui.editors.markerAnnotationSpecification">
 <specification 
    icon = "icons/fail.gif" 
    annotationType="com.customeditor.errorAnnotation" <!-- 修改为注册的注解类型 -->
    verticalRulerPreferenceKey="highlight.rulers.vertical" 
    textPreferenceKey="highlight.text" 
    colorPreferenceKey="highlight.color" 
    highlightPreferenceKey="highlight.background" 
    textPreferenceValue="true" 
    textStylePreferenceValue="PROBLEM_UNDERLINE" 
    overviewRulerPreferenceKey="highlight.rulers.overview" 
    presentationLayer="4" 
    highlightPreferenceValue="true" 
    label="DiffSiteB" 
    symbolicIcon="error" 
    colorPreferenceValue="255,0,0" 
    verticalRulerPreferenceValue="true" 
    overviewRulerPreferenceValue="true" 
    textStylePreferenceKey="highlight.text.style" >
 </specification >
</extension>

第三步:修改createMarker方法,移除手动注解操作

Eclipse的MarkerAnnotationManager会自动根据Marker类型创建对应的注解并关联到编辑器,不需要手动处理:

public IMarker createMarker(IResource res, String location, String message, int start, int end) throws CoreException {
    IMarker marker = null;
    if (res != null) {
        // 使用和annotationType配置中markerType一致的类型
        marker = res.createMarker("com.customeditor.marker");
        marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
        marker.setAttribute(IMarker.CHAR_START, start);
        marker.setAttribute(IMarker.CHAR_END, end);
        marker.setAttribute(IMarker.LOCATION, location);
        marker.setAttribute(IMarker.MESSAGE, message);
        // 移除所有手动创建和添加Annotation的代码
    }
    return marker;
}

第四步:确保编辑器继承正确的基类

如果你的编辑器继承自AbstractTextEditor,它会自动处理注解的绘制和管理。如果是自定义的编辑器,需要确保SourceViewer正确配置了AnnotationModel和AnnotationPainter。

完成这些修改后,重新启动Eclipse,保存编辑器时应该就能看到错误文本的下划线标记了。


内容的提问来源于stack exchange,提问作者jeppa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:27:26