VonHeikemen / lsp-zero.nvim

A starting point to setup some lsp related features in neovim.
https://lsp-zero.netlify.app/v3.x/
MIT License
3.66k stars 94 forks source link

How to integrate conform.nvim with lsp-zero.nvim #369

Closed azdanov closed 5 months ago

azdanov commented 5 months ago

Hello. I'm wondering if there is a better way to use conform.nvim alongside lsp-zero.

At the moment I have this setup:

-- conform.nvim is added in another lazy.nvim plugin file.
{
"VonHeikemen/lsp-zero.nvim",
-- ...
config = function()
  local lsp_zero = require("lsp-zero")

  lsp_zero.on_attach(function(_, bufnr)

    vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
      require("conform").format({ async = true, lsp_fallback = true })
    end, { desc = "Format current buffer" })

    vim.keymap.set("n", "<leader>cf", "<cmd>Format<CR>", { buffer = bufnr, desc = "Format" })
    -- ...
  end)
end,
}
-- ...

Would be cool to hear any improvements.

Edit after closing: I'm using recipe from https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#lazy-loading-with-lazynvim

VonHeikemen commented 5 months ago

Looks good to me.

You don't even have to create the command on lsp-zero's configuration. You can create an autocommand in the conform.nvim file.

vim.api.nvim_create_autocmd("LspAttach", {
  callback = function(event)

    vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
      require("conform").format({ async = true, lsp_fallback = true })
    end, { desc = "Format current buffer" })

    vim.keymap.set("n", "<leader>cf", "<cmd>Format<CR>", { buffer = bufnr, desc = "Format" })

  end,
})
azdanov commented 5 months ago

Got it. Thank you.

I guess the issue can be closed now? 😄