Repository navigation
Description
This is part of #41882 and tracks callers of registerDocumentFormattingEditProvider and registerDocumentRangeFormattingEditProvider that don't dispose their registrations when being disabled.
For VSCode calling registerDocument[Range]?FormattingEditProvider adds a formatter, that for instance drives enablement of context menu actions, like "Format Selection" or "Format Document". Most formatters follow our recommendation of having a setting to enable/disable them. However, some formatters don't unregister when being disabled, returning a null/empty-edit instead. That's problematic, because it doesn't allow VSCode to update the UI correctly (e.g hiding the "Format Document" action) and it contributes to the problem of multiple formatters (#41882). A disabled, but registered formatter is still a formatter in VSCode terms, meaning VSCode will continue to ask it for formatting edits and VSCode will continue to have multiple formatter available for a language.
A correct formatter unregisters when being disabled, as shown in the sample below. The following list is composed of those formatters that conflict most often:
- esbenp.prettier-vscode: (requires restart) Formatter doesn't unregister itself when being disabled prettier/prettier-vscode#725
- hookyqr.beautify: ✅ no change needed
- vscode.typescript-language-features: ✅ no change needed
- octref.vetur: Add option to disable formatter registration vuejs/vetur#1121
- ms-vscode.cpptools: Duplicated formatter registrations vscode-cpptools#3285
- ms-vscode.go: Avoid registering more than 1 formatting provider at a time vscode-go#2446
- lonefy.vscode-js-css-html-formatter: Formatter must unregister when being disabled Lonefy/vscode-JS-CSS-HTML-formatter#91
- yzhang.vscode-markdown: Formatter must unregister when being disabled yzhang-gh/vscode-markdown#395
- remimarsal.prettier-now: Formatter must unregister when being disabled remimarsal/prettier-now-vscode#26
- xaver.clang-format: Formatter must unregister when being disabled xaverh/vscode-clang-format#81
- redhat.vscode-yaml: (requires restart) Formatter must unregister when being disabled redhat-developer/yaml-language-server#136
- michelemelluso.code-beautifier: Formatter must unregister when being disabled mike7515/code-beautifier#27
- vscode.json-language-features: ✅ no change needed
- vscode.html-language-features: ✅ no change needed
The snippet below check with vsocde.workspace.getConfiguration() if the formatter is enabled and re-checks whenever the setting has changes (see vscode.workspace.onDidChangeConfiguration). Listening to the config-change is important for a smooth formatter update, without a restart.
// document formatter
const formatter: vscode.DocumentFormattingEditProvider = {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
const firstLine = document.lineAt(0);
if (firstLine.text !== '42') {
return [vscode.TextEdit.insert(firstLine.range.start, '42\n')];
}
}
};
// have a function that adds/removes the formatter based
// on a configuration setting
let registration: vscode.Disposable | undefined;
function registerFormatterIfEnabled() {
const isEnabled = vscode.workspace.getConfiguration().get('fooLang.formatter.enabled', true);
if (isEnabled && !registration) {
// 👍good - only enabled formatter is added
registration = vscode.languages.registerDocumentFormattingEditProvider('fooLang', formatter);
} else if (!isEnabled && registration) {
// 👍good - disabled formatter is removed
registration.dispose();
registration = undefined;
}
}
// register at activate-time
registerFormatterIfEnabled();
// add/remove formatter when config changes
vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('fooLang.formatter.enabled')) {
registerFormatterIfEnabled();
}
});
Activity
jrieken commentedon Mar 12, 2019
Dirk Bäumer (@dbaeumer) can you add a sample how to unregister a formatter in LSP land?
octref commentedon Mar 12, 2019
Will do vuejs/vetur#1121 for March.
dbaeumer commentedon Mar 13, 2019
You need to register a formatter dynamically to be able to unregister it. It can not be registered via the return value from the initialize call. So instead of something like this:
you need to do something like this:
To unregister you call
registration.dispose();DanTup commentedon Mar 19, 2019
Johannes Rieken (@jrieken) There was a note here about providing the setting name to VS Code to do this:
#41882 (comment)
Is that still an option? It seems simpler than having to implement the code above for every extension. It also gets a bit complicated than the above if you register multiple document modes dynamically, and have document + onType formatters. And also, I think the above code isn't adding the subscription to
context.subscriptions(is this required? I've been doing it for everything I register, even though it seems like VS Code should be able to track them itself).multics commentedon Mar 20, 2019
Does it make sense that leaving the disabled formatter registered?
Is it possible that vscode unregisters the formatter when it is disabled automatically?
jrieken commentedon Mar 20, 2019
Not AI (@multics) Two times "no". A disabled formatter must unregister so that it doesn't show in the UX anymore (see #70314 (comment)). Also, it's not possible to unregister them for us because we don't know the setting an extension uses. That means the responsibility is with the formatter
DanTup commentedon Mar 20, 2019
Couldn't VS Code keep all registered formatters, but skip over them when formatting if their corresponding setting is disabled?
It doesn't matter to me now, I've already done my work - but it feels like it'd be a much simpler API than each extension having to do its own housekeeping.
jrieken commentedon Mar 21, 2019
And how would vscode know those settings and how interpret them?
DanTup commentedon Mar 21, 2019
Johannes Rieken (@jrieken) you posted a comment here:
#41882 (comment)
which sounded like it was the plan. We pass a string setting name to VS Code and it toggles the formatter based on that setting (which would be a boolean). I honestly thought that was the plan (and couldn't see any obvious reason why that was no longer the case), which is why I was surprised it wasn't so trivial when I came to do it.
jrieken commentedon Mar 21, 2019
Please read the whole thread: #41882 (comment)
DanTup commentedon Mar 21, 2019
Aha! Apologies, I thought I'd read the whole thread, but I may have missed some comments collapsed by GH. Makes sense now, thanks!
iansan5653 commentedon Mar 22, 2019
What happens if an extension with a registered formatter is literally disabled or uninstalled without restart?
octref commentedon Mar 22, 2019
I added
vetur.format.enable, will be released in 0.17.1.jrieken commentedon Mar 25, 2019
In that case the
deactivatefunction of that extension is being called. If the extension did everything right, e.g. manages its registrations correctly than the formatter is unregistered.octref commentedon Mar 28, 2019
Some of the reasons people didn't use dynamic registration are probably:
I created microsoft/vscode-docs#2522 for that.
However, if all formatters should have a boolean setting for enablement / disablement, do we still consider adding API to simplify the registration?
In LSP it could look like:
DanTup commentedon Jun 17, 2019
I was just trying to run some of my tests against LSP and discovered my setting to disable the formatter didn't work. Is there a way to handle this? Pine (@octref) mentions LSP above but I'm not sure I understand the snippet. If I understand correctly, we need to use a VS Code setting to tell the LSP client not to call formatting on the server (the server doesn't really need to know, it just won't get called for formatting)?
octref commentedon Jun 18, 2019
Danny Tuppeny (@DanTup) Above was just a suggestion how it could look in LSP.
You should:
--data-dirand includesettings.json. ExampleDanTup commentedon Jun 18, 2019
Pine (@octref) I badly described the issue - my test highlighted that this setting doesn't work when using LSP (so if I migrate users to LSP, the setting will seem broken), I wasn't specifically trying to fix the tests.
I'm not sure how best to make that setting actually affect the LSP server. I guess I could pass it in the initialisation options and then have the server pretend it doesn't support it but that seems weird (the client shouldn't tell the server what it supports, and it seems a fairly VS-Code-specific way to handle it).
It seems like maybe the LSP client should be able to handle this (for ex. if we can tell it to disable/re-enable the formatter?).
That said, I wonder if this is necessary now that VS Code can prompt for multiple formatters anyway. Maybe it's reasonable to just ditch the setting entirely for both LSP and non-LSP?
octref commentedon Jun 18, 2019
#70314 (comment)
vuejs/vetur@061e4cf
It's better to provide an option so users can use other features of your extension and not having to choose formatter each time.
DanTup commentedon Jun 18, 2019
Pine (@octref) aha! I'd overlooked that - I think that'll work fine - thanks! 👍