Method to Return Line Number Where Cursor is Located


Is there a method to return the line number from where the cursor is positioned?

let currentRow = editor.lineTextForBufferRow (//editor.getRowOfCursorsPlacement()?);

I might’ve missed it or maybe it doesn’t exist and there is another way to do this?

I also am looking for a simple way to replace the current line of text with a given string. Is TextEditor::setTextInBufferRange() the best way? Is there a method without having to specify a buffer range?

I’m currently calling TextEditor.deleteLine(); and TextEditor.insertText (currentRowString);, but this produces some issues I don’t really want to work around.


Ok, I think I found it. ::getBufferRow()


For clarification: I guess you mean Cursor::getBufferRow().

There also is a method on the TextEditor if you don’t need the cursor instance for anything else: TextEditor::getCursorBufferPosition() (or one of the other similar cursor methods right below in the link).

(If you want to replace the current (single?) cursor’s line) I think using TextEditor::setTextInBufferRange() is your best shot. The buffer range would be [[currentRow, 0], [currentRow, editor.lineTextForBufferRow() - 1]].

You could also try TextEditor::mutateSelectedText after selecting the line. This may be easier since there are the TextEditor::selectToFirstCharacterOfLine and TextEditor::selectToBeginningOfLine helper methods which let you choose whether to include the line-preceding white space or not. Of course, there also is TextEditor::selectToEndOfLine. Handling the cursor being in the preceding whitespace would require some extra logic but you get the idea.


Yep, The one I mentioned I found the method inside TextEditor, I should’ve clarified. Thank you.


Instead of creating a bunch of threads and clogging the forum, I figured I would just edit this one to allow me to ask a few more questions, if that is alright.