Estus Flask
Estus Flask

Reputation: 222494

Prevent trailing and leading spaces in VS Code multiline comments

When multiline comment is added with Shift+Ctrl+A, trailing space is added at /* line, this may cause linter problems:

[multiline comment

I'm comfortable with no-trailing-spaces linter rule regarding comments and would prefer to not change it because of VS Code quirks.

Leading space before */ doesn't cause linter problems but I would like to get rid of it because it looks displaced and I don't use intermediate asterisks in multiline comments like:

/*
 * multiline
 * comment
 */

Can a way spaces are added to multiline comments be changed in Visual Studio Code?

Upvotes: 2

Views: 5551

Answers (2)

user1945782
user1945782

Reputation:

Just to add to the answer above:

I was having trouble with the /**/ completion in VS Code. As soon as the second * was typed, VS Code would add a */ to the end - extremely annoying, as we have set formatting for comments, and using the god awful:

/*
 * Some comment
 */

...format is abhorrent to me.

To combat this, navigate to the %ProgramFiles%\Microsoft VS Code\resources\app\extensions\javascript folder. Make a backup of the javascript-language-configuration.json file, then edit it. In the autoClosingPairs section, change...

    "autoClosingPairs": [
        ...
        { "open": "/**", "close": " */", "notIn": ["string"] }
    ],

...to...

    "autoClosingPairs": [
        ...
        { "open": "/*", "close": "*/", "notIn": ["string"] }
    ],

Upvotes: 3

Mark
Mark

Reputation: 181080

EDIT for v1.42 (setting is "Comments: Insert Space") :

Apparently coming to v1.42 is this setting editor.insertSpaceAfterComment which seems to solve your problem in the block comment case. But if disabled no spaces will be inserted after line // comment notation, so //comment starts immediately instead of // comment starts after space. This may or may not be acceptable to you.

enter image description here

See https://github.com/microsoft/vscode/pull/41747


If you have

"editor.trimAutoWhitespace": true

when you save the file it will remove that trailing whitespace. Alternatively, using the command editor.action.trimTrailingWhitespace will also remove the trailing spaces in the file Ctrl-K Ctrl-X.

Modifying the built-in snippets is tricky since they can be overridden upon updates.

You could make a macro that deletes the space in one go. I presume you meant Shift-Alt-A: that is the command for toggling block comments on my vscode. You said Shift+Ctrl+A in your question which is unbound for me.

Using the extension multiCommand: (in your settings.json)

{
  "command": "multiCommand.blockComment",

  "sequence": [
     "editor.action.blockComment",
     "editor.action.trimTrailingWhitespace",
     "cancelSelection",
     "deleteRight"
   ]
},

Those last two commands get rid of the leading space before the */ as you requested.

In your keybindings.json:

{
  "key": "shift+alt+a",
  "command": "-editor.action.blockComment",
  "when": "editorTextFocus && !editorReadonly"
},
{
  "key": "shift+alt+a",
  "command": "multiCommand.blockComment",
},

And then invoke with Shift-Alt-A, toggling off still works too.

Demo of macro running.

[The gif goes a little nuts on the entered keystrokes, it Is only Shift-Alt-A.]

Upvotes: 6

Related Questions