lukas-reineke / lsp-format.nvim

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

bugfix: handle error table #24

Closed jose-elias-alvarez closed 2 years ago

jose-elias-alvarez commented 2 years ago

Thanks for the plugin! It's nice to have an out-of-the-box async formatting solution. While testing it out, I ran into the following error:

Error executing vim.schedule lua callback: ...ack/packer/start/lsp-format.nvim/lua/lsp-format/init.lua:18:
 Expected lua string
stack traceback:
        [C]: in function 'nvim_err_write'
        ...ack/packer/start/lsp-format.nvim/lua/lsp-format/init.lua:18: in function 'handler'
        /Users/jose/.local/share/nvim/runtime/lua/vim/lsp.lua:1025: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

It seems like at least some servers (ESLint, for example) send errors as tables, which aren't currently handled. This PR adds handling for the table case.

For reference, the specification says that document formatting requests errors have the following structure:

  • error: code and message set in case an exception happens during the formatting request.

Neovim's built-in handlers only handle tables, so it may be possible to drop string handling altogether.

lukas-reineke commented 2 years ago

Thank you

I just assumed it would be a string, it looks like it is always a table. Should be safe to remove the check for string.

It might be nice to format the message the same as the build in handler does.

local client = vim.lsp.get_client_by_id(ctx.client_id)
local client_name = client and client.name or string.format("client_id=%d", ctx.client_id)

vim.api.nvim_err_write(client_name .. ': ' .. tostring(err.code) .. ': ' .. err.message)

Also, can you rebase the PR against develop please.

jose-elias-alvarez commented 2 years ago

Closing in favor of #27.