Different line startoffsets from Document
I am writing a plugin which adds some visuals using the Graphics object to the start of a line. I have the following sample code (with line numbers added):
1. public class Test {
2. public static void main(String[] args) {
3. System.out.println(getName());
4. System.out.println(getName());
5. }
6.
7. private static String getName() {
8. return "Jamie";
9. }
10. }
When I run mu plugin I calculate the line start offsets as follows (for example for the getName() element at line 3 and 4):
final int lineStartOffset = DocumentUtil.getLineStartOffset(element.getTextOffset(), editor.getDocument());
However this gives me a lineStartOffset of 65 for line 3 and a startOffset of 104 for line 4.
Is this the correct way of doing this for my purpose?
Please sign in to leave a comment.
I think I am figuring out how thinks work. The offsets for consecutive lines add up (so it seems). So to get the start offset of a line I subtract the startOffset from the endOffset for the current line.
getLineStartOffset method (in both Document and DocumentUtil) returns offset in characters from document start till given line start. I couldn't understand what other value you need instead.
Yes, thanks. I figured that out already. Is there a method to convert the offset in characters to a X-position in the editor (for drawing purposes)?
I used the editor.visualPositionToPoint2D to convert the offset to a visual position in the editor.
> editor.visualPositionToPoint2D
That's the correct way to do it. To obtain visual position, you can use editor.offsetToVisualPosition method.