在vscode(或我为此尝试过的大多数其他编辑器)中,当我有如下代码块时:
"react": "^16.12.0"
然后我尝试将function() {
if(test1) {
doThis();
andThenDoThat();
}
}
行注释掉,例如通过按 Ctrl + / ,我会得到:
andThenDoThat()
我想得到的是这样:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
换句话说,我希望注释保留代码的原始缩进,而是从行的开头开始,因为这不是通用的人类可读注释,而是代码,而且我认为它远不止于此。保留缩进后可读。
这可能吗?也许有插件?
答案 0 :(得分:1)
我认为这可行,修改了Make comments of VSCode start at column position 0的答案
您需要使用multi-command扩展名。
在您的设置中:
"multiCommand.commands": [
{
"command": "multiCommand.insertCommentColumn0",
"sequence": [
"cursorLineStart",
{
"command": "type",
"args": {
"text": "//"
}
},
"deleteRight",
"deleteRight"
]
},
{
"command": "multiCommand.AddCommentColumn0MultipleLines",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorLineStart",
{
"command": "type",
"args": {
"text": "//"
}
},
"deleteRight",
"deleteRight",
"removeSecondaryCursors"
]
},
{
"command": "multiCommand.removeCommentsSingleLine",
"sequence": [
"editor.action.removeCommentLine",
"cursorLineStart",
{
"command": "type",
"args": {
"text": " "
}
},
"removeSecondaryCursors"
]
},
{
"command": "multiCommand.removeCommentsMultipleLines",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorLineStart",
"editor.action.removeCommentLine",
{
"command": "type",
"args": {
"text": " "
}
},
"removeSecondaryCursors"
]
}
]
在您的keybindings.json中:
{ // disable ctrl+/ for js/php files only
"key": "ctrl+/",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
},
{ // call the macro multiCommand.insertCommentColumn0 when
// commenting a single line
"key": "ctrl+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertCommentColumn0" },
"when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
},
{ // call the macro multiCommand.AddCommentColumn0MultipleLines when
// commenting more than one line
"key": "ctrl+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.AddCommentColumn0MultipleLines" },
"when": "editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
},
{ // call the macro multiCommand.removeCommentsSingleLine when
// uncommenting a single line
"key": "ctrl+shift+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.removeCommentsSingleLine" },
"when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
},
{ // call the macro multiCommand.removeCommentsMultipleLines when
// uncommenting multiple lines
"key": "ctrl+shift+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.removeCommentsMultipleLines" },
"when": "editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
},
与其他链接的答案中的警告相同,因此请阅读。我仅针对js / php文件进行了上述操作,显然,该注释不适用于具有与javascript不同注释标记的html / css / scss等文件。
Ctrl + Shift + / 可以删除注释(您可以选择喜欢的任何键绑定)。 Ctrl + / 进行评论。