TransWikia.com

VS Code Extension: Appending text to the end of a document

Stack Overflow Asked by Chris Lincoln on November 29, 2021

How do you append text past then last line of a document in the editor using an extension?

I have an extension that either creates a new, untitled document, or it appends text to the bottom/end only of an existing document. It is the latter case that I am having trouble with. The extension does not depend on caret/cursor/selection position. I’ve tried both edit.insert() and edit.replace(), with various position/range values of getting past the last character, but my text addition is always placed above the last line:

Before operation (line 20 is the last line of the document):

enter image description here

What I get. Note the existing blank line is below the inserted text:

enter image description here

What I want. Note the existing blank line is above the inserted text.:

enter image description here

The code:

var lastLine = editor.document.lineAt(editor.document.lineCount - 1);
    const replaceContent = 'Inserted Text';

    editor.edit((editBuilder) => {
      editBuilder.replace(lastLine.range.end, replaceContent);
    });

I’ve found lots of SO articles for inserting/replacing text, just nothing specific to adding to the very end of an editor buffer.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP