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
let editorPromise = hx.window.getActiveTextEditor();
editorPromise.then(function(editor) {
let selection = editor.selection;
let document = editor.document;
let word = document.getText(selection);
let reversed = word.split('').reverse().join('');
editor.edit(editBuilder => {
editBuilder.replace(selection, reversed);
});
});
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 |
Returns
Type | Description |
---|---|
Promise<void> | Promise |
Example
let editor = hx.window.getActiveTextEditor();
editor.then((editor)=>{
editor.setSelection(10,12);
})
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 |
Returns
Type | Description |
---|---|
Promise<void> | Promise |
Example
let editorPromise = hx.window.getActiveTextEditor();
editorPromise.then((editor)=>{
editor.setSelection(10,12).then(()=>{
editor.addSelection(16,18);
});
})
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
let activeEditor = hx.window.getActiveTextEditor();
activeEditor.then(function(editor) {
let text = editor.document.getText({
start: 3755,
end: 3802
});
console.log(text);
});
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
let activeEditor = hx.window.getActiveTextEditor();
activeEditor.then(function(editor) {
// The row numbers start at 0
let linePromise = editor.document.lineAt(2);
linePromise.then((line)=>{
console.log("TextLine is:", line.text);
});
});
lineFromPosition
Returns a text line denoted by the position.
Name | Type | Description |
---|---|---|
pos | Number | A position. |
Returns
Type | Description |
---|---|
Promise<TextLine> | A Line |
Example
let activeEditor = hx.window.getActiveTextEditor();
activeEditor.then(function(editor) {
let linePromise = editor.document.lineFromPosition(editor.selection.active);
linePromise.then((line)=>{
console.log("Line info:", line.text, "start:", line.start, "end:", line.end);
});
});
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
let activeEditor = hx.window.getActiveTextEditor();
activeEditor.then(function(editor) {
let selection = editor.selection;
let word = editor.document.getText(selection);
editor.edit(editBuilder => {
// Converts the currently selected content from lowercase to uppercase
let toUpperCase = word.toUpperCase();
editBuilder.replace(selection, toUpperCase);
});
});
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. |