使用 VSCode“切换行注释”命令时更改注释符号位置

2024-04-14

是否可以在 VSCode 中自定义注释符号(使用 Python 时为“#”)的位置?

例如,如果我的代码是:

def my_func():
    value = 1

我在第 2 行按 CMD-/,我得到:

def my_func():
   # value = 1

我更愿意得到:

def my_func():
#    value = 1

有没有办法修改默认行为?

VS代码:1.67.1
苹果系统:12.3.1


没有内置的方法可以做到这一点。您将需要延期。看https://stackoverflow.com/a/59448448/836330 https://stackoverflow.com/a/59448448/836330对于之前使用不同扩展名的答案。这是使用我同时制作的扩展的更好答案,。但存在如下限制。

使这个键绑定(在你的keybindings.json):

{
  "key": "alt+/",                    // unfortunately, this cannot be ctrl+/
  "command": "findInCurrentFile",
  "args": {

  {
    "key": "alt+r",
    "command": "findInCurrentFile",
    "args": {
      "preCommands": [
        "cursorEnd",
        "cursorHomeSelect",
        "cursorHomeSelect"
      ],

      "replace": "${LINE_COMMENT}${TM_CURRENT_LINE}",

      "restrictFind": "line"   // works on multiple lines, see the demo
    },
    "when": "editorLangId == python"   // if you want to limit it to a language
  },
}

You can use whatever keybinding you want, but not Ctrl+/ because then toggle off will not work.

Ctrl+/ will work to toggle off comments if you do not use it in the keybinding above to add comments.

注意:为了使其正常工作,您需要禁用以下设置(我已显示对特定语言 python 禁用了该设置):

"[python]": {
  "editor.comments.insertSpace": false
}

这会进入你的settings.json.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 VSCode“切换行注释”命令时更改注释符号位置 的相关文章

随机推荐