如何为自定义 eclipse 编辑器实现 Quick Fix / Quick Assist?
我扩展了 org.eclipse.ui.editors.text.TextEditor 来实现自定义编辑器。 对于此编辑器,我定义了一个标记类型(org.eclipse.core.resources.markers
扩展点)和一个注释类型(org.eclipse.ui.editors.annotationTypes
) > 扩展点)来标记编辑器中代码的特定部分。我使用协调器来更新我的注释模型。
现在我想添加一个快速修复/快速协助功能。我只是想让 Eclipse 显示一个包含建议的框,当我将鼠标悬停在代码的带注释部分上并用给定的字符串替换该部分时,当我单击建议时。就像java编辑器的快速修复功能一样。
那么,实现这种行为的最佳方法是什么?
我读到了有关标记分辨率生成器和快速辅助处理器的信息,但我仍然很困惑它们是如何协同工作的......
如果有人能指出我正确的方向,我会很高兴。
编辑:据我目前所知,MarkerResolutionGenerator
负责在问题视图中显示快速修复。要在源代码查看器中快速修复,我必须为我的 SourceViewer
设置一个 QuickAssistAssistant
并实现一个返回 CompletionProposals 的
。 这是正确的做法吗?QuickAssistProcessor
EDIT2:我想知道我是否需要标记
,或者只需要注释
,我很困惑......
I have extended org.eclipse.ui.editors.text.TextEditor
to implement a custom editor.
For this editor, I have defined a marker type (org.eclipse.core.resources.markers
extension point) and an annotation type (org.eclipse.ui.editors.annotationTypes
extension point) to mark specific parts of code in my editor. I use a reconciler to update my annotation model.
Now I want to add a quick fix / quick assist feature. I simply want eclipse, to show a box with proposals, when I hover over an annotated part of the code and replace that part with a given string, when I click on a proposal. Just like the quick fix feature for the java editor.
So, what is the best way to implement this behavior?
I read about marker resolution generators and quick assist processors, but I'm still confused how it all works together...
I would be glad, if someone could point me to the right direction.
EDIT: From what I've understood so far, a MarkerResolutionGenerator
is responsible for showing quick fixes in the problems view. To get quick fixes in the source viewer, I would have to set a QuickAssistAssistant
for my SourceViewer
and implement a QuickAssistProcessor
which returns CompletionProposals
.
Is this the right way to do it?
EDIT2: I'm wondering if I need Markers
at all, or only Annotations
, I'm confused...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于找到了如何让 Quick Fix 为我的编辑器工作。
我使用
annotationTypes
扩展点来注册我自己的注释类型,并使用markerAnnotationSpecification
扩展点来指定外观。在我的自定义SourceViewerConfiguration
类中,我重写getAnnotationHover(...)
以返回DefaultAnnotationHover
对象和getTextHover(...)< /code> 返回一个
DefaultTextHover
对象,因此注释显示在我的源查看器中。然后,我重写
getReconciler(...)
以返回一个MonoReconciler
,并使用我自己的IReconcilingStrategy
实现来在其reconcile 中创建注释(...)
方法。最后,我重写了getQuickAssistAssistant(...)
以使用我自己的IQuickAssistProcessor
实现返回一个QuickAssistAssistant
。当我按 CTRL+1 时,处理器类中的computeQuickAssistProposals(...)
方法会计算显示的快速修复建议。我不创建任何
Marker
对象,也不使用MarkerResolutionGenerator
,因为标记概念比仅使用注释要重要得多,而且注释提供的功能适合我的需求。需要。I finally found out how to get Quick Fix to work for my editor.
I use the
annotationTypes
extension point to register my own annotation type and themarkerAnnotationSpecification
extension point to specify the look and feel. In my customSourceViewerConfiguration
class I overridegetAnnotationHover(...)
to return aDefaultAnnotationHover
object andgetTextHover(...)
to return aDefaultTextHover
object, so the annotations are shown in my source viewer.Then I override
getReconciler(...)
to return aMonoReconciler
with my own implementation ofIReconcilingStrategy
to create the annotations in itsreconcile(...)
method. And finally I overridegetQuickAssistAssistant(...)
to return aQuickAssistAssistant
with my own implementation ofIQuickAssistProcessor
. ThecomputeQuickAssistProposals(...)
method in the processor class computes the quick fix proposals which show up, when I press CTRL+1.I don't create any
Marker
objects and don't use aMarkerResolutionGenerator
, since the marker concept is much more heavyweight than using only annotations and the functionality which annotations provide fits my needs.您必须将扩展注册到扩展点 org.eclipse.ui.ide.markerResolution。该扩展引用了一个markerType(使用markerId),同时也是一个解析生成器。
后一个组件负责计算可能的修复:它读取标记,它可以检查相关文件等,并创建标记解析实例。这些解决实例基本上处理错误的文件,并有望解决原始问题。
在标记解析期间,您不必担心删除标记,因为在执行修复后,有时验证会再次运行(例如在构建期间,或者如果没有可用的自动验证,则手动 - 但这不是用于更新标记列表的标记分辨率)。
You have to register an extension to the extension point org.eclipse.ui.ide.markerResolution. This extension refers to a markerType (using the markerId), and also a resolution generator.
The latter component is responsible for calculating the possible fixes: it reads the marker, it can check the related files, etc., and creates marker resolution instances. These resolution instances basically process the erroneous files, and hopefully fix the original problem.
During marker resolution, you should not worry about removing the markers, as after the fix is executed, sometimes the validation would run again (e.g. during the build, or if no automatic validation is available, then manually - but it is not the task of the marker resolution to update the list of markers).