vscode сохраняет отступ при комментировании строк - PullRequest
0 голосов
/ 13 марта 2020

В vscode (или в большинстве других редакторов, которые я пробовал в этом отношении), когда у меня есть такой блок кода:

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

И я пытаюсь закомментировать строку andThenDoThat(), например, нажав Ctrl + / , я получу это:

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

Что я хотел бы получить это:

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

Другими словами Я хочу, чтобы комментарий сохранил исходный отступ кода и вместо этого начинался с начала строки, потому что это не общий c понятный человеку комментарий, это код, и я думаю, что он гораздо более читабелен, когда отступ сохраняется.

Возможно ли это? Может быть с плагином?

1 Ответ

1 голос
/ 14 марта 2020

Я думаю, что это работает, модифицируя мой ответ от Комментируйте VSCode, начиная с позиции столбца 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"
      ]
    }
  ]

В ваших сочетаниях клавиш. 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 / s css, et c. файлы с маркерами комментариев, отличными от javascript.

Ctrl + Shift + / для удаления комментариев (вы можете выбрать любую комбинацию клавиш, которая вам нравится). Ctrl + / для комментариев.


comments at col 0 with no indentation

...