I'm creating an extension for vs code and want to clear existing text from the document and replace it with new text. as of now, I'm able to delete selected content only. tried the different solution but didn't worked
function clearAllText(editor = vscode.window.activeTextEditor){
if (!editor) {
return;
}
editor.edit(builder => {
const cursorPos = editor.selection.anchor;
builder.delete(editor.document.lineAt(cursorPos).range);
});
if(editor.document.lineCount>0){
clearAllText();
}
}
Any help will be appriciated.
CodePudding user response:
await vscode.commands.executeCommand('editor.action.selectAll`);
await vscode.commands.executeCommand('editor.action.clipboardCutAction');
Just select it all and cut it.
If you want to use the delete(Range)
method, then use the editor.document.lineCount
property to know how many lines there are in your document.
builder.delete(new Range(new Position(0, 0), new Position(editor.document.lineCount-1, 1000))));
[You could get the number of characters on the last line too. editor.document.lineAt(editor.document.lineCount-1).range.end.character);
]