2023-09-17 19:05:50 +00:00
|
|
|
local prettier = function()
|
2023-09-19 05:21:43 +00:00
|
|
|
return {
|
2023-11-06 06:36:10 +00:00
|
|
|
exe = Get_project_node_modules_path() .. "/.bin/prettier",
|
2023-09-19 05:21:43 +00:00
|
|
|
args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
|
|
|
|
stdin = true,
|
|
|
|
}
|
2023-09-17 19:05:50 +00:00
|
|
|
end
|
|
|
|
|
2023-09-22 03:03:24 +00:00
|
|
|
local goimports = function()
|
2023-09-22 03:33:39 +00:00
|
|
|
local args = {}
|
|
|
|
|
|
|
|
local mod_path = Get_project_go_module_path()
|
|
|
|
if mod_path ~= "" then
|
|
|
|
table.insert(args, "-local")
|
|
|
|
table.insert(args, mod_path)
|
|
|
|
end
|
|
|
|
|
2023-09-22 03:03:24 +00:00
|
|
|
return {
|
|
|
|
exe = "goimports",
|
2023-09-22 03:33:39 +00:00
|
|
|
args = args,
|
2023-09-22 03:03:24 +00:00
|
|
|
stdin = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-11-06 06:36:32 +00:00
|
|
|
local rustfmt = function()
|
|
|
|
return {
|
|
|
|
exe = "rustfmt",
|
|
|
|
args = { "--edition", "2021" },
|
|
|
|
stdin = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-09-19 05:21:43 +00:00
|
|
|
require("formatter").setup({
|
|
|
|
log_level = vim.log.levels.INFO,
|
|
|
|
filetype = {
|
|
|
|
javascript = { prettier },
|
|
|
|
typescript = { prettier },
|
|
|
|
typescriptreact = { prettier },
|
2023-09-22 03:03:24 +00:00
|
|
|
go = { goimports },
|
2023-11-06 06:36:32 +00:00
|
|
|
rust = { rustfmt },
|
2023-09-19 05:15:18 +00:00
|
|
|
|
2023-09-19 05:21:43 +00:00
|
|
|
-- https://github.com/JohnnyMorganz/StyLua
|
|
|
|
-- cargo install stylua --features lua54
|
|
|
|
lua = {
|
|
|
|
require("formatter.filetypes.lua").stylua,
|
|
|
|
},
|
|
|
|
},
|
2023-09-17 19:05:50 +00:00
|
|
|
})
|
|
|
|
|
2023-09-19 04:51:33 +00:00
|
|
|
-- TODO: use vim.api.nvim_create_augroup()
|
2023-09-19 05:21:43 +00:00
|
|
|
vim.api.nvim_exec2(
|
|
|
|
[[
|
2023-09-17 19:05:50 +00:00
|
|
|
augroup FormatAutogroup
|
|
|
|
autocmd!
|
2023-11-06 06:36:32 +00:00
|
|
|
autocmd BufWritePost *.js,*.ts,*.tsx,*.go,*.lua,*.rs silent FormatWrite
|
2023-09-17 19:05:50 +00:00
|
|
|
augroup END
|
2023-09-19 05:21:43 +00:00
|
|
|
]],
|
|
|
|
{ output = true }
|
|
|
|
)
|
2023-09-17 19:05:50 +00:00
|
|
|
|
|
|
|
local bufnr = vim.fn.bufnr("%")
|
2023-09-19 05:21:43 +00:00
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>F", "<Cmd>silent Format<CR>", { noremap = true, silent = true })
|