79 lines
2.3 KiB
Lua
79 lines
2.3 KiB
Lua
vim.keymap.set("n", "<leader>P", function()
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "ConformProject" })
|
|
vim.cmd("ConformProject")
|
|
end, { noremap = true, silent = true })
|
|
|
|
vim.api.nvim_create_user_command("ConformProject", function()
|
|
local conform = require("conform")
|
|
local root = vim.fn.getcwd()
|
|
|
|
-- respects .gitignore
|
|
local handle = io.popen(string.format("cd %s && git ls-files --cached --others --exclude-standard", root))
|
|
|
|
if not handle then
|
|
vim.notify("Failed to scan project files", vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
local files = {}
|
|
for file in handle:lines() do
|
|
table.insert(files, vim.fn.fnamemodify(file, ":p"))
|
|
end
|
|
handle:close()
|
|
|
|
-- format each file
|
|
-- TODO: maybe run each formatter on the directory instead as it may be faster
|
|
-- although that would actually mean we can't count what files were formatted so i don't know
|
|
local formatted_count = 0
|
|
for _, filepath in ipairs(files) do
|
|
local bufnr = vim.fn.bufadd(filepath)
|
|
vim.fn.bufload(bufnr)
|
|
|
|
local ok, err = conform.format({ bufnr = bufnr })
|
|
if ok then
|
|
formatted_count = formatted_count + 1
|
|
-- Save the buffer
|
|
vim.api.nvim_buf_call(bufnr, function()
|
|
vim.cmd("write")
|
|
end)
|
|
end
|
|
end
|
|
|
|
vim.notify(string.format("Formatted %d files", formatted_count), vim.log.levels.INFO)
|
|
end, {})
|
|
|
|
-- docs -> https://github.com/stevearc/conform.nvim
|
|
return {
|
|
{
|
|
"conform.nvim",
|
|
enabled = nixCats("format") or false,
|
|
event = "User ConformProject", -- also will load when we format the entire project yayayayy :333
|
|
keys = {
|
|
{ "<leader>p", desc = "Format File (pretty :3)" },
|
|
},
|
|
after = function(plugin)
|
|
local conform = require("conform")
|
|
|
|
conform.setup({
|
|
formatters_by_ft = {
|
|
lua = nixCats("lang.lua") and { "stylua" } or nil,
|
|
nix = nixCats("lang.nix") and { "alejandra" } or nil,
|
|
rust = nixCats("lang.rust") and { "rustfmt", lsp_format = "fallback" } or nil,
|
|
haskell = nixCats("lang.haskell") and { "ormolu" } or nil,
|
|
proto = nixCats("lang.protobuf") and { "buf" } or nil,
|
|
},
|
|
format_on_save = {
|
|
timeout_ms = 500,
|
|
},
|
|
})
|
|
|
|
vim.keymap.set({ "n", "v" }, "<leader>p", function()
|
|
conform.format({
|
|
lsp_fallback = false,
|
|
async = false,
|
|
timeout_ms = 1000,
|
|
})
|
|
end, { desc = "Format File (pretty :3)" })
|
|
end,
|
|
},
|
|
}
|