120 lines
3.3 KiB
Lua
120 lines
3.3 KiB
Lua
return {
|
|
"stevearc/conform.nvim",
|
|
event = "VeryLazy",
|
|
opts = {
|
|
-- log_level = vim.log.levels.DEBUG,
|
|
|
|
-- See aviable formatters in: https://github.com/stevearc/conform.nvim#formatters
|
|
-- Formatters can be installed by mason
|
|
formatters_by_ft = {
|
|
-- Conform will run multiple formatters sequentially
|
|
-- Use a stop_after_first = true to run only the first available formatter
|
|
-- Use the "_" filetype to run formatters on filetypes that don't
|
|
-- have other formatters configured.
|
|
["_"] = { "trim_whitespace", "injected" },
|
|
blade = { "blade-formatter" },
|
|
css = { "prettierd", "prettier" },
|
|
go = { "gofumpt", "goimports_reviser", "golines" },
|
|
html = { "djlint", "prettierd", stop_after_first = true },
|
|
javascript = { "prettierd", "prettier", stop_after_first = true },
|
|
javascriptreact = { "prettierd", "prettier", stop_after_first = true },
|
|
json = { "prettierd", "prettier", stop_after_first = true },
|
|
jsonc = { "prettierd", "prettier", stop_after_first = true },
|
|
lua = { "stylua" },
|
|
markdown = { "markdownlint" },
|
|
nim = { "nimpretty" },
|
|
php = { "pint" },
|
|
python = { "ruff" },
|
|
scss = { "prettierd", "prettier", stop_after_first = true },
|
|
sh = { "shfmt" },
|
|
typescript = { "prettierd", "prettier", stop_after_first = true },
|
|
typescriptreact = { "prettierd", "prettier", stop_after_first = true },
|
|
xml = { "lemminx" },
|
|
zsh = { "shfmt" }
|
|
},
|
|
formatters = {
|
|
djlint = {
|
|
prepend_args = {
|
|
"--format-css",
|
|
"--indent-css",
|
|
"2",
|
|
"--format-js",
|
|
"--indent-js",
|
|
"2",
|
|
"--indent",
|
|
"2",
|
|
"--preserve-blank-lines",
|
|
"--quiet"
|
|
}
|
|
}
|
|
},
|
|
format_on_save = function(bufnr)
|
|
-- Disable with a global or buffer-local variable
|
|
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
|
return
|
|
end
|
|
|
|
return { timeout_ms = 2000, lsp_fallback = true }
|
|
end,
|
|
},
|
|
config = function(_, opts)
|
|
require("conform").setup(opts)
|
|
|
|
local function toggleAutoFormat()
|
|
-- to make this global, change b to g
|
|
if vim.b.disable_autoformat == nil then
|
|
vim.b.disable_autoformat = true
|
|
print("Autoformat set to: " .. tostring(not vim.b.disable_autoformat))
|
|
return
|
|
end
|
|
|
|
vim.b.disable_autoformat = not vim.b.disable_autoformat
|
|
print("Autoformat set to: " .. tostring(not vim.b.disable_autoformat))
|
|
end
|
|
|
|
MAP("n", "<leader>uf", toggleAutoFormat, "Toggle auto format")
|
|
|
|
vim.api.nvim_create_user_command("Fmt", function(args)
|
|
local range = nil
|
|
if args.count ~= -1 then
|
|
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
|
|
range = {
|
|
start = { args.line1, 0 },
|
|
["end"] = { args.line2, end_line:len() },
|
|
}
|
|
end
|
|
|
|
local function callback(err, did_edit)
|
|
if not did_edit then
|
|
vim.notify("The file was not formatted:\n" .. tostring(err), vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
if args.bang then
|
|
vim.cmd("w")
|
|
end
|
|
end
|
|
|
|
require("conform").format(
|
|
{
|
|
async = true,
|
|
lsp_format = "fallback",
|
|
range = range,
|
|
formatters = args.fargs
|
|
},
|
|
callback
|
|
)
|
|
end, {
|
|
range = true,
|
|
bang = true,
|
|
force = true,
|
|
desc = "Format the document",
|
|
nargs = '*',
|
|
-- complete = function()
|
|
-- local formatters = require('conform').formatters_by_ft
|
|
--
|
|
-- return vim.tbl_keys(formatters)
|
|
-- end
|
|
})
|
|
end,
|
|
}
|