vscode插件开发----编辑器中当前行及选中行的文本并替换
当前行的内容替换:
func.myRegisterCommand('codeStat.read',()=>{
const editor = vscode.window.activeTextEditor;
if(editor===undefined){return;};
const cursorPosition = editor.selection.active;
const line = editor.document.lineAt(cursorPosition.line);
const text = line.text;
vscode.window.showInformationMessage(text);
const newText = "new text";
const startPosition = new vscode.Position(cursorPosition.line, 0);
const endPosition = new vscode.Position(cursorPosition.line, line.range.end.character);
const range = new vscode.Range(startPosition, endPosition);
editor.edit((editBuilder) => {
editBuilder.replace(range, newText);
});
})
选中行的内容替换(选中行包括当前行):
func.myRegisterCommand('codeStat.read',()=>{
const editor = vscode.window.activeTextEditor;
if(editor===undefined){return;};
const selection = editor.selection;
const startLine = selection.start.line;
const endLine = selection.end.line;
editor.edit((editBuilder) => {
for (let i = startLine; i <= endLine; i++) {
const line = editor.document.lineAt(i);
const text = line.text;
const newText = text + '\t#H.Read';
const startPosition = new vscode.Position(i, 0);
const endPosition = new vscode.Position(i, line.range.end.character);
const range = new vscode.Range(startPosition, endPosition);
editBuilder.replace(range, newText);
};
});
})