VSCode 可以逐行进行块注释吗?
Can VSCode do block-comments line-by-line?
这是我在 VS Code 中使用 ctrl + /
注释 HTML 代码的示例:
<!-- </label>
<label>Confirm Your Email:
<!-- <input type="email" name="emailConfirm" required>
<!-- </label>
<label> -->
<input type="checkbox" name="termsAgree" required/>
I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
</label> -->
- 选择了整个代码块 &
ctrl+/
对整个代码进行评论
- 选择了一段代码 &
ctrl+/
un注释一行,但它只是添加了一个新注释,从而破坏了较大的注释
- 再次执行#2,现在这是一场噩梦
我在 ctrl + /
CSS 代码块时遇到同样的问题。在 PHP 代码中没有问题,因为它使用 //
逐行注释
我怎样才能改变这种行为?
我在想:
- 一个 "smart" 可以使用块评论的评论者 & "know" 无论它当前是否在评论中
- 一个愚蠢的评论者 comments/uncomments 每一行都像这样:
<!-- </label> -->
<!-- <label>Confirm Your Email: -->
<!-- <input type="email" name="emailConfirm" required> -->
</label>
<label>
<!-- <input type="checkbox" name="termsAgree" required/> -->
<!-- I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
<!-- </label> -->
请忽略html无效的事实。我在这个例子中使用的只是一个随机块。
我刚刚做了一个扩展,Toggle Line Comments,它使用你的“愚蠢的方法”来单独切换每一行。
以下是您的代码演示:
原回答:
是的,vscode 可以按行进行块注释,但该功能不是内置的。您将不得不使用宏命令,在这里我使用 multi-command,将行分开并为每个行应用块注释。
将此放入您的 settings.json
:
"multiCommand.commands": [
{
"command": "multiCommand.blockHTMLCommentByLine",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHomeSelect",
"editor.action.blockComment",
"cancelSelection",
]
}
]
它将您的 selection 分成单独的行,然后在每行上切换块注释。
添加键绑定以触发该宏 - 重载 ctrl+/ 键绑定(将其放入您的 keybindings.json
):
{
"key": "ctrl+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.blockHTMLCommentByLine" },
// "when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\.(html|css|scss)/"
"when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\.html/"
},
我将其限制为 .html
个文件,但您可以看到如何在另一个 when
子句中包含其他扩展名。
演示:
如果要对整行进行注释,则必须 select 完整的行。
这是我在 VS Code 中使用 ctrl + /
注释 HTML 代码的示例:
<!-- </label>
<label>Confirm Your Email:
<!-- <input type="email" name="emailConfirm" required>
<!-- </label>
<label> -->
<input type="checkbox" name="termsAgree" required/>
I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
</label> -->
- 选择了整个代码块 &
ctrl+/
对整个代码进行评论 - 选择了一段代码 &
ctrl+/
un注释一行,但它只是添加了一个新注释,从而破坏了较大的注释 - 再次执行#2,现在这是一场噩梦
我在 ctrl + /
CSS 代码块时遇到同样的问题。在 PHP 代码中没有问题,因为它使用 //
逐行注释
我怎样才能改变这种行为?
我在想:
- 一个 "smart" 可以使用块评论的评论者 & "know" 无论它当前是否在评论中
- 一个愚蠢的评论者 comments/uncomments 每一行都像这样:
<!-- </label> -->
<!-- <label>Confirm Your Email: -->
<!-- <input type="email" name="emailConfirm" required> -->
</label>
<label>
<!-- <input type="checkbox" name="termsAgree" required/> -->
<!-- I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
<!-- </label> -->
请忽略html无效的事实。我在这个例子中使用的只是一个随机块。
我刚刚做了一个扩展,Toggle Line Comments,它使用你的“愚蠢的方法”来单独切换每一行。
以下是您的代码演示:
原回答:
是的,vscode 可以按行进行块注释,但该功能不是内置的。您将不得不使用宏命令,在这里我使用 multi-command,将行分开并为每个行应用块注释。
将此放入您的 settings.json
:
"multiCommand.commands": [
{
"command": "multiCommand.blockHTMLCommentByLine",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHomeSelect",
"editor.action.blockComment",
"cancelSelection",
]
}
]
它将您的 selection 分成单独的行,然后在每行上切换块注释。
添加键绑定以触发该宏 - 重载 ctrl+/ 键绑定(将其放入您的 keybindings.json
):
{
"key": "ctrl+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.blockHTMLCommentByLine" },
// "when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\.(html|css|scss)/"
"when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\.html/"
},
我将其限制为 .html
个文件,但您可以看到如何在另一个 when
子句中包含其他扩展名。
演示:
如果要对整行进行注释,则必须 select 完整的行。