From a0c184813400adadc31a08208c23caa30b9649ce Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Thu, 12 Oct 2023 11:57:21 +0800 Subject: [PATCH] fix(conform.nvim): code format via conform.nvim (#272) * chore(install): disable nvim-lint generated configs * chore(hooks): narrow hooks file * chore(hooks): narrow hooks file * chore(hooks): narrow hooks files * chore(hooks): narrow ignored generated hooks * chore(hooks): narrow ignored generated files * fix(mason-null-ls.nvim): ensure diagnostics installed, not formatters * fix(mason-null-ls.nvim): disable formatters setup handlers * chore(neoconf): format code * fix(conform.nvim): use conform formatter on cf * fix(conform.nvim): fallback to lsp * fix(conform.nvim): fix vim commands --- .gitignore | 32 ++++++------- install.ps1 | 14 +++--- install.sh | 12 ++--- lua/builtin/lsp.lua | 17 ++++--- .../ensure_installed_sample.lua | 7 ++- .../setup_handlers_sample.lua | 8 ++-- lua/configs/stevearc/conform-nvim/config.lua | 14 +++--- lua/configs/stevearc/conform-nvim/keys.lua | 12 +++++ lua/plugins/init.lua | 1 + neoconf_sample.json | 45 +++++++++---------- 10 files changed, 88 insertions(+), 74 deletions(-) create mode 100644 lua/configs/stevearc/conform-nvim/keys.lua diff --git a/.gitignore b/.gitignore index 1c0282f6..8717bf83 100644 --- a/.gitignore +++ b/.gitignore @@ -9,28 +9,28 @@ /universal-ctags /golang-tools-install-script -# global hooks -preinit.vim -preinit.lua -postinit.vim -postinit.lua +# hooks +/preinit.vim +/postinit.vim +/lua/preinit.lua +/lua/postinit.lua # plugin hooks /lua/plugins/users.lua -user_plugins.lua -user_plugins_blacklist.lua -user_init.lua -user_init.vim -user_config.lua -user_config.vim -user_keys.lua +/lua/user_plugins.lua +/lua/user_plugins_blacklist.lua +/lua/configs/**/user_init.lua +/lua/configs/**/user_init.vim +/lua/configs/**/user_config.lua +/lua/configs/**/user_config.vim +/lua/configs/**/user_keys.lua # lsp hooks -ensure_installed.lua -setup_handlers.lua +/lua/configs/**/ensure_installed.lua +/lua/configs/**/setup_handlers.lua /lua/configs/stevearc/conform-nvim/formatters_by_ft.lua /lua/configs/mfussenegger/nvim-lint/linters_by_ft.lua -neoconf.json +/neoconf.json # nvim-cmp hooks -setup_handler.lua +/lua/configs/hrsh7th/nvim-cmp/setup_handler.lua diff --git a/install.ps1 b/install.ps1 index b418f506..d7611496 100644 --- a/install.ps1 +++ b/install.ps1 @@ -177,13 +177,13 @@ function NvimConfig() Copy-Item -Path "$ConformHome\formatters_by_ft_sample.lua" -Destination "$ConformFormattersByFt" } - # nvim-lint - $NvimLintHome="$NVIM_HOME\lua\configs\mfussenegger\nvim-lint" - $NvimLintLintersByFt="$NvimLintHome\linters_by_ft.lua" - if (-not(TestReparsePoint $NvimLintLintersByFt) -and -not(Test-Path $NvimLintLintersByFt)) - { - Copy-Item -Path "$NvimLintHome\linters_by_ft_sample.lua" -Destination "$NvimLintLintersByFt" - } + # # nvim-lint + # $NvimLintHome="$NVIM_HOME\lua\configs\mfussenegger\nvim-lint" + # $NvimLintLintersByFt="$NvimLintHome\linters_by_ft.lua" + # if (-not(TestReparsePoint $NvimLintLintersByFt) -and -not(Test-Path $NvimLintLintersByFt)) + # { + # Copy-Item -Path "$NvimLintHome\linters_by_ft_sample.lua" -Destination "$NvimLintLintersByFt" + # } # neoconf $Neoconf = "$NVIM_HOME\neoconf.json" diff --git a/install.sh b/install.sh index d0d636e8..7e1b874f 100755 --- a/install.sh +++ b/install.sh @@ -117,12 +117,12 @@ nvim_config() { cp $conform_home/formatters_by_ft_sample.lua $conform_formatters_by_ft fi - # nvim-lint - local nvim_lint_home="$NVIM_HOME/lua/configs/mfussenegger/nvim-lint" - local nvim_lint_linters_by_ft="$nvim_lint_home/linters_by_ft.lua" - if [ ! -f $nvim_lint_linters_by_ft ]; then - cp $nvim_lint_home/linters_by_ft_sample.lua $nvim_lint_linters_by_ft - fi + # # nvim-lint + # local nvim_lint_home="$NVIM_HOME/lua/configs/mfussenegger/nvim-lint" + # local nvim_lint_linters_by_ft="$nvim_lint_home/linters_by_ft.lua" + # if [ ! -f $nvim_lint_linters_by_ft ]; then + # cp $nvim_lint_home/linters_by_ft_sample.lua $nvim_lint_linters_by_ft + # fi # neoconf local neoconf="$NVIM_HOME/neoconf.json" diff --git a/lua/builtin/lsp.lua b/lua/builtin/lsp.lua index 2424539e..74c36f9e 100644 --- a/lua/builtin/lsp.lua +++ b/lua/builtin/lsp.lua @@ -168,12 +168,15 @@ vim.api.nvim_create_autocmd("LspAttach", { "lua vim.lsp.buf.rename()", map_desc("Rename symbol") ) - set_key( - { "n", "x" }, - "cf", - "lua vim.lsp.buf.format({async=false})", - map_desc("Code format") - ) + -- don't format via lsp. + -- use conform.nvim, see: /lua/configs/stevearc/conform-nvim/keys.lua. + -- + -- set_key( + -- { "n", "x" }, + -- "cf", + -- "lua vim.lsp.buf.format({async=false})", + -- map_desc("Code format") + -- ) set_key( "n", "ca", @@ -256,4 +259,4 @@ vim.api.nvim_create_autocmd("LspAttach", { -- disable tagfunc to fix workspace/symbol not support error vim.bo.tagfunc = nil end, -}) \ No newline at end of file +}) diff --git a/lua/configs/jay-babu/mason-null-ls-nvim/ensure_installed_sample.lua b/lua/configs/jay-babu/mason-null-ls-nvim/ensure_installed_sample.lua index fc204c0d..1dea5b23 100644 --- a/lua/configs/jay-babu/mason-null-ls-nvim/ensure_installed_sample.lua +++ b/lua/configs/jay-babu/mason-null-ls-nvim/ensure_installed_sample.lua @@ -6,9 +6,8 @@ -- * [null-ls BUILTINS](https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md) and install them from mason. local ensure_installed = { - "stylua", -- lua formatter - "prettier", -- js,ts,json,html,css,etc formatter - "eslint", -- js,ts,json,html,css,etc diagnostic + "jsonlint", -- json + "markdownlint", -- markdown } -return ensure_installed \ No newline at end of file +return ensure_installed diff --git a/lua/configs/jay-babu/mason-null-ls-nvim/setup_handlers_sample.lua b/lua/configs/jay-babu/mason-null-ls-nvim/setup_handlers_sample.lua index 97c9cb35..652f02b7 100644 --- a/lua/configs/jay-babu/mason-null-ls-nvim/setup_handlers_sample.lua +++ b/lua/configs/jay-babu/mason-null-ls-nvim/setup_handlers_sample.lua @@ -9,9 +9,9 @@ local null_ls = require("null-ls") local setup_handlers = { -- Custom setup. - stylua = function(source, methods) - null_ls.register(null_ls.builtins.formatting.stylua) - end, + -- stylua = function(source, methods) + -- null_ls.register(null_ls.builtins.formatting.stylua) + -- end, } -return setup_handlers \ No newline at end of file +return setup_handlers diff --git a/lua/configs/stevearc/conform-nvim/config.lua b/lua/configs/stevearc/conform-nvim/config.lua index 55d1c74a..be8f9b9b 100644 --- a/lua/configs/stevearc/conform-nvim/config.lua +++ b/lua/configs/stevearc/conform-nvim/config.lua @@ -22,12 +22,14 @@ end require("conform").setup({ formatters_by_ft = formatters_by_ft, - format_on_save = { - -- These options will be passed to conform.format() - timeout_ms = 2000, - lsp_fallback = true, - async = true, - }, + + -- format_on_save = { + -- -- These options will be passed to conform.format() + -- timeout_ms = 2000, + -- lsp_fallback = true, + -- async = true, + -- }, + format_after_save = { lsp_fallback = true, async = true, diff --git a/lua/configs/stevearc/conform-nvim/keys.lua b/lua/configs/stevearc/conform-nvim/keys.lua new file mode 100644 index 00000000..4fe2f432 --- /dev/null +++ b/lua/configs/stevearc/conform-nvim/keys.lua @@ -0,0 +1,12 @@ +local keymap = require("builtin.utils.keymap") + +local M = { + keymap.set_lazy_key( + { "n", "x" }, + "cf", + "lua require('conform').format({ timeout_ms = 2000, lsp_fallback = true })", + { desc = "Code format" } + ), +} + +return M diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 17c8fb9c..cd5cf79f 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -207,6 +207,7 @@ local M = { "williamboman/mason-lspconfig.nvim", }, config = lua_config("stevearc/conform.nvim"), + keys = lua_keys("stevearc/conform.nvim"), }, -- { -- "mfussenegger/nvim-lint", diff --git a/neoconf_sample.json b/neoconf_sample.json index 067ae3db..db5f4372 100644 --- a/neoconf_sample.json +++ b/neoconf_sample.json @@ -1,29 +1,26 @@ // Please copy this file to 'neoconf.json' to enable it. { - "neodev": { - "library": { - "enabled": true, - "plugins": [ - "nvim-lspconfig", - "lsp" - ] - } - }, - "neoconf": { - "plugins": { - "lua_ls": { - "enabled": true - } - } + "neodev": { + "library": { + "enabled": true, + "plugins": ["nvim-lspconfig", "lsp"] + } + }, + "neoconf": { + "plugins": { + "lua_ls": { + "enabled": true + } + } + }, + "lspconfig": { + "lua_ls": { + "Lua.format.enable": false, // use stylua formatter + "Lua.completion.callSnippet": "Replace", + "Lua.workspace.checkThirdParty": false }, - "lspconfig": { - "lua_ls": { - "Lua.format.enable": false, // use stylua formatter - "Lua.completion.callSnippet": "Replace", - "Lua.workspace.checkThirdParty": false - }, - "jsonls": { - "json.format.enable": false // use prettier formatter - } + "jsonls": { + "json.format.enable": false // use prettier formatter } + } }