I want to insert [11,12,...,20] in 10 lines of vscode
editor, somehow I want to simulate iota
functionality of golang
in vscode editor, for example let assume we had this lines in vscode editor:
line1 xxxxx
line2 xx
line3 xxxxxxxx
.
.
.
line10 xxx
I want to add [11,12,...,20] to end of each line with vscode custom snippets!
Desired output will be:
line1 xxxxx11
line2 xx12
line3 xxxxxxxx13
.
.
.
line10 xxx20
Is it possible with vscode provided snippets or we should develop an extension for this purpose ?
Note that the init value = 11
and the count=10
are user defined and not known in the begining.
CodePudding user response:
Here's an example how you can append numbers to the ends of the first 10 lines (or to the existing lines if there are fewer).
Excerpt from package.json
:
"activationEvents": [
"onCommand:stackoverflow-71294699.appendNumbers"
],
"contributes": {
"commands": [
{
"command": "stackoverflow-71294699.appendNumbers",
"title": "Append numbers"
}
]
},
extension.ts:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('stackoverflow-71294699.appendNumbers',
async function () {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
await editor.edit((editBuilder) => {
for (let i=0; i<10; i ) {
if (i >= editor.document.lineCount) {
break;
}
const line = editor.document.lineAt(i);
editBuilder.insert(
new vscode.Position(i, line.text.length),
"" (10 i)
);
}
});
})
);
}
export function deactivate() {}
CodePudding user response:
So select as many lines as you want. And replace 10
in the keybinding with whatever you want to add to each match.