Search code examples
javafxrichtextfx

How do you get a paragraph number for a given position in a RichTextFX CodeArea?


Specifically I want to know the starting and ending lines of the user's current selection. The positions in the text are available from the selection, as in:

IndexRange selection = editor.getSelection();
int startPos = selection.getStart();
int endPos = selection.getEnd();

and as far as I can tell one of those positions is in the current paragraph (unless the cursor is at the beginning of the line after the selected line above?).

But how can I go from the other position to a paragraph (i.e. line) number?

I see that the starting and ending paragraphs are part of the SelectionImpl class https://javadoc.io/doc/org.fxmisc.richtext/richtextfx/latest/org/fxmisc/richtext/SelectionImpl.html But I can't see how to get an instance of that class representing the current selection.


Solution

  • I found the answer. The data is hidden within CaretSelectionBind:

        int startPar = editor.getCaretSelectionBind().getAnchorParIndex();
        int endPar = editor.getCaretSelectionBind().getParagraphIndex();