William Brach
William Brach

Reputation: 51

Java Intelij Idea how to add gutter icon?

I have a problem with rendering gutter icons, in my IntelliJ idea plugin project. problem

I want to render gutter icon next to a line number, but it renders only small rectangle. Gutter icon path is loaded properly and gutter icon size is 12x12px, format png. Can you help me?

My code:

public static void addLineHighlight(Document document, int lineNumber,
                                        String text) {


        Icon highlightIcon = IconLoader.getIcon("META-INF/fail.png");

        addGutterIcon(getRangeHighlighter(document, lineNumber), highlightIcon, text);
    }


    @NotNull
    private static RangeHighlighter getRangeHighlighter(Document document, int lineNumber) {
        MarkupModel markupModel = getMarkupModel(document);
        TextAttributes textAttributes = getTextAttributes();

        RangeHighlighter highlighter;
        highlighter = markupModel.addLineHighlighter(lineNumber, 66 , textAttributes);
        return highlighter;
    }

    private static void addGutterIcon(@NotNull RangeHighlighter highlighter, Icon icon, String text) {

        highlighter.setGutterIconRenderer(new GutterIconRenderer() {
            @Override
            public boolean equals(Object obj) {
                return false;
            }

            @Override
            public int hashCode() {
                return 0;
            }

            @NotNull
            @Override
            public Icon getIcon() {
                return icon;
            }
        });

    }

    private static MarkupModel getMarkupModel(Document document) {
        return DocumentMarkupModel.forDocument(document, TestSingleton.getInstance().getProject(), true);
    }

    @NotNull
    private static TextAttributes getTextAttributes() {
        TextAttributes textAttributes = null;
        textAttributes = new TextAttributes();
        textAttributes.setBackgroundColor(JBColor.RED);
        textAttributes.setErrorStripeColor(JBColor.RED);
        return textAttributes;
    }
}

Upvotes: 3

Views: 1107

Answers (1)

Koyasha
Koyasha

Reputation: 6817

I believe you should be using com.intellij.codeInsight.daemon.LineMarkerProvider.

See this post and this example.

Upvotes: 2

Related Questions