Home > Net >  Snippet that depend on the selection
Snippet that depend on the selection

Time:11-02

I'm looking for a tool that could help me to do the following operation:

Given that code:

const before = (arg1: string, arg2: string) => null;

I would like to have the following result:

const after = ({ arg1, arg2 }: { arg1: string; arg2: string }) => null;

Is it possible to do that with a snippet ? (for exemple, i select my args and it directly transform them)

CodePudding user response:

Define the following Typescript snippet

  "Convert Arguments": {
    "prefix": "conarg",
    "body": [
      "{ ${TM_SELECTED_TEXT/(\\w ):[^,]*(,\\s*)?/$1$2/g} }: { ${TM_SELECTED_TEXT/,/;/g}}"
    ],
    "description": "Convert Arguments"
  }
  • select the arguments between ()
  • type conarg and select the snippet

CodePudding user response:

As I generally need to execute a considerably big list of changes, I tend to use Tasks (Ctrl Shift P/Run Task/[task name])
https://code.visualstudio.com/docs/editor/tasks

        {
            "label": "vueconv",
            "type": "shell",
            "command": "tsx scripts/vueconv ${file}",
            "problemMatcher": [],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": false,
                "clear": true
            }
        },

My scripts read the file, make fixes (mostly with string.replace(/regex/g, callback)), and overwrite the file
I only use ${file}, but if you can also add line number or selection as script arguments
https://code.visualstudio.com/docs/editor/variables-reference

self-made somple replacer library

  • Related