0

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.

Chris Lincoln
  • 368
  • 4
  • 13
  • Does this answer your question? [VSCode extension - how to alter file's text](https://stackoverflow.com/questions/53585737/vscode-extension-how-to-alter-files-text) – soulshined Jul 22 '20 at 19:28
  • @soulshined, Unfortunately, no. I did you that post to get to where I am now though. – Chris Lincoln Jul 22 '20 at 19:38
  • That repo mentioned with the example didn't help either? https://github.com/Microsoft/vscode-extension-samples/tree/master/document-editing-sample Is there an exact issue with your approach? Meaning, is it working for you, but just not for the last line? Is the function being called? – soulshined Jul 22 '20 at 19:40
  • No, the example just reverses the characters of whatever text is highlighted in the current editor. I'll try to make it clearer, but I thought the 3 little screen shots pretty much nailed it. I can successfully modify the document, just not the exact position. – Chris Lincoln Jul 22 '20 at 19:44
  • The sample...is just a sample, but it uses all the components you need to accomplish this. What your expectations are, is clear. What's not working with your approach isn't, at least to me anyways. Meaning, where is it failing exactly? That's why I asked if it's working for you every other way, except at the end of the line, or is it not working in general? – soulshined Jul 22 '20 at 19:46
  • @soulshined, Thanks, I edited the "what happens" vs. "what I want" sections for clarity. Let me know if that paints a better picture. – Chris Lincoln Jul 22 '20 at 19:48
  • Got ya, thanks. I see. And what happens when you don't subtract 1 from the line count?: `editBuilder.insert(new vscode.Position(editor.document.lineCount, 0), value)` and if you specifically want that on its own line, prefix value with an eol char: `\n${value}` – soulshined Jul 22 '20 at 20:02
  • It crashes because the document.lineAt() function is 0-based. However, I have tried other intentional trickery using new Position() just to both subtly and blatantly go past that point, which do not crash, but also do not exhibit a different behavior. – Chris Lincoln Jul 22 '20 at 20:06
  • I didn't recommend `lineAt()` or replace – soulshined Jul 22 '20 at 20:07
  • For brevity, I don't know why it's not working, I'm just trying to see if not using lineAt() works for you. In reality, what you have should work, but it's important to note, per their docs, that it could possibly be because the file is dirty or some other reason, because `lineAt()` is not live – soulshined Jul 22 '20 at 20:26
  • Gotcha. No joy on your suggestion. I read it wrong the first time. But I will heed your lineAt() advice. The file has been dirty when I've been troubleshooting. I'll stick with your technique and move on. This isn't a major issue. – Chris Lincoln Jul 22 '20 at 21:21
  • Well it should be somewhat major considering you are manipulating editors :) But if my suggestion didn't work no worries just trying to help. It will at least make it easier for others who read this narrow it down. FYI, might also be worth a try to run without extensions enabled, maybe another extension is conflicting, like a formatter? – soulshined Jul 22 '20 at 21:27

1 Answers1

0
// Get the text editor object
const editor = vscode.window.activeTextEditor;

// Get the current document object
const document = editor.document;

// Get the last line of the document
const lastLine = document.lineAt(document.lineCount - 1);

// Get the last line text range
const range = new vscode.Range(lastLine.range.start, lastLine.range.end);

// Append the text to the document
editor.edit((editBuilder) => {
  editBuilder.insert(range.end, "\nAppended text");
});
cs01
  • 5,287
  • 1
  • 29
  • 28