Text-editor
Methods
edit
- sig: (callback: (editBuilder: TextEditorEdit) => void, options?: {undoStopAfter: boolean, undoStopBefore: boolean}): Thenable
- return: Thenable
- A promise that resolves with a value indicating if the edits could be applied.
Perform an edit on the document associated with this text editor.
The given callback-function is invoked with an edit-builder which must be used to make edits. Note that the edit-builder is only valid while the callback executes.
TextEdit
static
delete(range: Range): TextEdit
insert(position: Position, newText: string): TextEdit
replace(range: Range, newText: string): TextEdit
setEndOfLine(eol: EndOfLine): TextEdit
TextEditorEdit
A complex edit that will be applied in one transaction on a TextEditor. This holds a description of the edits and if the edits are valid (i.e. no overlapping regions, document was not changed in the meantime, etc.) they can be applied on a document associated with a text editor.
=== Cook
get access to all text editors
- https://github.com/eamodio/vscode-restore-editors/blob/master/src/documentManager.ts#L57
- https://marketplace.visualstudio.com/items?itemName=eamodio.restore-editors
- https://github.com/microsoft/vscode/issues/15178
- https://github.com/microsoft/vscode/issues/49740
set the cursor
const editor = (await VSCodeUtils.openFileInEditor(
vscode.Uri.file(notePath)
)) as vscode.TextEditor;
editor.selection = new vscode.Selection(7, 0, 7, 12);
make an edit
- src/extension.ts
- source: vscode-extension-samples/document-editing-sample/src/extension.ts
const disposable = vscode.commands.registerCommand('extension.reverseWord', function () {
// Get the active text editor
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// Get the word within the selection
const word = document.getText(selection);
const reversed = word.split('').reverse().join('');
editor.edit(editBuilder => {
editBuilder.replace(selection, reversed);
});
}
});