Skip to content

TextEditor

Jan Richter edited this page Aug 6, 2019 · 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();

Save Changes

// find if the editor has changes
const hasChanges = await editor.isDirty();
// save the document
await editor.save();

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);
Clone this wiki locally