barreiroleo / ltex_extra.nvim

Provides external LTeX file handling (off-spec lsp) and other functions.
GNU General Public License v3.0
148 stars 16 forks source link

ltex_extra fails to attach to server #21

Closed disidrosi closed 2 years ago

disidrosi commented 2 years ago

Describe the bug When opened, nvim displays five messages reporting "Error catching ltex client", followed by an analogous error message (see screenshots below). However, ltex_extra seems to work as expected on both Markdown and TeX files. The plugin is configured with the default:

require('lspconfig').ltex.setup {
    on_attach = on_attach,
    capabilities = capabilities,
        require('ltex_extra').setup{
            load_langs = { 'it', 'en-US' }, -- table <string> : languages for witch dictionaries will be loaded
            init_check = true, -- boolean : whether to load dictionaries on startup
            path = nil, -- string : path to store dictionaries. Relative path uses current working directory
            log_level = 'trace', -- string : 'none', 'trace', 'debug', 'info', 'warn', 'error', 'fatal'
        }
    ,
    settings = {
        ltex = {
            -- your settings.
        }
    }
}

For completeness, here is also the attach function that I'm using, which is taken from Kickstart.nvim (see link):

local on_attach = function(_, bufnr)

  local nmap = function(keys, func, desc)
    if desc then
      desc = 'LSP: ' .. desc
    end

    vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  end

  nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')

  nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  nmap('gi', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  nmap('gr', require('telescope.builtin').lsp_references)
  nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')

  -- See `:help K` for why this keymap
  nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')

  -- Lesser used LSP functionality
  nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  nmap('<leader>wl', function()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, '[W]orkspace [L]ist Folders')

  -- Create a command `:Format` local to the LSP buffer
  vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
    if vim.lsp.buf.format then
      vim.lsp.buf.format { async = true }
    else
      vim.lsp.buf.formatting()
    end
  end, { desc = 'Format current buffer with LSP' })
end

Screenshot 2022-11-22 at 10 44 05

Screenshot 2022-11-22 at 10 46 31

barreiroleo commented 2 years ago

Hello @disidrosi,

Please define a path for your ltex file and try again. You can use a relative path to store data in your working directory or an absolute path to store common data. Do you have the server running? You can check it with :LspInfo

disidrosi commented 2 years ago

Hi @barreiroleo, thanks for the reply.

I tried setting a path, and this indeed gets rid of the second error, but the five lines "Error catching ltex client" remain. The server is also running correctly, as shown by :LspInfo (see below). Diagnostics are displayed correctly on each buffer, and are updated also after resetting with :lua vim.diagnostic.reset().

Screenshot 2022-11-22 at 12 23 03

barreiroleo commented 2 years ago

Sorry, I read fast on your initial post. I notice your config doesn't follow the recommendation what is in the docs.

require("lspconfig").ltex.setup {
    capabilities = your_capabilities,
    on_attach = function(client, bufnr)
        -- your other on_attach functions.
        require("ltex_extra").setup{...}
    end,
    settings = {...}
}

You can see that idea is passing a callback function to execute when "on_attach" event is happening. In your snippet, you're calling to ltex_extra setup when the ltex specification is given to lspconfig, not when the server is attaching, so ltex is never gonna be up when ltex_extra is trying to catch it.

require('lspconfig').ltex.setup {
    on_attach = on_attach, -- Here goes the function
    capabilities = capabilities,
    require('ltex_extra').setup{ ... } , -- This is calling to ltex_extra when there isn't a server attached
    settings = {...}
}
disidrosi commented 2 years ago

Thank you very much! I should have read more carefully too, because indeed changing the placement of the callback function solved the problem.

barreiroleo commented 2 years ago

Excellent, I hope you find the plugin useful.