neovim: Format-on-save for Prettier
This commit is contained in:
parent
3d64bd4c30
commit
0ef09ea168
2 changed files with 39 additions and 0 deletions
|
|
@ -306,6 +306,44 @@ return {
|
|||
"alaviss/nim.nvim",
|
||||
"simrat39/rust-tools.nvim",
|
||||
"neovim/nvim-lspconfig",
|
||||
{
|
||||
"nvimtools/none-ls.nvim",
|
||||
config = function(_plugin, opts)
|
||||
local null_ls = require("null-ls")
|
||||
|
||||
local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false })
|
||||
|
||||
null_ls.setup({
|
||||
sources = {
|
||||
null_ls.builtins.formatting.prettier,
|
||||
},
|
||||
on_attach = function(client, bufnr)
|
||||
if client.supports_method("textDocument/formatting") then
|
||||
vim.keymap.set("n", "<Leader>f", function()
|
||||
vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
|
||||
end, { buffer = bufnr, desc = "[lsp] format" })
|
||||
|
||||
-- format on save
|
||||
vim.api.nvim_clear_autocmds({ buffer = bufnr, group = group })
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
buffer = bufnr,
|
||||
group = group,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, async = false })
|
||||
end,
|
||||
desc = "[lsp] format on save",
|
||||
})
|
||||
end
|
||||
|
||||
if client.supports_method("textDocument/rangeFormatting") then
|
||||
vim.keymap.set("x", "<Leader>f", function()
|
||||
vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
|
||||
end, { buffer = bufnr, desc = "[lsp] format" })
|
||||
end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
},
|
||||
{
|
||||
"williamboman/mason.nvim",
|
||||
build = function(_plugin)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue