VS代码:选择或删除未选择文本的块注释的快捷方式

问题描述 投票:0回答:1

我知道有一个注释和取消注释代码块的快捷方式(SHIFT + ALT + A),但是有一种方法可以快速选择(甚至删除而不选择)块注释,而无需使用鼠标或键盘来选择并按Delete / Backspace按钮?例如:

/* 
This is a large block of code with at least 50 lines of code!
   :
   :
*/

是否存在键盘快捷键,我可以在其中放置光标块注释中的任何位置,只需单击几次即可将其删除?谢谢!

visual-studio-code vscode-settings
1个回答
0
投票

您可以设置宏来轻松完成此操作。

首先,使用出色的Select By扩展名选择块注释标记/**/之间并包括其的文本。将他设置为您的设置:

"selectby.regexes": {

  "regex1": {
    "backward": "\/\\*",
    "forward": "\\*\/",
    "forwardInclude": true,
    "backwardInclude": true,
    "showSelection": true
  }
},

您可以将其与诸如以下的键绑定一起使用:

{
  "key": "alt+s",                // whatever keybinding you wish
  "command": "selectby.regex1",  // you may warning squiggles under the command but it will still work
  "when": "editorTextFocus",
},

您可以在此处停止并使用键盘绑定选择文本,然后按Shift + Alt + A来关闭块注释。

或者您可以将selectby.regex1添加到宏中,然后一步一步地进行选择和切换。在这里,使用宏扩展名multi-command以及上面的selectby.regexes设置将其设置为您的设置:

"multiCommand.commands": [

  {
    "command": "multiCommand.BlockCommentOff",
    "sequence": [
      "selectby.regex1",            // run the selectby command defined above
      "editor.action.blockComment"  // toggle off the block comment
    ]
  }
]

然后是用于触发该宏的键绑定(在您的keybindings.json中:

{
  "key": "shift+alt+A",    // trigger the macro with whatever keybinding if you wish
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.BlockCommentOff" },
  "when": "editorTextFocus && !editorHasSelection"
},

这里我用Shift + Alt + A触发宏。我使用了when子句!editorHasSelection,因为如果您有一个选择,也许您只想阻止该选择(在另一个阻止注释中!)。

演示:(1)仅是第一种方法,其中selectby选择文本并手动将其关闭,然后(2)使用宏版本一步完成。

block toggle from within the block

© www.soinside.com 2019 - 2024. All rights reserved.