From a3f44dfddefc0afd96058770fdfec1ab6e7faf9c Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Mon, 8 Apr 2024 09:31:50 +0200 Subject: [PATCH] chore(nvim): add conform.nvim --- nvimrc | 10 +++--- vim/lua/conform_config.lua | 22 +++++++++++++ vim/lua/formatter_config.lua | 62 ------------------------------------ 3 files changed, 27 insertions(+), 67 deletions(-) create mode 100644 vim/lua/conform_config.lua delete mode 100644 vim/lua/formatter_config.lua diff --git a/nvimrc b/nvimrc index 65e56d4..6412845 100644 --- a/nvimrc +++ b/nvimrc @@ -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 diff --git a/vim/lua/conform_config.lua b/vim/lua/conform_config.lua new file mode 100644 index 0000000..d093317 --- /dev/null +++ b/vim/lua/conform_config.lua @@ -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, +}) diff --git a/vim/lua/formatter_config.lua b/vim/lua/formatter_config.lua deleted file mode 100644 index 66f4cbc..0000000 --- a/vim/lua/formatter_config.lua +++ /dev/null @@ -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", "F", "silent Format", { noremap = true, silent = true })