nvim-config/lua/configs/formatter.lua

50 lines
1.4 KiB
Lua
Raw Normal View History

2024-03-20 00:47:26 +00:00
local function config()
local formatter = require('formatter')
local prettierd = require('formatter.defaults.prettierd')
local fmt_lua = require('formatter.filetypes.lua')
local fmt_json = require('formatter.filetypes.json')
local fmt_python = require('formatter.filetypes.python')
formatter.setup({
filetype = {
-- sudo pacman -S stylua
-- https://github.com/JohnnyMorganz/StyLua/releases
lua = { fmt_lua.stylua },
2024-03-25 17:48:50 +00:00
-- pip install isort black ruff
2024-03-20 00:47:26 +00:00
python = {
fmt_python.isort,
2024-03-25 17:48:50 +00:00
function()
-- use `black` or `ruff` based on the project dir
local cwd = vim.fn.getcwd()
-- substr the cwd starting at the last "/" character
local cwd_name = string.sub(cwd, -cwd:reverse():find('/') + 1)
local style = {
['invsys'] = fmt_python.black,
['invsys-b'] = fmt_python.black,
['invsys-c'] = fmt_python.black,
['cubix'] = fmt_python.ruff,
['slots'] = fmt_python.ruff,
}
return style[cwd_name] or nil
end,
2024-03-20 00:47:26 +00:00
},
-- sudo npm i -g fixjson
json = { fmt_json.fixjson },
-- npm i prettierd
typescript = { prettierd },
typescriptreact = { prettierd },
scss = { prettierd },
},
})
-- format on write
vim.api.nvim_create_augroup('__formatter__', { clear = true })
vim.api.nvim_create_autocmd('BufWritePost', {
group = '__formatter__',
command = ':FormatWrite',
})
end
return config