Search code examples
javaintellij-ideaintellij-pluginstatic-code-analysis

How to highlight one line contains a specific PsiElement programmably in Intellij IDEA?


I know how to highlight a PsiElement, But how to do it for one line? I want highlight java source code in my plugin like the test coverage do.


Solution

  • Use these code in the UI thread, can accomplish the goal.

    int lineNum = document.getLineNumber(needHighlightPsiElement.getTextRange().getStartOffset());
    final TextAttributes textattributes = new TextAttributes(null, backgroundColor, null, EffectType.LINE_UNDERSCORE, Font.PLAIN);
    final Project project = needHighlightPsiElement.getProject();
    final FileEditorManager editorManager = FileEditorManager.getInstance(project);
    final Editor editor = editorManager.getSelectedTextEditor();    
    editor.getMarkupModel().addLineHighlighter(lineNum, HighlighterLayer.CARET_ROW, textattributes);