Visual Studio Code extension to automatically continue inline comments.
Visual Studio Code doesn't support automatic continuation of inline comments out of the box. This extension adds support for it in various languages (see default config).
Inspired by Auto Comment Next Line (GitHub) which wasn't working very well for me.
Here's the default config as seen in package.json
. Feel free
to tweak as you please!
{
"continueInlineComments.list": {
"type": "array",
"default": [
{
"comment": "//",
"languages": [
"php",
"javascript",
"typescript",
"jsonc",
"scss",
"sass",
"json",
"vue",
"markdown",
"javascriptreact",
"typescriptreact"
]
},
{
"comment": "#",
"languages": [
"python",
"ruby",
"shellscript",
"makefile"
]
}
]
}
}
See the full code in src/extension.ts
.
When a new document is opened or when the language of a document changes
(vscode.workspace.onDidOpenTextDocument
), we check if the language is
supported by our config. If so, we use
vscode.languages.setLanguageConfiguration
to extend the onEnterRules
to add
completion for the inline comments (see hacks below).
On load, we also iterate over vscode.window.visibleTextEditors
to apply the
onEnterRules
because otherwise the editors already opened when the extension
is loaded would not trigger onDidOpenTextDocument
.
-
Extending
onEnterRules
is pretty hacky to do. Becausevscode.languages.setLanguageConfiguration
overwrites the properties we give to it, it means that by setting our customonEnterRules
entry, we're effectively removing all the otheronEnterRules
defined by the original language definition. Not good.To avoid that, we need to get the existing language configuration to add our custom rule on top of the existing ones.
The problem is that there's no way to get the existing language configuration from the extensions API (see and see), so we have to resort to a dirty hack where we manually identify the language configuration files of the installed extensions, load that file manually with jsonc-parser, and translate the JSON representation of
onEnterRules
(which is different from the JavaScript representation expected by the extensions API) so that we can use it invscode.languages.setLanguageConfiguration
.And while it's annoying AF and a pretty dirty hack in my opinion, it does work pretty fucking well.
-
jsonc-parser is not bundled with esbuild because of this issue.
-
In
package.json
, therepository
needs to be set togithub:valeriangalliat/vscode-continue-inline-comments
instead of justvaleriangalliat/vscode-continue-inline-comments
(which are semantically equivalent) becausevsce
doesn't support the latter syntax. -
The
UNLICENSE
file needs to be symlinked asLICENSE
forvsce
to be happy.
npm install
npm run watch
npm install -g vsce
vsce package
vsce publish