The Text Editor API provides functionality to interact with text editors, manipulate text documents, apply edits, and control editor appearance and behavior.
editor.edit(editBuilder => { // Apply edits to all selections for (const selection of editor.selections) { const text = editor.document.getText(selection); editBuilder.replace(selection, text.toUpperCase()); }});
// Create a position (line 10, character 5)const pos = new vscode.Position(10, 5);// Compare positionsif (pos1.isBefore(pos2)) { console.log('pos1 comes before pos2');}// Translate positionconst newPos = pos.translate(1, 0); // Move down one lineconst relative = pos.translate({ lineDelta: -2, characterDelta: 3 });// Create new positionconst adjusted = pos.with(5, 10); // Line 5, character 10const sameLine = pos.with(undefined, 0); // Same line, character 0
// Create a rangeconst range = new vscode.Range(0, 0, 5, 10);// Or from positionsconst range2 = new vscode.Range( new vscode.Position(0, 0), new vscode.Position(5, 10));// Range propertiesconsole.log(range.isEmpty); // true if start === endconsole.log(range.isSingleLine); // true if start.line === end.line// Check containmentif (range.contains(position)) { console.log('Position is in range');}// Range operationsconst intersection = range1.intersection(range2);const union = range1.union(range2);
// Selection extends Range with anchor and activeconst selection = new vscode.Selection(10, 5, 15, 20);console.log(selection.anchor); // Position where selection startedconsole.log(selection.active); // Current cursor positionconsole.log(selection.isReversed); // true if anchor > active// Set selectioneditor.selection = new vscode.Selection(5, 0, 5, 10);// Multiple selectionseditor.selections = [ new vscode.Selection(0, 0, 0, 5), new vscode.Selection(1, 0, 1, 5), new vscode.Selection(2, 0, 2, 5)];
const document = editor.document;// Get all textconst fullText = document.getText();// Get text in rangeconst range = new vscode.Range(0, 0, 5, 0);const text = document.getText(range);// Get lineconst line = document.lineAt(10);console.log(line.text);console.log(line.firstNonWhitespaceCharacterIndex);// Get word at positionconst wordRange = document.getWordRangeAtPosition(position);if (wordRange) { const word = document.getText(wordRange);}
// Before savevscode.workspace.onWillSaveTextDocument(event => { const document = event.document; const reason = event.reason; // Provide edits to apply before save event.waitUntil( Promise.resolve([/* text edits */]) );});// After savevscode.workspace.onDidSaveTextDocument(document => { console.log('Saved:', document.fileName);});
const snippet = new vscode.SnippetString();// Use variablessnippet.appendText('File: ');snippet.appendVariable('TM_FILENAME', 'untitled');snippet.appendText('\n');snippet.appendText('Date: ');snippet.appendVariable('CURRENT_DATE', new Date().toISOString());editor.insertSnippet(snippet);
// Reveal a range in the editorconst range = new vscode.Range(100, 0, 100, 10);// Default - minimal scrollingeditor.revealRange(range);// Center in viewporteditor.revealRange(range, vscode.TextEditorRevealType.InCenter);// At top of viewporteditor.revealRange(range, vscode.TextEditorRevealType.AtTop);// Center if outside viewporteditor.revealRange( range, vscode.TextEditorRevealType.InCenterIfOutsideViewport);