Richtextfx: Question: Easy way to get line number from caret position

Created on 5 Mar 2018  路  11Comments  路  Source: FXMisc/RichTextFX

Hey,
This is a super simple Question, does the Api somehow provide a way to get the current lineNumber the caret is on?
If not i will write me a simple helper method.

Regards,

Yann "Liz3" HN

question

All 11 comments

ok
thx a lot

ERROR: Does not work...
I am calling it in area.caretPositionProperty().addListener(kotlin) and the Optional int is present, but always returns 0

I tried using area.getCaretSelectionBind().lineIndexProperty().addListener() and it never gets invoked, which is consistent then with what liz3 reports.

We have here a discrepancy between what the API seems to imply (user expectation) and what it actually does. So the method names (getLineIndex and lineIndexProperty) imply that you are going to get the currrent line index relative to the area / document, since that is what the method is being invoked on, but what is actually being calculated and returned is the index of the line relative to the current paragraph. Hence the above behavior and results.

The JavaDoc on the methods do mention that it is relative to the paragraph and not as one would expect relative to the document.

Here is my helper method(Kotlin), im sure there are better ways, but its working,
NOTE: this shall refer to the CodeArea:

fun CodeArea.getCaretLine(): Int {
    var count = -1
    var lines = 1
    val split = this.text.toCharArray()
    while (count != this.caretPosition) {
        count++
        if (count == split.size) break
        if (split[count] == '\n') lines++
    }
    if (count < split.size && split[count] == '\n') lines--
    return lines
}

Regards
Liz3

The API works as it should work and @Jugen's explanation is correct. The problem was the terminology that @liz3 used. She used the terminology line index to refer to what we would call the paragraph index. So, I thought she was really asking for the line index on a multi-line paragraph (i.e. when the paragraph's text is wrapped and spans multiple lines), not the paragraph index.

To get the area's main caret's current paragraph index, you can write area.getCurrentParagraph(); or area.getCaretSelectionBind().getParagraphIndex();

@liz3, have you read through the class-level javadoc of GenericStyledArea and the various @see OtherClass tags there at the bottom yet?

Here is a better way I think, Liz3 :-)

    area.caretPositionProperty().addListener( (ob,ov,nv) ->
    {
        int curPar = area.getCurrentParagraph(), lineCount = 0;
        for ( int p = 0; p < curPar; p++ ) {
            lineCount += area.getParagraphLinesCount( p );
        }
        lineCount += area.lineIndex( curPar, area.getCaretColumn() );
    });

This wouldn't be very efficient for very large documents. I would then split this with the currentParagraphProperty listener, moving the bulk paragraph count there.

Yeah ok, sorry that i ask the wrong thing!
(Kotlin) area.caretSelectionBind.paragraphIndex works fine

Thank you very much

There's no need to apologize, you didn't ask the wrong thing !

Be aware that area.caretSelectionBind.paragraphIndex won't give you the correct result if line wrapping is on, and the same problem probably also applies to the Kotlin code you provided.

@liz3 Jugen's right. It's not your fault and there's no need to apologize. It was simply a miscommunication and I also misunderstood you. Please, don't feel like you cannot ask other such questions in the future.

Was this page helpful?
0 / 5 - 0 ratings