TextEditor - 文本编辑器对象 @TextEditor


TextEditor: 文本編輯器對象,爲API hx.window.getActiveTextEditor() 返回值的詳細說明文檔。

TextEditor

TextEditor屬性列表

屬性名 屬性類型 描述
document TextDocument 該編輯器關聯的文檔
selection TextSelection 當前光標選中的位置
selections Array<TextSelection> 當前多光標選中的位置集合
options TextEditorOptions 該編輯器的設置項

edit

修改當前編輯器打開的文檔

參數說明

參數名稱 參數類型 描述
callback Function(TextEditorEdit) 文檔編輯操作回調

返回值

返回類型 描述
Promise<void> Promise

示例

  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

設置主選擇區域,該API會首先清除原來的光標選擇,如果要使用多光標,請使用addSelection方法

參數說明

參數名稱 參數類型 描述
active Number 選擇區域中帶光標的一側,詳情見下圖
anchor Number 選擇區域中不帶光標的一側,詳情見下圖

TextEditor  - 文本编辑器对象 @TextEditor - 图1

返回值

返回類型 描述
Promise<void> Promise

示例

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

addSelection

增加新的選擇區域,調用後會在編輯器內追加一個新一個光標。

參數說明

參數名稱 參數類型 描述
active Number 選擇區域中帶光標的一側,詳情見下圖
anchor Number 選擇區域中不帶光標的一側,詳情見下圖

TextEditor  - 文本编辑器对象 @TextEditor - 图2

返回值

返回類型 描述
Promise<void> Promise

示例

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

TextDocument

編輯器打開的文檔文件

屬性列表

屬性名 屬性類型 描述
fileName String 文件名稱
isDirty Boolean 是否是修改狀態
isUntitled Boolean 是否是無標題文件
lineCount Number 文檔總行數
uri Uri 文檔的uri,如果是本地文件,可通過uri.fsPath獲取本地文件路徑
languageId String 編程語言Id,如’javascript’,’html’等,完整id列表參見這裏
workspaceFolder WorkspaceFolder 該文檔文件所屬的項目對象

getText

獲取指定區域內的文本

參數名稱 參數類型 描述
range Range [可選]文本區域,如果不傳該參數,則獲取整個文檔的內容

返回值

返回類型 描述
String 文本字符串

示例

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. // 获取指定区域内的文本
  4. let text = editor.document.getText({
  5. start: 3755,
  6. end: 3802
  7. });
  8. console.log(text);
  9. });

lineAt

獲取指定行號的行信息

參數名稱 參數類型 描述
lineno Number 行號,從0開始

返回值

返回類型 描述
Promise<TextLine> 文本行對象

示例

  1. let activeEditor = hx.window.getActiveTextEditor();
  2. activeEditor.then(function(editor) {
  3. // 行号是从0开始
  4. let linePromise = editor.document.lineAt(2);
  5. linePromise.then((line)=>{
  6. console.log("TextLine is:", line.text);
  7. });
  8. });

lineFromPosition

根據光標位置獲取光標所在行。

參數名稱 參數類型 描述
pos Number 光標位置

返回值

返回類型 描述
Promise<TextLine> 文本行對象

示例

  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, "开始位置:", line.start, "结束位置:", line.end);
  6. });
  7. });

TextEdit

TextEdit: 文檔編輯

TextEdit 屬性列表

屬性名 屬性類型 描述
range Range 要修改的區域
newText String 要插入的新內容

replace static

參數說明

參數名稱 參數類型 描述
range Range 要修改的區域
newText String 要插入的新內容

返回值

返回類型 描述
TextEdit 文檔編輯對象

示例

  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. // 把当前选中的内容由小写转换大写
  7. let toUpperCase = word.toUpperCase();
  8. editBuilder.replace(selection, toUpperCase);
  9. });
  10. });

Range

Range: 文本區域

屬性列表

屬性名 屬性類型 描述
start Number 起始位置
end Number 結束位置

TextLine

TextLine: 文档中的某一行, 为editor.document.lineAteditor.document.lineFromPosition 返回值。

屬性列表

屬性名 屬性類型 描述
start Number 行起始位置
end Number 行結束位置,不計算換行符
text String 行內容,不包含換行符