antosha417 / nvim-lsp-file-operations

Neovim plugin that adds support for file operations using built-in LSP
Apache License 2.0
299 stars 17 forks source link

Document how to setup extended clientCapabilities #30

Closed mikehaertl closed 4 months ago

mikehaertl commented 4 months ago

I'm not sure if I missed anything, but for me the default setup is not enough, because nvim will not include the corresponding capabilities in the LSP initialize request.

So for nvim-lspconfig I had to add something like this to my config:

local lspconfig = require'lspconfig'
local util = lspconfig.util

-- Set global defaults for all servers
util.default_config = vim.tbl_extend(
  'force',
  util.default_config,
  {
    capabilities = vim.tbl_deep_extend(
      "force",
      vim.lsp.protocol.make_client_capabilities(),

      -- support added by lsp-file-operations
      {
        workspace = {
          fileOperations = {
            willRename = true,
            didRename = true,
            willCreate = true,
            didCreate = true,
            willDelete = true,
            didDelete = true,
          }
        }
      }
    )
  }
)

Shouldn't this be mentioned in the README?

antosha417 commented 4 months ago

It depends on language server that you are using. Most of language servers do not support all kinds of file operations. What exactly does not work for you? What language server are you using?

mikehaertl commented 4 months ago

It's eclipse.jdt.ls via nvim-jdtls.

To clarify: It does work - but only if I let neovim report the above client capabilities. Correct me if I'm wrong, but what I think how it works:

Without my above change the server response did not include workspace.fileOperations.willRename even though the server does support this operation.

mikehaertl commented 4 months ago

To clarify: lspconfig.util.default_config.capabilities configures the client capabilities, not the server capabilities.

antosha417 commented 4 months ago

I get it now. We add recipes for specific language servers to the wiki https://github.com/antosha417/nvim-lsp-file-operations/wiki

You can also open a pr and add it to readme if you want.

mikehaertl commented 4 months ago

So you did not have this problem? Then maybe LSP servers behave differently and only jdt.ls has this problem.

If you think it makes sense I can still provide a PR for the README later.

antosha417 commented 4 months ago

I mostly use metals language server for scala with https://github.com/scalameta/nvim-metals I make client capabilities like this and they don't contain any of the file operations

    local capabilities = vim.lsp.protocol.make_client_capabilities()

But still metals language server returns everything.

I think it is ok to add some specific instructions to the readme, it might save people time.

mikehaertl commented 4 months ago

What do you think about providing a utility method that returns the capabilities to add? That's how cmp-nvim-lsp does it.

It can be used to extend nvim's default capabilities as suggested here. It would look like this:

local lspconfig = require'lspconfig'
local cmp_nvim_lsp = require'cmp_nvim_lsp'
local lsp_file_operations = require'lsp-file-operations'
local util = lspconfig.util

-- Set global defaults for all servers
util.default_config = vim.tbl_extend(
  'force',
  util.default_config,
  {
    capabilities = vim.tbl_deep_extend(
      "force",
      vim.lsp.protocol.make_client_capabilities(),
      cmp_nvim_lsp.default_capabilities(),
      lsp_file_operations.default_capabilities(),
    )
  }
)