查找与IMarker关联的注释

时间:2016-08-19 00:47:43

标签: java eclipse-plugin

我正在制作一个带有文本替换组件的插件,例如Eclipse的QuickFix。我所使用的偏移量(从marker.getAttribute中提取)在进行更改时是错误的,但未保存。我知道这只是因为标记的功能。

将标记添加到资源时,它们会将Annotation附加到文档的注释模型。此注释会跟踪未保存的更改,因此我更适合从其位置而不是标记中提取偏移量。

给定一个标记,有没有办法找到与之相关的注释?

1 个答案:

答案 0 :(得分:0)

注释不会直接跟踪其位置。该位置由注释模型类(IAnnotationModel)维护。

您可以从编辑器ISourceViewer

获取注释模型
IAnnotatorModel model = viewer.getAnnotationModel();

然后,您必须在模型注释中搜索所需的标记注释,并从模型中获取其位置:

Iterator<?> iter = model.getAnnotationIterator();
while (iter.hasNext()) {
  Object object = iter.next();
  if (object instanceof MarkerAnnotation) {
    MarkerAnnotation annotation = (MarkerAnnotation)object;

    IMarker marker = annotation.getMarker();

    // TODO check this is the marker you are interested in

    Position position = model.getPosition(annotation);
  }
}
相关问题