Idiomatic way of replacing a line in the editor

I have a method for replacing a line in the editor with a given line and a row number, but it feels cumbersome and just wrong (though it works).

Is there a better way?

function overwriteLineAtRow(editor, rowNumber, newRowString) {
    editor.setCursorBufferPosition([rowNumber, 0]);
    editor.moveToEndOfLine();
    editor.deleteToBeginningOfLine();
    editor.insertText(newRowString);
}

It seems I’ll probably have to use the getBuffer() method on the editor object, I think.

Edit: This is my next version, but it still seems heavy-handed. Is there a single call that will do this?

function overwriteLineAtRow(editor, rowNumber, newRowString) {
    let deletedTextRange = editor.getBuffer().deleteRow(rowNumber);
    editor.getBuffer().setTextInRange(deletedTextRange, newRowString + "\n");
}

Also, not sure I should be assuming they want a “\n”. Without it, it consumes a newline, but its bad because it assumes they want a LF and not a CRLF, and it also assumes there are lines below it. Thats the great thing about the first solution, it doesn’t mess with the line endings, since its working directly on the editor.

  • It will normalise line endings by default, so \n is fine
  • You can just leave the text in the range you want to replace. So like
editor.setTextInBufferRange([[row, 0], [row, Infinity]], newText)

(this doesn’t require a manual newline either I think)
* Infinity seems safe to use, as I believe it will clip the range before replacing text, but I’ve had trouble with it for other line range stuff before. If this is for a package, I suggest finding the line length and using that instead.

Also yeah, manipulating the cursor in a command is probably a bad idea unless the command itself is meant to reposition the cursor. Always consider the possibility of multiple cursors when writing a package; setCursorBufferPosition will destroy them all. If you don’t want to handle multiple cursors, editor.getLastCursor is the easiest way to only work with the most recent one.

I tried to find out what the normalizeLineEndings option meant, is that just simply replacing whatever ending I insert, with the one is selected as the user’s default in the line-ending-selector setting?

Yes, it does it here if you’re interested.

This seems to work for me, I’m basically wanting the fastest, most efficient way of replacing the line. I don’t need to worry about multiple cursors, just whatever is the fastest method really. I assume buffer methods are much faster than editor methods.

That is effectively a buffer method; if you look at the source code linked to in the API, it is just calling that buffer method (and making sure it’s not a read only editor)

Oh you’re right, I was still thinking of the line I wrote involving getting the buffer, which, I guess, is still called on the editor object, so it is also an editor method.

Regardless, my biggest concern is speed. Whatever I can do that will be equivalent to what I was doing, but faster, will help.

With your solution being a one liner, I can move the code out of its own function and into the calling function, which alone will probably offer some improvements.