Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use yaml formatter with Neovim builtin LSP client? #486

Closed
dhruvmanila opened this issue Jun 11, 2021 · 14 comments
Closed

How to use yaml formatter with Neovim builtin LSP client? #486

dhruvmanila opened this issue Jun 11, 2021 · 14 comments

Comments

@dhruvmanila
Copy link

The codebase contains a yamlFormatter although the server capabilities says otherwise:

        documentFormattingProvider: false,

The formatter runs perfectly well in VSCode. I installed the server using npm and running the server with the default settings.

@dhruvmanila
Copy link
Author

So, I did a bit of experimentation where I forced Neovim to send the formatting request by specifying that the server actually supports it using client.resolved_capabilities.document_formatting = true and the server responded appropriately.

My only question is why is the server specifying documentFormattingProvider: false while it actually can do the formatting?

@evidolob
Copy link
Collaborator

That true, we return false there https://github.com/redhat-developer/yaml-language-server/blob/main/src/yamlServerInit.ts#L99 for documentFormattingProvider.
@JPinkney Do you remember why?

@dhruvmanila
Copy link
Author

I tracked it down to this PR (#179) which fixes this issue (redhat-developer/vscode-yaml#220) although I'm not sure I understand the reasoning behind this.

@JPinkney
Copy link
Contributor

IIRC it was done that way because VSCode needed formatters to be registered dynamically so that you could dispose of them after they weren't in use. I'm not sure if that's still neccessary though. You might be able to set documentFormattingProvider to true now

@dhruvmanila
Copy link
Author

There's already an open PR for this: #316 although it's outdated and needs rebasing.

@dhruvmanila
Copy link
Author

For reference, the vscode-json-languageserver provides the initialization option provideFormatter which will then determine whether to provide the formatter on initialization or not.

Source: https://github.com/microsoft/vscode/blob/41c86e5bb8c29f801d6036b738c0c6b548be58e5/extensions/json-language-features/server/src/jsonServer.ts#L140-L141

From the README (server#initialization-options):

provideFormatter: boolean | undefined. If defined, the value defines whether the server provides the documentRangeFormattingProvider capability on initialization. If undefined, the setting json.format.enable is used to determine whether formatting is provided. The formatter will then be registered through dynamic registration. If the client does not support dynamic registration, no formatter will be available.

@ttys3
Copy link

ttys3 commented Oct 16, 2021

I encountered the same problem, the formatter does not work (or can not find a way to enable it)

@lewissetter
Copy link

lewissetter commented Feb 13, 2022

Why was #316 closed? I think #657 is the same problem we're talking about here. From what @dhruvmanila mentioned, it looks like documentFormattingProvider can be set to true since leaving provideFormatter undefined will cause the formatter to be registered dynamically.

@meatballs
Copy link

So, I did a bit of experimentation where I forced Neovim to send the formatting request by specifying that the server actually supports it using client.resolved_capabilities.document_formatting = true and the server responded appropriately.

Would you mind sharing how you did that?

@meatballs
Copy link

Never mind. I got it working myself by adding a line to my on_attach function:

local on_attach = function(client, bufnr)
  client.resolved_capabilities.document_formatting = true

@dhruvmanila
Copy link
Author

I think you should directly use prettier for YAML files as the language server uses that internally.

const formatted = prettier.format(text, prettierOptions);

But, if you want to use the language server, then I suggest to guard the line so that it only affects the YAML language server:

local on_attach = function(client, buffer)
  if client.name == "yamlls" then
    client.resolved_capabilities.document_formatting = true
  end
end

@seanturner026
Copy link

I think you should directly use prettier for YAML files as the language server uses that internally.

How would one implement this with neovim?

@dhruvmanila
Copy link
Author

How would one implement this with neovim?

Using a formatter plugin like https://github.com/stevearc/conform.nvim. You can find a list here: https://github.com/rockerBOO/awesome-neovim#formatting

@seanturner026
Copy link

Awesome! I was able to get it working super easily (the method above in this PR wasn't working).

seanturner026/kickstart.nvim@507223e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants