Skip to content

Commit

Permalink
fix(conform.nvim): code format via conform.nvim (#272)
Browse files Browse the repository at this point in the history
* 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 <leader>cf

* fix(conform.nvim): fallback to lsp

* fix(conform.nvim): fix vim commands
  • Loading branch information
linrongbin16 authored Oct 12, 2023
1 parent f56dada commit a0c1848
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 74 deletions.
32 changes: 16 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 10 additions & 7 deletions lua/builtin/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,15 @@ vim.api.nvim_create_autocmd("LspAttach", {
"<cmd>lua vim.lsp.buf.rename()<cr>",
map_desc("Rename symbol")
)
set_key(
{ "n", "x" },
"<Leader>cf",
"<cmd>lua vim.lsp.buf.format({async=false})<cr>",
map_desc("Code format")
)
-- don't format via lsp.
-- use conform.nvim, see: /lua/configs/stevearc/conform-nvim/keys.lua.
--
-- set_key(
-- { "n", "x" },
-- "<Leader>cf",
-- "<cmd>lua vim.lsp.buf.format({async=false})<cr>",
-- map_desc("Code format")
-- )
set_key(
"n",
"<Leader>ca",
Expand Down Expand Up @@ -256,4 +259,4 @@ vim.api.nvim_create_autocmd("LspAttach", {
-- disable tagfunc to fix workspace/symbol not support error
vim.bo.tagfunc = nil
end,
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
return ensure_installed
Original file line number Diff line number Diff line change
Expand Up @@ -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
return setup_handlers
14 changes: 8 additions & 6 deletions lua/configs/stevearc/conform-nvim/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lua/configs/stevearc/conform-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local keymap = require("builtin.utils.keymap")

local M = {
keymap.set_lazy_key(
{ "n", "x" },
"<Leader>cf",
"<cmd>lua require('conform').format({ timeout_ms = 2000, lsp_fallback = true })<cr>",
{ desc = "Code format" }
),
}

return M
1 change: 1 addition & 0 deletions lua/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 21 additions & 24 deletions neoconf_sample.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit a0c1848

Please sign in to comment.