Link Search Menu Expand Document

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

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);
			});
		}
	});

Copyright © 2020 Thence LLC