如何在 vscode 片段中增加一个变量,比如行号 [英] How to increment a variable, like the line number, in a vscode snippet

查看:92
本文介绍了如何在 vscode 片段中增加一个变量,比如行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VScode 中有一个多行代码段.问题是,TM_LINE_NUMBER 给出了触发代码段的行号,我需要将该数字增加 1,使其等于实际所在的行号.

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}');"
  ]
},

我该怎么做?

推荐答案

至少有这两个选项:

"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. 您将需要类似于 多命令扩展一>.

将上面的代码段更改为:

Change above snippet to :

"Console_Log_Test": {

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

所以现在代码段只打印包含 TM_LINE_NUMBER 的行.

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

  1. 在您的用户设置中:

  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. 在您的 keybindings.json 中:

  1. In your keybindings.json:

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

现在 Ctrl-Alt-L 正是您想要做的.设置起来有点麻烦,但学习方法非常强大.

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.

我想知道是否有一种方法可以让 emmet 数学发挥作用,但令人惊讶的是,确实如此.

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

使用此代码段:

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

那个片段除了最后的 ');

现在这个宏:

{
    "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"
        }
      }
    ]
  },

会起作用!!行号将加一,然后 ');\n 将添加到该行的末尾.

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

并且您可以使用 "editor.emmet.action.evaluateMathExpression" 代替 incrementNumberByOne 命令进行更高级的数学计算.

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

要将 10 添加到行号,请使用

To add 10 to the line numbers, use

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

在代码段和 "editor.emmet.action.evaluateMathExpression" 代替 "editor.emmet.action.incrementNumberByOne" 在多命令宏中.

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

这篇关于如何在 vscode 片段中增加一个变量,比如行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆