Visual Studio Code extension to automatically continue inline comments.
Overview
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.
Configuration
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"
]
}
]
}
}
How it works
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
.
Hacks
Extending onEnterRules
is pretty hacky to do. Because
vscode.languages.setLanguageConfiguration
overwrites the properties we
give to it, it means that by setting our custom onEnterRules
entry, we're
effectively removing all the other onEnterRules
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 in vscode.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
, the repository
needs to be set to
github:valeriangalliat/vscode-continue-inline-comments
instead of
just valeriangalliat/vscode-continue-inline-comments
(which are
semantically equivalent) because vsce
doesn't support the latter
syntax.
The UNLICENSE
file needs to be symlinked as LICENSE
for vsce
to
be happy.
Development
npm install
npm run watch
Publishing
See publishing extensions.
npm install -g vsce
vsce package
vsce publish