LunarVim / Neovim-from-scratch

📚 A Neovim config designed from scratch to be understandable
https://www.chrisatmachine.com/
GNU General Public License v3.0
5.43k stars 1.17k forks source link

nulls-ls and reformat after save #217

Open kamalkech opened 2 years ago

kamalkech commented 2 years ago

that my config of null-ls:

local null_ls_status_ok, null_ls = pcall(require, "null-ls")
if not null_ls_status_ok then
  return
end

-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
local formatting = null_ls.builtins.formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
local diagnostics = null_ls.builtins.diagnostics

local augroup = vim.api.nvim_create_augroup("LspFormatting", {})

null_ls.setup({
  debug = false,
  sources = {
    formatting.prettier.with({ extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" } }),
    formatting.black.with({ extra_args = { "--fast" } }),
    formatting.stylua,
    -- diagnostics.flake8
  },
  -- you can reuse a shared lspconfig on_attach callback here
  on_attach = function(client, bufnr)
    if client.supports_method("textDocument/formatting") then
      vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
      vim.api.nvim_create_autocmd("BufWritePre", {
        group = augroup,
        buffer = bufnr,
        callback = function()
          -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
          vim.lsp.buf.formatting_sync()
        end,
      })
    end
  end,
})

every time save file i should the correct server (capture bellow), question how can i save and nvim know the correct server, mean i don't need every time choose manually the server ??

image

Alexis12119 commented 2 years ago

Maybe you can try vim.lsp.buf.formatting_seq_sync() instead of vim.lsp.buf.formatting_sync().

nichtsam commented 2 years ago

I personally turned of sumneok_lua's document_formatting capabilities just like chris did with the tsserver and let stylua do all the work.

However, If you have multiple and every one does something, I think you can do as @Alexis12119 suggested.

Maybe you can try vim.lsp.buf.formatting_seq_sync() instead of vim.lsp.buf.formatting_sync().

you can :help vim.lsp.buf.formatting_seq_sync() to see what it does.