2

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.

S4eed3sm
  • 1,398
  • 5
  • 20

2 Answers2

1

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() {}
md2perpe
  • 3,372
  • 2
  • 18
  • 22
1

Using an extension I wrote you can do math easily on lines/selections/document, etc. The extension is Find and Transform.

With this keybinding (or it could be a setting):

{
  "key": "alt+r",                 // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    
    "find": "(?<=\\w)$",
    "replace": "$${ return ${matchNumber} + 10 }$$",
    "isRegex": true,
    "restrictFind": "selections"
}

find: (?<=\\w)$ matches end of line where there is text

or

find: $ matches end of all lines - blank or not

replace: $${ return ${matchNumber} + 10 }$$ will add to the matchNumber - which starts at 1 - and it will go on the end of the line

restrictFind: selections Do this only in any selections

Demo:

add 10 to matchNumber at end of line

So select as many lines as you want. And replace 10 in the keybinding with whatever you want to add to each match.

Mark
  • 143,421
  • 24
  • 428
  • 436