通过VSCODE摘要中的输入文本

发布于 2025-01-17 21:07:24 字数 477 浏览 4 评论 0 原文

阅读 sminippets文档片段主体在摘要前缀之后键入它。

假设我有一个带有 log 前缀的摘要,我想在 log> log(*+)之类的内容中转换前缀以捕获文本之后。

如果我键入 loganObject ,则剪切应捕获“ AnObject”,并致电我的摘要以插入一些文本,例如 console.log(“ AnObject的值为:”,AnObject); 。

有可能吗?

我认为不可能是不可能的,因为VSCODE应该认识到我仍在键入摘要的前缀,并且在摘要主体内应捕获和使用前缀后的文字,但我希望他们意识到这样的东西。

After reading the snippets documentation I'm still searching a way to pass some text inside the snippet body by typing it after the snippet prefix.

Let's say I have a snippet with log prefix, I would like to transform the prefix in something like log(*+) to capture the text after.

If I type loganObject, the snipped should capture "anObject" and call my snippet to insert some text like console.log ("the value of anObject is: ", anObject);.

Is that possible?

I think that it is not possible because vscode should recognize that I'm still typing the prefix of the snippet and the text after the prefix should be captured and used inside the snippet body, but I hope they realized something like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2025-01-24 21:07:24

这是一个可以执行您想要的操作的键绑定,但您必须首先选择文本 Shift+Home,这是该行中唯一的文本。

使用 log.myTextHere 作为匹配文本,即开头的 log. 只是为了清楚起见。

在您的 keybindings.json 中:

{
  "key": "alt+l",                   // whatever keybinding you want
  "command": "editor.action.insertSnippet",
  "args": {
    // get the extra text into capture group 1
    "snippet": "console.log (\"${TM_SELECTED_TEXT/^log\\.(.*)/the value of $1 is: \", $1/});"
  }
}

console.log snippet demo


另外,更简单、更灵活的是使用我编写的扩展,查找并变换,执行此操作。使用此键绑定:

{
  "key": "alt+y",                   // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "preCommands": "cursorHomeSelect",    // does the selection for you
    "replace": [
      "${",
              // log. = 4
      "let myVar = '${selectedText}'.substring(4);",
      "return `console.log (\"the value of ${myVar} is: \", ${myVar});`",
      "}$"
    ],
    "restrictFind": "line"
  }
}

如果您只想使用 log 而不是 log.,则可以更改 substring(4)

日志片段带有查找和转换扩展

Here is a keybinding that does what you want, but you have to select the text first Shift+Home and that is the only text on the line.

Using log.myTextHere as the matched text, i.e., log. at the beginning just to be clear.

In your keybindings.json:

{
  "key": "alt+l",                   // whatever keybinding you want
  "command": "editor.action.insertSnippet",
  "args": {
    // get the extra text into capture group 1
    "snippet": "console.log (\"${TM_SELECTED_TEXT/^log\\.(.*)/the value of $1 is: \", $1/});"
  }
}

console.log snippet demo


Alternatively, and a little easier and more flexible, is to use an extension I wrote, Find and Transform, to do this. Use this keybinding:

{
  "key": "alt+y",                   // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "preCommands": "cursorHomeSelect",    // does the selection for you
    "replace": [
      "${",
              // log. = 4
      "let myVar = '${selectedText}'.substring(4);",
      "return `console.log (\"the value of ${myVar} is: \", ${myVar});`",
      "}$"
    ],
    "restrictFind": "line"
  }
}

You could change substring(4) if you just want to use log and not log..

log snippet with Find and Transform extension

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文