我知道如何獲取游標的位置:
editor.selection.active
這將產生類似于: { _character: 4, _line 1 }
現在,我想匹配一個字符或單詞(在活動編輯器中)并獲取它的行號和字符位置:
const editor = vscode.window.activeTextEditor
let text = editor.document.getText()
const match = text.match(/match/)
// What should I write here?
如何獲取匹配(或第一個匹配)的行號和字符位置?
我在 Google 或 VS Code API 的檔案中找不到任何內容。
uj5u.com熱心網友回復:
使用TextDocument 中的方法
positionAt(offset: number): Position
將基于零的偏移量轉換為位置。
在match已經偏移(開始比賽)。
uj5u.com熱心網友回復:
遍歷檔案的行,并計算行號:
const editor = vscode.window.activeTextEditor;
let lines = editor.document.getText().split(“\n”);
for (let i=0;i<lines.length;i )
{
const match = lines[i].match(/match/);
if (match)
{
let char = match[1].index;
let lineNb = i;
break;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331224.html
