VS代码段:如何使用变量连续两次变换

发布于 2025-01-29 12:14:36 字数 362 浏览 3 评论 0 原文

请参阅以下片段:

    "srcPath":{
    "prefix": "getSrcPath",
    "body": [
        "$TM_FILEPATH",
        "${1:${TM_FILEPATH/(.*)src.(.*)/${2}/i}}",
        "${TM_FILEPATH/[\\\\]/./g}"
    ]
},

第1-3行的输出是:

D:\root\src\view\test.lua
view\test.lua
D:.root.src.view.test.lua

如何获得诸如'view/test.lua'之类的输出?

See the following snippet:

    "srcPath":{
    "prefix": "getSrcPath",
    "body": [
        "$TM_FILEPATH",
        "${1:${TM_FILEPATH/(.*)src.(.*)/${2}/i}}",
        "${TM_FILEPATH/[\\\\]/./g}"
    ]
},

The output of lines 1-3 is :

D:\root\src\view\test.lua
view\test.lua
D:.root.src.view.test.lua

How can I get output like 'view/test.lua'?

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

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

发布评论

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

评论(2

九命猫 2025-02-05 12:14:36

尝试此片段:

"srcPath":{
  "prefix": "getSrcPath",
  "body": [
      "$TM_FILEPATH",
      "${TM_FILEPATH/.*src.|(\\\\)/${1:+/}/g}",
      "${TM_FILEPATH/[\\\\]/\\//g}"
  ]
}

“在路径上的变换”

。*src。|(\\\\)将与所有内容匹配并包括 ... src \ 路径信息。我们不会将其保存在捕获组中,因为我们没有在转换的替换部分中使用它。

(\\\\)在路径的其余部分中匹配任何 \ - 需要 g 标志以使它们全部获取。

替换: $ {1:+/} ,这意味着如果有一个捕获组1 。代码>/。请注意,我们在 src \ 之后不匹配其余路径,仅可能跟随它的 \ 。因此,不匹配其他路径零件只会允许它们保留在结果中。


您在此方面接近了:

“ $ {tm_filepath/[\\\\\\]/\\ // g}” 只需用 \\\\ > \\/。

Try this snippet:

"srcPath":{
  "prefix": "getSrcPath",
  "body": [
      "$TM_FILEPATH",
      "${TM_FILEPATH/.*src.|(\\\\)/${1:+/}/g}",
      "${TM_FILEPATH/[\\\\]/\\//g}"
  ]
}

transform on path snippet

.*src.|(\\\\) will match everything up to and including the ...src\ path information. We don't save it in a capture group because we aren't using it in the replacement part of the transform.

The (\\\\) matches any \ in the rest of the path - need the g flag to get them all.

Replace: ${1:+/} which means if there is a capture group 1 in .*src.|(\\\\) then replace it with a /. Note we don't match the rest of the path after src\ only the \'s that might follow it. So, not matching those other path parts just allows them to remain in the result.


You were close on this one:

"${TM_FILEPATH/[\\\\]/\\//g}" just replace any \\\\ with \\/.

带刺的爱情 2025-02-05 12:14:36

使用扩展 /a>您可以插入包含变量和多个发现操作的“摘要”。

使用键绑定:

  {
    "key": "ctrl+alt+f",  // or any other combo
    "command": "templates.pasteTemplate",
    "args": {
      "text": [
        "${relativeFile#find=.*?src/(.*)#replace=$1#find=[\\\\/]#flags=g#replace=.#}"
      ]
    }
  }

目前只有键绑定或通过 Multi Command (或类似)。将添加一个问题,以使前缀成为可能。

另外,一些标准变量丢失了。

With the extension File Templates you can insert a "snippet" that contains a variable and multiple find-replace operations.

With a key binding:

  {
    "key": "ctrl+alt+f",  // or any other combo
    "command": "templates.pasteTemplate",
    "args": {
      "text": [
        "${relativeFile#find=.*?src/(.*)#replace=$1#find=[\\\\/]#flags=g#replace=.#}"
      ]
    }
  }

At the moment only possible with a key binding or via multi command (or similar). Will add an issue to also make it possible by prefix.

Also some of the standard variables are missing.

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