how to apply edits to text document from server side of LSP language server vscode extension? Maybe I have to create a WorkspaceEdit structure and send a workspace/applyEdit request to the client? Looking for some sample code I can review.
What I am looking to do: In the initial onDidChangeContent event ( when file is opened ), I want the LSP server to review the code in the opened file and possibly insert some missing code.
I needed to do something similar ... Using this post for inspriration ... I came up with this silly function for adding new text to a file. Note: I am calling this from my: connection.onCompletion() function
function addSomeText(documentURI:string, position: Position) {
// Sample function for pushing next text to an open document.
const textToAdd1: string = "You're a bozo 1";
const textToAdd2: string = "You're a bozo 2";
const textToAdd3: string = "You're a bozo 3";
const insertPosition1: Position = Position.create(position.line + 1, 0);
const insertPosition2: Position = Position.create(position.line + 2, 0);
const insertPosition3: Position = Position.create(position.line + 3, 0);
let workspaceChange = new WorkspaceChange();
let textChange = workspaceChange.getTextEditChange(documentURI);
textChange.insert(insertPosition1, textToAdd1);
textChange.insert(insertPosition2, textToAdd2);
textChange.insert(insertPosition3, textToAdd3);
// pass these edits to the client to apply:
let reply = connection.workspace.applyEdit(workspaceChange.edit);
console.log (reply);}