How to increment a variable, like the line number, in a vscode snippet

早过忘川 提交于 2020-06-29 04:28:23

问题


I have a multi-line snippet in VScode. The problem is, TM_LINE_NUMBER gives the number of the line where the snippet was triggered, I need to increase that number by 1 so it equals the line number that it is actually on.

"Console_Log_Test": {
  "prefix": "clg",
  "body": [
    "//Debugging (remove)",
    "console.log('Line #${TM_LINE_NUMBER}');"
  ]
},

How can I do that?


回答1:


There are at least these two options:

"Console_Log_Test": {
      "prefix": "clg",
      "body": [
          "//Debugging (remove)",
          "console.log('Line #${1:${TM_LINE_NUMBER}}');"
          // "console.log('Line #${TM_LINE_NUMBER}');"
      ]
}

With the above at least the line number is selected and you could easily change it yourself.

More interesting is to make this into a "macro" which will accomplish exactly what you want.

  1. You will need something like the multi-command extension.

  2. Change above snippet to :

    "Console_Log_Test": {
    
      "prefix": "clg",
      "body": [
          "console.log('Line #${TM_LINE_NUMBER}');"
      ]
    }
    

so now the snippet only prints the line with the TM_LINE_NUMBER in it.

  1. In your User Settings:

    "multiCommand.commands": [
    
    {
      "command": "multiCommand.lineNumber",
      "sequence": [
        {
          "command": "type",
          "args": {
            "text": "//Debugging (remove)\n"
          }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": {
            // "langId": "csharp",
            "name": "Console_Log_Test"
          }
        }
      ]
    }
    

Now the snippet is actually triggered on the line number you want.

  1. In your keybindings.json:

    {
      "key": "ctrl+alt+l",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.lineNumber" }
    },
    

Now Ctrl-Alt-L does exactly what you want to do. It is a bit of work to set up but a pretty powerful method to learn.

[EDIT]

I was wondering if there was a way to get some emmet math to work and, surprisingly, it does.

Using this snippet:

 "log line number on second line": {
      "prefix": "clg",
      "body": [
          "//Debugging (remove)",
          "console.log('Line #${TM_LINE_NUMBER}"
      ]
  },

That snippet has everything but the final ');

Now this macro:

{
    "command": "multiCommand.lineNumber",
    "sequence": [
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "name": "log line number on second line"
        }
      },
      "editor.emmet.action.incrementNumberByOne",
      {
        "command": "type",
        "args": {
          "text": "');\n"
        }
      }
    ]
  },

will work!! The line number will be incremented by one and then ');\n will be added to the end of that line.

And you can do fancier math using "editor.emmet.action.evaluateMathExpression" in place of the incrementNumberByOne command.

To add 10 to the line numbers, use

"console.log('Line #${TM_LINE_NUMBER}+11"

in the snippet and "editor.emmet.action.evaluateMathExpression" in place of "editor.emmet.action.incrementNumberByOne" in the multicommand macro.



来源:https://stackoverflow.com/questions/53305549/how-to-increment-a-variable-like-the-line-number-in-a-vscode-snippet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!