TextEditor


Represents an editor that is attached to a document.

For apis [hx. Window. GetActiveTextEditor ()] (/ ExtensionDocs/API/Windows/getActiveTextEditor) return values.

TextEditor

TextEditor Attribute list

Attribute name Type Description
document TextDocument The document associated with this text editor. The document will be the same for the entire lifetime of this text editor.
selection TextSelection The primary selection on this text editor. Shorthand for TextEditor.selections[0].
selections Array<TextSelection> The selections in this text editor. The primary selection is always at index 0.
options TextEditorOptions Text editor options.

edit

Perform an edit on the document associated with this text editor.

Parameter

Name Type Description
callback Function(TextEditorEdit) A function which can create edits using an edit-builder.

Returns

Type Description
Promise<void> Promise

Example

  1. let editorPromise = hx.window.getActiveTextEditor();
  2. editorPromise.then(function(editor) {
  3. let selection = editor.selection;
  4. let document = editor.document;
  5. let word = document.getText(selection);
  6. let reversed = word.split('').reverse().join('');
  7. editor.edit(editBuilder => {
  8. editBuilder.replace(selection, reversed);
  9. });
  10. });

setSelection

Set the main selection area, the API will clear your selection first, if you want to use multiple channels, please useaddSelection方法

Parameter

Name Type Description
active Number Select the side with the cursor, see the figure below
anchor Number Select the side without the cursor, see the figure below

TextEditor - 图1

Returns

Type Description
Promise<void> Promise

Example

  1. let editor = hx.window.getActiveTextEditor();
  2. editor.then((editor)=>{
  3. editor.setSelection(10,12);
  4. })

addSelection

Add a new selection area, and a new cursor will be added in the editor after calling.

Parameter

Name Type Description
active Number Select the side with the cursor, see the figure below
anchor Number Select the side without the cursor, see the figure below

TextEditor - 图2

Returns

Type Description
Promise<void> Promise

Example

  1. let editorPromise = hx.window.getActiveTextEditor();
  2. editorPromise.then((editor)=>{
  3. editor.setSelection(10,12).then(()=>{
  4. editor.addSelection(16,18);
  5. });
  6. })

TextDocument

Represents a text document, such as a source file. Text documents have lines and knowledge about an underlying resource like a file.

Attribute list

Attribute name Type Description
fileName String The file system path of the associated resource.
isDirty Boolean true if there are unpersisted changes.
isUntitled Boolean Is this document representing an untitled file which has never been saved yet.
lineCount Number The number of lines in this document
uri Uri The associated uri for this document.If it is local file, which can get local file path through uri.fsPath.
languageId String language Id,for example ‘javascript’,’html’ etc, see for complete id list Here
workspaceFolder WorkspaceFolder

The project object to which the document file belongs |

getText

Get the text of this document.

Name Type Description
range Range [Optional]Include only the text included by the range.

Returns

Type Description
String The text inside the provided range or the entire text.

Example

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. let text = editor.document.getText({
  4. start: 3755,
  5. end: 3802
  6. });
  7. console.log(text);
  8. });

lineAt

Returns a text line denoted by the line number.

Name Type Description
lineno Number A line number in [0, lineCount).

Returns

Type Description
Promise<TextLine> A Line

Example

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. // The row numbers start at 0
  4. let linePromise = editor.document.lineAt(2);
  5. linePromise.then((line)=>{
  6. console.log("TextLine is:", line.text);
  7. });
  8. });

lineFromPosition

Returns a text line denoted by the position.

Name Type Description
pos Number A position.

Returns

Type Description
Promise<TextLine> A Line

Example

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. let linePromise = editor.document.lineFromPosition(editor.selection.active);
  4. linePromise.then((line)=>{
  5. console.log("Line info:", line.text, "start:", line.start, "end:", line.end);
  6. });
  7. });

TextEdit

TextEdit: A text edit represents edits that should be applied to a document.

TextEdit Attribute list

Attribute name Type Description
range Range A range.
newText String A new text edit object.

replace static

Parameter

Name Type Description
range Range A range.
newText String A string.

Returns

Type Description
TextEdit A new text edit object.

Example

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. let selection = editor.selection;
  4. let word = editor.document.getText(selection);
  5. editor.edit(editBuilder => {
  6. // Converts the currently selected content from lowercase to uppercase
  7. let toUpperCase = word.toUpperCase();
  8. editBuilder.replace(selection, toUpperCase);
  9. });
  10. });

Range

Range: A range represents an ordered pair of two positions.

Attribute list

Attribute name Type Description
start Number The start position. It is before or equal to end.
end Number The end position. It is after or equal to start.

TextLine

TextLine: Represents a line of text, such as a line of source code.

Attribute list

Attribute name Type Description
start Number The start position of row.
end Number The end position of row without the line separator characters.
text String The text of this line without the line separator characters.