vscode 注释行时保留缩进
vscode preserve indentation when commenting out lines
在 vscode(或我为此尝试过的大多数其他编辑器)中,当我有这样一段代码时:
function() {
if(test1) {
doThis();
andThenDoThat();
}
}
然后我尝试注释掉 andThenDoThat()
这一行,例如按 Ctrl+/,我会得到这个:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
我想得到的是:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
换句话说,我希望注释保留代码的原始缩进,而是从行首开始,因为这不是通用的人类可读注释,它是代码,我认为它是保留缩进后可读性更高。
这可能吗?也许有插件?
我认为这行得通,修改了我从
得到的答案
您需要 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 文件制作了上面的内容,显然它不适用于 html/css/scss 等具有与 javascript 不同注释标记的文件。
Ctrl+Shift+/ 删除评论(您可以选择您喜欢的任何键绑定像)。 Ctrl+/评论。
在 vscode(或我为此尝试过的大多数其他编辑器)中,当我有这样一段代码时:
function() {
if(test1) {
doThis();
andThenDoThat();
}
}
然后我尝试注释掉 andThenDoThat()
这一行,例如按 Ctrl+/,我会得到这个:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
我想得到的是:
function() {
if(test1) {
doThis();
// andThenDoThat();
}
}
换句话说,我希望注释保留代码的原始缩进,而是从行首开始,因为这不是通用的人类可读注释,它是代码,我认为它是保留缩进后可读性更高。
这可能吗?也许有插件?
我认为这行得通,修改了我从
您需要 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 文件制作了上面的内容,显然它不适用于 html/css/scss 等具有与 javascript 不同注释标记的文件。
Ctrl+Shift+/ 删除评论(您可以选择您喜欢的任何键绑定像)。 Ctrl+/评论。