chore(nvim): add conform.nvim

This commit is contained in:
Rob Watson 2024-04-08 09:31:50 +02:00
parent e19302fa19
commit a3f44dfdde
3 changed files with 27 additions and 67 deletions

10
nvimrc
View File

@ -282,6 +282,11 @@ packadd! vim-rhubarb " https://github.com/tpope/vim-rhubarb.git
packadd! editorconfig-vim " https://github.com/editorconfig/editorconfig-vim.git
packadd! vim-wordmotion " https://github.com/chaoren/vim-wordmotion.git
" conform.nvim:
packadd! conform.nvim " https://github.com/stevearc/conform.nvim.git
lua require "conform_config"
" nvim-dap
packadd! nvim-dap " https://github.com/mfussenegger/nvim-dap.git
packadd! nvim-dap-go " https://github.com/leoluz/nvim-dap-go.git
@ -289,11 +294,6 @@ packadd! nvim-dap-ruby " https://github.com/suketa/nvim-dap-ruby.git
lua require "debugger"
" formatter.nvim:
packadd! formatter.nvim " https://github.com/mhartington/formatter.nvim.git
lua require "formatter_config"
" treesitter:
packadd! nvim-treesitter " https://github.com/nvim-treesitter/nvim-treesitter.git
packadd! nvim-treesitter-textobjects " https://github.com/nvim-treesitter/nvim-treesitter-textobjects.git

View File

@ -0,0 +1,22 @@
-- TODO: prettierd
-- TODO: async save-on-write? Or set timeout.
require("conform").setup({
formatters_by_ft = {
javascript = { "prettier" },
go = { "goimports" },
lua = { "stylua" },
rust = { "rustfmt" },
typescript = { "prettier" },
typescriptreact = { "prettier" },
},
formatters = {
prettier = {
args = { "--stdin-filepath", "$FILENAME", "--single-quote" },
},
},
format_on_save = {
lsp_fallback = true,
},
log_level = vim.log.levels.ERROR,
notify_on_error = true,
})

View File

@ -1,62 +0,0 @@
local prettier = function()
return {
exe = Get_project_node_modules_path() .. "/.bin/prettier",
args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
stdin = true,
}
end
local goimports = function()
local args = {}
local mod_path = Get_project_go_module_path()
if mod_path ~= "" then
table.insert(args, "-local")
table.insert(args, mod_path)
end
return {
exe = "goimports",
args = args,
stdin = true,
}
end
local rustfmt = function()
return {
exe = "rustfmt",
args = { "--edition", "2021" },
stdin = true,
}
end
require("formatter").setup({
log_level = vim.log.levels.INFO,
filetype = {
javascript = { prettier },
typescript = { prettier },
typescriptreact = { prettier },
go = { goimports },
rust = { rustfmt },
-- https://github.com/JohnnyMorganz/StyLua
-- cargo install stylua --features lua54
lua = {
require("formatter.filetypes.lua").stylua,
},
},
})
-- TODO: use vim.api.nvim_create_augroup()
vim.api.nvim_exec2(
[[
augroup FormatAutogroup
autocmd!
autocmd BufWritePost *.js,*.ts,*.tsx,*.go,*.lua,*.rs silent FormatWrite
augroup END
]],
{ output = true }
)
local bufnr = vim.fn.bufnr("%")
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>F", "<Cmd>silent Format<CR>", { noremap = true, silent = true })