neovim / nvim-lspconfig

Quickstart configs for Nvim LSP
Apache License 2.0
10.72k stars 2.08k forks source link

Custom command name may not conform user command naming rules #3087

Open blmarket opened 7 months ago

blmarket commented 7 months ago

Description

TLDR

I have a use case to register commands which is not actually user commands. In short, I wanna execute lsp.start_client without calling nvim_create_user_command.

Minimal reproduction

note that I have my own LSP implementation, which is a part of the issue.

vim.cmd('packadd nvim-lspconfig')

local lspconfig = require('lspconfig')
local configs = require('lspconfig.configs')
local util = require("lspconfig.util")

-- My own lsp server for markdown
if not configs.blah then
  configs.blah = {
    default_config = {
      cmd = { 'blahblah' },
      filetypes = { 'markdown' },
      single_file_support = true,
      commands = {
        ['my_app/command1'] = { function(args, client_info)
        end },
        ['my_app/command2'] = { function(args, client_info)
        end },
      }
    },
  }
end

lspconfig.blah.setup({})

-- Codelens actions include `my_app/command1` which will be handled by custom commands
vim.api.nvim_create_autocmd('LspAttach', {
  callback = function(_)
    vim.api.nvim_create_autocmd({'TextChanged', 'InsertLeave'}, {
      callback = function(_)
        vim.lsp.codelens.refresh()
      end
    })
    vim.lsp.codelens.refresh()
  end
})

The problem

With 0.9.5 it works fine, but with 0.10-dev it fails as it tries to register my_app/command1 as user command and fail because of the command name. I don't know why 0.9.5 is working fine though...

Context

I need a client-side integration to implement codelens action properly. (e.g. I'd like to use telescope to show the result etc...), and relying on this neovim lsp client impl. So those commands are only to be run by codelens actions, not by user commands. I'm also fine if there's another way to add such custom command handlers without breaking neovim's user command name rules.

glepnir commented 7 months ago

use vim.lsp.start instead

blmarket commented 7 months ago

use vim.lsp.start instead

Are there no LSP providing codelens with custom commands? Wondering this is something we can consider support in the future or never will be.

And other stuff I'm missing:

glepnir commented 7 months ago

actually lspconfig just wrapper of vim.lsp.start . i don't know lazy nvim. if you want create a server config with some custom you can just use vim.lsp.start.

blmarket commented 7 months ago

Yeah I do need that wrapper part - at least autocmd to launch lsp. Just asking whether it's possible to disable nvim-lspconfig doing something with commands config.

blmarket commented 7 months ago

Let me put it in this way - would you consider if I create a PR to skip registration of commands as user commands? like setting commands_created but maybe with different config name.

glepnir commented 7 months ago

could you explain what's your custom commands do in codelens ?

blmarket commented 7 months ago

could you explain what's your custom commands do in codelens ?

Not something standard LSP functionality. Here they are:

  1. LSP provides a sorted reference list with score, and client(nvim) show it for navigation.
  2. LSP provides a list of keywords, client use telescope to let user choose, and do keyword based search for the sections.

Just for reference, here is the real configuration:

commands = {
  ['lsp_md/searchSimilar'] = function(args, client_info)
    local client = vim.lsp.get_active_clients()[client_info.client_id]
    client.request('workspace/executeCommand',
      args,
      function(_, resp, ctx, _)
        local items = {}
        for _, it in ipairs(resp) do
          table.insert(items, {
            filename = vim.uri_to_fname(it.location.uri),
            lnum = it.location.range.start.line + 1,
            col = it.location.range.start.character + 1,
            text = string.format("%.3f: %s", it.score, it.title),
            user_data = it,
          })
        end
        vim.fn.setloclist(client_info.bufnr, {}, ' ', { title = "title", items = items, context = ctx })
        vim.api.nvim_command("lopen")
      end
    )
  end,
  ['lsp_md/keywords'] = function(args, client_info)
    local client = vim.lsp.get_active_clients()[client_info.client_id]
    client.request('workspace/executeCommand',
      args,
      function(_, resp, _, _)
        local items = {}
        for _, it in ipairs(resp) do
          table.insert(items, it) -- it has text and score
        end

        local opts = {}
        pickers.new(opts, {
          prompt_title = "Keywords",
          finder = finders.new_table {
            results = items,
            entry_maker = function(entry)
              return {
                value = entry,
                display = entry.text,
                ordinal = entry.score,
              }
            end,
          },
          sorter = conf.generic_sorter(opts),
          attach_mappings = function(prompt_bufnr, _)
            actions.select_default:replace(function()
              actions.close(prompt_bufnr)
              local selected = action_state.get_selected_entry()
              SearchByKeyword(selected.value.text)
            end)
            return true
          end,
        }):find()
      end
    )
  end
}
blmarket commented 7 months ago

What I just realized is my commands is actually for https://neovim.io/doc/user/lsp.html#vim.lsp.ClientConfig struct, where nvim-lspconfig has its own definition of commands which is described at https://github.com/neovim/nvim-lspconfig/blob/master/doc/lspconfig.txt#L281

And seems I need a way to pass commands table as-is to the vim.lsp.start API. I'm wondering this is something we can create a PR to add, or no plan to fix.

blmarket commented 5 months ago

Hi, Any updates on this? as 0.10 is officially released, I'd like to fix it if possible.

asmodeus812 commented 3 months ago

Related possibly - https://github.com/neovim/nvim-lspconfig/issues/2632