Skip to content

TextEditor

Jan Richter edited this page Nov 19, 2021 · 10 revisions

editor

Lookup

import { TextEditor, EditorView } from 'vscode-extension-tester';
...
// to look up current editor
const editor = new TextEditor();
// to look up an open editor by name
const editor1 = await new EditorView().openEditor('package.json');

Text Retrieval

Note: Most text retrieval and editing actions will make use of the clipboard.

// get all text
const text = await editor.getText();
// get text at a given line number
const line = await editor.getTextAtLine(1);
// get number of lines in the document
const numberOfLines = await editor.getNumberOfLines();

Editing Text

// replace all text with a string
await editor.setText('my fabulous text');
// replace text at a given line number
await editor.setTextAtLine(1, 'my fabulous line');
// type text starting at given coordinates (line, column)
await editor.typeText(1, 3, ' absolutely');
// format the whole document with built-in tools
await editor.formatDocument();
// get the current cursor coordinates as number array [x,y]
const coords = await editor.getCoordinates();

Save Changes

// find if the editor has changes
const hasChanges = await editor.isDirty();
// save the document
await editor.save();
// save as, this only opens the save prompt
const prompt = await editor.saveAs();

Get Document File Path

const path = await editor.getFilePath();

Content Assist

// open content assist at current position
const contentAssist = await editor.toggleContentAssist(true);
// close content assist
await editor.toggleContentAssist(false);

Search for Text

// get line number that contains some text
const lineNum = await editor.getLineOfText('some text');
// find and select text
await editor.selectText('some text');
// get selected text as string
const text = await editor.getSelectedText();
// get selection block as a page object
const selection = await editor.getSelection();
// open the Find (search) widget
const find = await editor.openFindWidget();

Breakpoints

// toggle breakpoint on a line with given number
await editor.toggleBreakpoint(1);
Clone this wiki locally