Highlight Range not working

Answered

Hello,

I'm trying to highlight the right gutter for a specified range of lines when a menu item is selected:

public void actionPerformed(AnActionEvent event) {
final Editor editor = event.getData(CommonDataKeys.EDITOR);
RangeHighlighter highlighter = editor.getMarkupModel().addRangeHighlighter(4, 8, 0, null, HighlighterTargetArea.EXACT_RANGE);
highlighter.setErrorStripeMarkColor(JBColor.MAGENTA);
}

But this code only makes a small highlight and always at the top:

Am I passing in the right params? I read through it's definition here and thought I used the method properly.

0
11 comments

startOffset/endOffset are not line numbers. These are character numbers, so you have to find lines boundaries first.

Editor editor = event.getData(CommonDataKeys.EDITOR);
Document document = editor.getDocument();
int startOffset = document.getLineStartOffset(4);
int endOffset = document.getLineEndOffset(7); // assuming open line range [4-8)
RangeHighlighter highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, 0, null, HighlighterTargetArea.EXACT_RANGE);
highlighter.setErrorStripeMarkColor(JBColor.MAGENTA);
0

Thanks that helped, it's now highlighting lines, but highlighting the wrong ones:

 

so for:

int startOffset = document.getLineStartOffset(4);
int endOffset = document.getLineEndOffset(7);

 it highlights lines 6-8

 

 

 

0

1. Internal line numbers are starting from 0.

2. Markers on scrollbar are not aligned 1-to-1 to editor lines. 
ex: compilation error produces only small dash, without painting whole area related to the line.
Here, the markers on scrollbar are also shifted by code analysis indicator at the top (green check mark).

0

So then is there no way to ensure that the highlighter will start highlighting at the correct internal line?

0

What task are you trying to solve? What should be the correct internal line?

What I am trying to say, is that these markers are shown for the whole document, and not only for the visible part.
And if the document is longer than one screen, they have to be compressed (and your highlighter for 5 lines will occupy less than 5-line-heights of the scrollbar).

0

The outcome I was trying to achieve was to create some sort of marker to highlight specific lines. The main part of my plugin returns the line numbers of a range of lines to call to the user's attention.

So I wanted to highlight/mark the returned lines and include some sort of annotation for each group of lines.

0

In this case, these should be "highlighter starting at correct internal line" you are looking for.

Ex: you can compare them with an inspection warning for the same line range.

0

So is there no way to find the offset into the document for a particular internal line?

Since I won't have the PSI elements of the lines that I'm looking to mark, I'm having trouble finding an example of using an internal line to get the offset.

0

Won't Document solve this?

Document document = editor.getDocument();
int startOffset = document.getLineStartOffset(lineNumber);
int endOffset = document.getLineEndOffset(lineNumber);

 

0

No it didn't solve that because it doesn't highlight the right gutter of the the lines specified by startOffset and endOffset.

I'm trying to find a way to create a marker for a given line number in a file.

Ideally the marker would provide some added information when clicked/hovered over.

0

> No it didn't solve that because it doesn't highlight the right gutter of the lines specified by startOffset and endOffset.

It does highlight the correct lines. But:

  1. Scrollbar itself is shifted one line down because if the Inspections marker.
  2. Line numbers start with 0

So, the document.getLineStartOffset(4) actually returns startOffset of the 5th line in Editor, and getLineEndOffset(7) of the 8th line => it actually highlights line 5-7, and the highlighting is shifted one line as the entire scrollbar. That is why it looks like 6-8 are highlighted.

> I'm trying to find a way to create a marker for a given line number in a file.

 As Alexey noted, Scrollbar is not expected to always be aligned 1-1 with the editor. Seems you need some markers in the left gutter instead.

Check around

com.intellij.openapi.editor.markup.RangeHighlighter#setLineMarkerRenderer com.intellij.openapi.editor.markup.LineMarkerRendererEx#getPosition

0

Please sign in to leave a comment.