can format entire projects now
This commit is contained in:
parent
211fa03252
commit
3e14220569
1 changed files with 39 additions and 0 deletions
|
|
@ -1,3 +1,42 @@
|
|||
vim.keymap.set("n", "<leader>P", "<cmd>ConformProject<cr>", { noremap = true, silent = true })
|
||||
|
||||
vim.api.nvim_create_user_command("ConformProject", function()
|
||||
local conform = require("conform")
|
||||
local root = vim.fn.getcwd()
|
||||
|
||||
-- Use git to get files (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
|
||||
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 {
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue