lukas-reineke / lsp-format.nvim

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

Enabling eslint #67

Closed MaximilianGaedig closed 1 year ago

MaximilianGaedig commented 1 year ago

Thank you very much for creating this plugin :)

I was configuring it today and I ran into some problems enabling eslint (and disabling tsserver) for formatting, but finally found a solution and wanted to share it as other people might run into the same problem. I had to add

  if client.name == 'tsserver' then
    -- Disable formatting for typescript 
    client.server_capabilities.documentFormattingProvider = false -- for lsp-format
  elseif client.name == 'eslint' then
    -- Enable formatting for eslint
    client.server_capabilities.documentFormattingProvider = true -- for lsp-format
  end

in my on_attach function, then eslint was enabled, tsserver had to be disabled because otherwise eslint ran first and it corrected its issues then tsserver formatted the file while ignoring my eslint rules. Such problems were documented already but this plugin required a bit of a different solution as the solution normally was to set document_formatting instead of documentFormattingProvider.

This seems a bit hacky, so if there is a better solution please tell me!

lukas-reineke commented 1 year ago

This is not how you should do it.

There are 2 options.

  1. To completely disable an LSP from formatting, don't call lsp-formats attach function for that LSP at all.

  2. To exclude an LSP for specific filetypes, use :help lsp-format-exclude

MaximilianGaedig commented 1 year ago

This is not how you should do it.

There are 2 options.

  1. To completely disable an LSP from formatting, don't call lsp-formats attach function for that LSP at all.
  2. To exclude an LSP for specific filetypes, use :help lsp-format-exclude

ah yeah true the exclusion should be done differently, but what about the eslint enabling? Is there a better solution?

lukas-reineke commented 1 year ago

but what about the eslint enabling

There is no concept of enabling in lsp-format. When you attach a server, and it is not explicitly excluded, it is enabled.

MaximilianGaedig commented 1 year ago

but what about the eslint enabling

There is no concept of enabling in lsp-format. When you attach a server, and it is not explicitly excluded, it is enabled.

the server has to have that "document/textFormatting" ability tho, and eslint has that but doesn't report it, so you need to override it

this is more of a eslint server problem but it has a workaround which doesn't work on lsp-formatter because it checks for document/textFormatting instead of just executing the command for a format

lukas-reineke commented 1 year ago

I understand. Yeah this is a problem with the eslint LSP server. I don't have any better suggestion than what you are already doing