Dynamically display Gutter

已回答

I want to display an icon in the Gutter area when the mouse hovers over a Java method, similar to LineMarkerProvider, but what I want to do is hide it at other times.

I found a similar feature, "Toggle Rendered View", which only displays when the mouse is in the comments. How can I do this? Thank you

0

Consider the codeInsight.lineMarkerProvider extension point,Implement LineMarkerProvider interface

example

<codeInsight.lineMarkerProvider language="yaml"
                                implementationClass="xxx.PluginDartIconLineMark"/>
class PluginDartIconLineMark : LineMarkerProvider {

    override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<PsiElement>? {
        if (element.isDartPluginElement()) {
            val packageName = element.getPluginName()
            val igManager = DartPluginIgnoreConfig.getInstance(element.project)
            val isIgnored = igManager.isIg(packageName)
            return LineMarkerInfo(
                element.firstChild,
                element.firstChild.textRange,
                if (isIgnored) MyImages.ignore else MyIcons.dartPackageIcon,
                { element.text },
                PluginDartIconLineMarkNavHandler(element),
                GutterIconRenderer.Alignment.LEFT
            ) { "" }
        }
        return null
    }
}
0
private boolean hasRangeHighlighter(EditorMarkupModel editorMarkupModel) {
    for (RangeHighlighter allHighlighter : editorMarkupModel.getAllHighlighters()) {
        if (allHighlighter == rangeHighlighter) return true;
    }
    return false;
}

private synchronized void clearOldGutterIconRenderer(EditorMarkupModel markupModel) {
    if (rangeHighlighter != null) {
        rangeHighlighter.setGutterIconRenderer(null);
        if (hasRangeHighlighter(markupModel)) {
            markupModel.removeHighlighter(rangeHighlighter);
        }
        rangeHighlighter = null;
    }
}

@Override
public void mouseMoved(@NotNull EditorMouseEvent e) {
    EditorMouseMotionListener.super.mouseMoved(e);
    Project project = e.getEditor().getProject();
    if (project == null) return;
    Editor editor = e.getEditor();
    EditorMarkupModel markupModel = (EditorMarkupModel) editor.getMarkupModel();
    Document document = e.getEditor().getDocument();
    PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);

    if (psiFile == null) {
        clearOldGutterIconRenderer(markupModel);
        return;
    }
    PsiElement elementAt = psiFile.findElementAt(e.getOffset());
    PsiMethod method = PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
    if (method == null) {
        clearOldGutterIconRenderer(markupModel);
        return;
    }

    if (currentMethod != method) {
        currentMethod = method;
        clearOldGutterIconRenderer(markupModel);
    }

    TextRange textRange = currentMethod.getTextRange();
    if (textRange.equals(currentTextRange) && rangeHighlighter != null) {
        return;
    }
    currentTextRange = textRange;
    MethodGutterIconRenderer.INSTANCE.setMethod(currentMethod);
    int lineNumber = document.getLineNumber(textRange.getStartOffset());
    RangeMarker highlighter = LazyRangeMarkerFactory.getInstance(project).createRangeMarker(psiFile.getVirtualFile(), lineNumber, 0, true);
    rangeHighlighter =
            editor.getMarkupModel().addRangeHighlighter(null, highlighter.getStartOffset(), highlighter.getEndOffset(), 0, HighlighterTargetArea.EXACT_RANGE);
    rangeHighlighter.setGutterIconRenderer(MethodGutterIconRenderer.INSTANCE);
}

This is my code, although it displays the icon correctly, I cannot click on it. When I move the mouse nearby, I cannot obtain the location of PsiMethod through `PsiTreeUtil.getParentOfType`

0

See com.intellij.codeInsight.documentation.render.DocRenderItemManager#setupListeners for implementation of “Rendered Doc”

0

I cannot understand the approach of DocRenderItemManager. Do I need to retrieve all PsiMethods from the Editor and then view the PsiMethods that match the current mouse position?

 

My current problem is that if the mouse and Java method definitions are in the same position,` PsiFile.findElementAt(EditMouseEvent. getOffset())` cannot retrieve PsiMethod

0

Unfortunately, the DocRender functionality is pretty complex and depends on a number of additional classes/functionality so it cannot be easily matched to your plugin's needs.

Unless “dynamic” gutter is absolutely required, I'd strongly suggest to just use regular gutter icons.

0

请先登录再写评论。