[vscode注释掉行时保留缩进

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

在vscode(或我为此尝试过的大多数其他编辑器中,当我有这样的代码块时:

function() {
    if(test1) {
        doThis();
        andThenDoThat();
    }
}

[我尝试注释掉andThenDoThat()行,例如通过按Ctrl + /,我会得到这个:

function() {
    if(test1) {
        doThis();
        // andThenDoThat();
    }
}

我想得到的是这个:

function() {
    if(test1) {
        doThis();
//      andThenDoThat();
    }
}

换句话说,我希望注释保留代码的原始缩进,而是从行的开头开始,因为这不是通用的人类可读注释,而是代码,而且我认为它更具可读性保留缩进时。

这可能吗?也许有插件?

visual-studio-code comments indentation vscode-settings
1个回答
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 + /进行评论。


comments at col 0 with no indentation

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