lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

feat: add force_attach option to attach even though formatting unsupported #87

Closed GaetanLepage closed 2 months ago

GaetanLepage commented 2 months ago

Some language servers rely on dynamic registration for the textDocument/formatting capability. Although the neovim client.supports_method (used here) is supposed to account for that, some language servers are still not reporting to support this capability.

To account for this, this PR introduces a new parameter force_attach that lets the user skip the if not client.supports_method(method) check and attach lsp-format nonetheless. This has been working for me for tinymist. Formatting works great now.

Myriad-Dreamin commented 2 months ago

Instead of skipping the check, it should subscribe to the changes IMO, to react to dynamic registration, like vscode:

_languageFeaturesService.onTypeFormattingEditProvider.onDidChange(this._update, this);

https://github.com/microsoft/vscode/blob/2b277fcb563fe182444b8c2cf3ac41f95daa4b74/src/vs/editor/contrib/format/browser/formatActions.ts#L45

lukas-reineke commented 2 months ago

Yeah I agree, this should get handled automatically. Having to set a force option for this is not nice user experience.

GaetanLepage commented 2 months ago

I agree as well that an an automatic behavior would be better. Currently, there is no easy way to listen for this kind of events in neovim. It has been discussed here to add an LspCapability event that would let us register an appropriate autocmd. However, this has not been addressed yet.

I do feel like a force_attach option is the only way in the meantime. What do you think ?

lukas-reineke commented 2 months ago

We don't need to listen for an event. We can just attach all buffers, and check if the client supports formatting each time formatting is triggered. client.supports_method supports dynamic registration, this shouldn't be too complicated.

GaetanLepage commented 2 months ago

We don't need to listen for an event. We can just attach all buffers, and check if the client supports formatting each time formatting is triggered. client.supports_method supports dynamic registration, this shouldn't be too complicated.

Right indeed ! I had not understood this at first. I made a separate PR: #88.