问题描述
在vscode(或我为此尝试过的大多数其他编辑器)中,当我有如下代码块时:
In vscode (or most other editors I tried for that matter) when I have a block of code like this:
function() {
if(test1) {
doThis();
andThenDoThat();
}
}
然后我尝试注释掉andThenDoThat()
行,例如通过按 + ,我将得到以下信息:
And I try to comment out the line andThenDoThat()
e.g. by pressing +, I will get this:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
我想得到的是这样的:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
换句话说,我希望注释保留代码的原始缩进,而是从行的开头开始,因为这不是通用的人类可读注释,而是代码,而且我认为它远不止于此.保留缩进后可读.
In other words, I want the comment to preserve the original indentation of the code, and start from the beginning of the line instead, because this is not a generic human-readable comment, it's code, and I think it's far more readable when the indentation is preserved.
这可能吗?也许有插件?
Is this possible? With a plug-in maybe?
推荐答案
我认为这可行,修改了
I think this works, modifying my answer from Make comments of VSCode start at column position 0
您需要多命令扩展名.
在您的设置中:
"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中:
In your 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等文件.
Same caveats as in the other linked answer, so read that. I made the above for js/php files only, obviously it wouldn't work for html/css/scss, etc. files with different comment markers than javascript.
+ + 可以删除注释(可以选择所需的任何键绑定). + 进行评论.
++ to remove comments (you can choose whichever keybindings you like). + to comment.
这篇关于注释掉行时,vscode保留缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!