Add back format on save functionality
This commit is contained in:
parent
a6e93a6d76
commit
02d2f4066e
39
lua/valeth/plugins/formatting.lua
Normal file
39
lua/valeth/plugins/formatting.lua
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
local autocmd = vim.api.nvim_create_autocmd
|
||||||
|
local autogroup = vim.api.nvim_create_augroup
|
||||||
|
|
||||||
|
local spec = {
|
||||||
|
"mhartington/formatter.nvim",
|
||||||
|
}
|
||||||
|
|
||||||
|
spec.config = function()
|
||||||
|
local formatter = require("formatter")
|
||||||
|
|
||||||
|
formatter.setup({
|
||||||
|
filetype = {
|
||||||
|
c = {
|
||||||
|
require("formatter.filetypes.c").clangformat
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function format_file()
|
||||||
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
local lsp_client = vim.lsp.get_active_clients({ bufnr = bufnr })
|
||||||
|
|
||||||
|
if #lsp_client ~= 0 then
|
||||||
|
vim.lsp.buf.format({ async = false, timeout_ms = 5000 })
|
||||||
|
else
|
||||||
|
vim.cmd(":FormatWrite")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.keymap.set("n", "<Leader>fmt", format_file)
|
||||||
|
|
||||||
|
-- Format on save
|
||||||
|
autocmd("BufWritePost", {
|
||||||
|
group = autogroup("FormatOnSave", {}),
|
||||||
|
callback = format_file,
|
||||||
|
})
|
||||||
|
|
||||||
|
return spec
|
@ -7,7 +7,6 @@ spec.dependencies = {
|
|||||||
"williamboman/mason-lspconfig.nvim",
|
"williamboman/mason-lspconfig.nvim",
|
||||||
"nvim-telescope/telescope.nvim",
|
"nvim-telescope/telescope.nvim",
|
||||||
"folke/neodev.nvim",
|
"folke/neodev.nvim",
|
||||||
"mhartington/formatter.nvim",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spec.config = function()
|
spec.config = function()
|
||||||
@ -59,10 +58,6 @@ spec.config = function()
|
|||||||
vim.keymap.set("n", "<Leader>ci", vim.diagnostic.open_float, opts)
|
vim.keymap.set("n", "<Leader>ci", vim.diagnostic.open_float, opts)
|
||||||
vim.keymap.set("n", "<Leader>re", vim.lsp.buf.rename, opts)
|
vim.keymap.set("n", "<Leader>re", vim.lsp.buf.rename, opts)
|
||||||
vim.keymap.set({ "n", "i" }, "<C-h>", vim.lsp.buf.signature_help, opts)
|
vim.keymap.set({ "n", "i" }, "<C-h>", vim.lsp.buf.signature_help, opts)
|
||||||
|
|
||||||
vim.keymap.set("n", "<Leader>fmt", function()
|
|
||||||
vim.lsp.buf.format({ async = false, timeout_ms = 5000 })
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user