【问题标题】:vscode preserve indentation when commenting out lines注释掉行时 vscode 保留缩进
【发布时间】:2020-06-25 15:01:19
【问题描述】:

当我有这样的代码块时,在 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】:

    我认为这可行,将我的答案从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 文件制作了上述内容,显然它不适用于 html/css/scss 等具有与 javascript 不同的注释标记的文件。

    Ctrl+Shift+/ 删除 cmets(您可以选择任何您喜欢的键绑定)。 Ctrl+/ 进行评论。


    【讨论】:

    • 谢谢,我试试! :)
    • 确实如此,再次感谢!但是我不得不修改你的键绑定中的正则表达式以包含我现在主要使用的 TypeScript:"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(ts|js|php)$/"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2020-09-22
    • 2019-04-18
    相关资源
    最近更新 更多