爱看视频的网站,方城网站建设,千度搜索引擎,软件公司简介简介
Code Vision Hints是idea Inlay提示中的一种类型#xff0c;它只能提供block类型的inlay#xff0c;可以把它添加到字段、方法、类等上面#xff0c;一个元素如果包含多个提示的话#xff0c;这些inlay会被展示在同一行上。
Code vision hints可以展示在元素的上面、…简介
Code Vision Hints是idea Inlay提示中的一种类型它只能提供block类型的inlay可以把它添加到字段、方法、类等上面一个元素如果包含多个提示的话这些inlay会被展示在同一行上。
Code vision hints可以展示在元素的上面、右边、或者行末尾具体展示的位置可以在IDE中修改Preferences | Editor | Inlay Hints | Code vision。
目前已经有许多的插件都使用了Inlay例如
Java代码中会在链式调用的每行展示返回类型信息版本控制的项目中会展示提交者信息
有两个扩展点可以用于实现code vision:
DaemonBoundCodeVisionProvider : PSI改变后会得到通知例如usages其他文件变动后会继续计算被使用信息CodeVisionProvider : 不依赖PSI改变通知例如git的提交信息。
目前在2022.2这个版本测试中发现CodeVisionProvider有很多bug很多废弃的方法也需要实现也许在新版本已经解决了这个问题如果你依赖IDE版本较老建议还是直接实现DaemonBoundCodeVisionProvider。
实现实例
新建VisionProvider的实现类
public class MyVisionProvider implements DaemonBoundCodeVisionProvider {public static final String GROUP_ID com.demo;public static final String ID myPlugin;public static final String NAME my plugin;NotNullOverridepublic CodeVisionAnchorKind getDefaultAnchor() {// 默认展示在元素的顶部return CodeVisionAnchorKind.Top;}NotNullOverridepublic String getId() {return ID;}NotNullOverridepublic String getGroupId() {return GROUP_ID;}NlsNotNullOverridepublic String getName() {return NAME;}NotNullOverridepublic ListCodeVisionRelativeOrdering getRelativeOrderings() {// 设置展示顺序为第一个return List.of(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingFirst.INSTANCE);}// 设置展示场景java文件的方法上展示NotNullOverridepublic ListPairTextRange, CodeVisionEntry computeForEditor(NotNull Editor editor, NotNull PsiFile file) {ListPairTextRange, CodeVisionEntry lenses new ArrayList();String languageId file.getLanguage().getID();if (!JAVA.equalsIgnoreCase(languageId)) {return lenses;}SyntaxTraverserPsiElement traverser SyntaxTraverser.psiTraverser(file);for (PsiElement element : traverser) {if (!(element instanceof PsiMethod)) {continue;}if (!InlayHintsUtils.isFirstInLine(element)) {continue;}String hint getName();TextRange range InlayHintsUtils.INSTANCE.getTextRangeWithoutLeadingCommentsAndWhitespaces(element);lenses.add(new Pair(range, new ClickableTextCodeVisionEntry(hint, getId(), new MyClickHandler((PsiMethod) element), null, hint, , List.of())));}return lenses;}NotNullOverrideDeprecatedpublic ListPairTextRange, CodeVisionEntry computeForEditor(NotNull Editor editor) {// 过时方法不用实现return List.of();}// Inlay被点击后的处理逻辑Overridepublic void handleClick(NotNull Editor editor, NotNull TextRange textRange, NotNull CodeVisionEntry entry) {if (entry instanceof CodeVisionPredefinedActionEntry) {((CodeVisionPredefinedActionEntry)entry).onClick(editor);}}RequiredArgsConstructorstatic class MyClickHandler implements Function2MouseEvent, Editor, Unit {private final PsiMethod psiMethod;// 点击inlay后的响应打开一个popup显示一组菜单public Unit invoke(MouseEvent event, Editor editor) {TextRange range InlayHintsUtils.INSTANCE.getTextRangeWithoutLeadingCommentsAndWhitespaces(psiMethod);int startOffset range.getStartOffset();int endOffset range.getEndOffset();editor.getSelectionModel().setSelection(startOffset, endOffset);AnAction action1 ActionManager.getInstance().getAction(MyPlugin.Action1);AnAction action2 ActionManager.getInstance().getAction(MyPlugin.Action2);DefaultActionGroup actionGroup new DefaultActionGroup(List.of(action1, action2));ListPopup popup JBPopupFactory.getInstance().createActionGroupPopup(null, actionGroup, EditorUtil.getEditorDataContext(editor), JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, true);popup.show(new RelativePoint(event));return null;}}
}注册VisionProvider的实现类
idea-pluginextensions defaultExtensionNscom.intellijcodeInsight.daemonBoundCodeVisionProvider implementationcom.demo.MyVisionProvider//extensions
/idea-plugin参考文献
Code Vision Provider