Proper way (or example code) on how to hightlight a line(s) of text in an editor window

Answered

I have the need to highlight and provide a tooltip for code open in an Editor pane. I am successfully opening the file I need to highlight in an editor tab and have the following data:

  • Start line
  • End line
  • Startoffset
  • Endoffset

I've looked through several examples in these forums, but can't seem to get it working correctly. Any help or additional links are appreciated.

2 comments
Comment actions Permalink

While not exactly what I was looking to do, this is meeting my current need.

Editor editor = FileEditorManager.getInstance(PROJECT).openTextEditor(new OpenFileDescriptor(PROJECT, FILE.getVirtualFile()), true);
TextAttributes att = new TextAttributes();
att.setErrorStripeColor(JBColor.CYAN);
att.setBackgroundColor(JBColor.CYAN);

// Set line highlights using TextRange object
int startLine = STARTLINE - 1;
int endLine = ENDLINE - 1;
for (int i = startLine; i <= endLine; i++) {
RangeHighlighter hl = editor.getMarkupModel().addLineHighlighter(i, 0, att);
hl.setErrorStripeTooltip(TOOLTIP_MESSAGE);
}

In this code:

  1. FILE = PsiFile that was retrieved using FilenameIndex.getFilesByName, this is being opened into the IDE and I get a reference to the current editor here as well
  2. PROJECT = The current project
  3. STARTLINE and ENDLINE = These get passed into my method

This is giving me a highlighted line of code, with a gutter line that holds the TOOLTIP_MESSAGE.

1
Comment actions Permalink

See com.intellij.codeHighlighting.TextEditorHighlightingPass and inheritors

0

Please sign in to leave a comment.