You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.2 KiB

  1. local null_ls = require("null-ls")
  2. local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false })
  3. local event = "BufWritePre" -- or "BufWritePost"
  4. local async = event == "BufWritePost"
  5. null_ls.setup({
  6. on_attach = function(client, bufnr)
  7. if client.supports_method("textDocument/formatting") then
  8. vim.keymap.set("n", "<Leader>ff", function()
  9. vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
  10. end, { buffer = bufnr, desc = "[lsp] format" })
  11. -- format on save
  12. vim.api.nvim_clear_autocmds({ buffer = bufnr, group = group })
  13. vim.api.nvim_create_autocmd(event, {
  14. buffer = bufnr,
  15. group = group,
  16. callback = function()
  17. vim.lsp.buf.format({ bufnr = bufnr, async = async })
  18. end,
  19. desc = "[lsp] format on save",
  20. })
  21. end
  22. if client.supports_method("textDocument/rangeFormatting") then
  23. vim.keymap.set("x", "<Leader>f", function()
  24. vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
  25. end, { buffer = bufnr, desc = "[lsp] format" })
  26. end
  27. end,
  28. })