josean-dev / dev-environment-files

2.64k stars 740 forks source link

NVIM v0.7.2 `null-ls.lua:37: attempt to call field 'format' (a nil value)` and Choose lsp #1

Closed jokerclay closed 1 year ago

jokerclay commented 1 year ago

HI ! Josean , Thank you for your amazing video, I followed along your video , Here are some problems I run into, hope it can inspire you and help others out there with similar problems.

  1. null-ls.lua:37: attempt to call field 'format' (a nil value) this just because the version of NVIM v0.7 and NVIM v0.8 you can find it Here:How do I format files? they said :

null-ls formatters run when you call vim.lsp.buf.formatting() or vim.lsp.buf.formatting_sync(). If a source supports it, you can run range formatting by visually selecting part of the buffer and calling vim.lsp.buf.range_formatting(). On 0.8, you should use vim.lsp.buf.format (see the help file for usage instructions).

so, I change it as following, and it works fix1

  1. Choose a lsp when save a lua file after that , you may run into other problem as following fix

I don't really know how to fix it, so I add the following

if client.name == "sumneko_lua" then
    client.resolved_capabilities.document_formatting = false
    client.resolved_capabilities.document_range_formatting = false
end

Just disable sumneko_lua, instead of choose null-ls or sumneko_lua every time I find it Here : Avoiding LSP formatting conflicts and Here : Neovim Select a Language Prompt

Again, your video is amazing , thx !

josean-dev commented 1 year ago

Hey! I really appreciate your feedback, happy to hear you enjoyed the video! Thanks for pointing this out and for the detailed explanation of the issue you ran into with images for what you've encountered. What you've described would definitely fix the issue for previous versions of nvim, though you would have to disable "document_formatting" and "document_range_formatting" for all of the languages you have a null-ls formatter configured, not just "sumneko_lua" (i.e. the "tsserver" as well as I am using prettier with null-ls for formatting here too). This was my original approach, but I decided to make this config specifically for NVIM 0.8 or later and modified things according to the null-ls documentation.

You can find more info on where my decisions came from here: Formating on Save. Specifically, the "Sync Formatting" section and "Choosing a Client for Formatting".

I went with the 0.8 version of things and their suggestion for configuring null-ls so that it is the only one that gets formatting requests and you don't have to choose an lsp when saving. Using NVIM 0.8 is a requirement for it to work properly. Is there a particular reason why you are using an earlier version of Neovim?