Skip to content

Commit

Permalink
neovim: update lsp formatting settings
Browse files Browse the repository at this point in the history
Use conform.nvim instead of null-ls
  • Loading branch information
geodimm committed Mar 3, 2024
1 parent d0806c8 commit 7a82e3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 78 deletions.
1 change: 0 additions & 1 deletion Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ brew "moreutils"
brew "ncurses"
brew "nvm"
brew "ripgrep"
brew "rust"
brew "shellcheck"
brew "tig"
brew "tree"
Expand Down
90 changes: 53 additions & 37 deletions nvim/lua/plugins/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local icons = require('user.icons')
local lsp_utils = require('utils.lsp')
local feat = require('utils.feat')

local lsp_tools = {
-- language servers
Expand All @@ -26,9 +27,9 @@ local lsp_tools = {
'markdownlint',

-- formatters
'gci',
'shfmt',
'stylua',
'clang-format',

-- code actions
'gomodifytags',
Expand Down Expand Up @@ -157,7 +158,33 @@ local servers_config = {
return {
{
'neovim/nvim-lspconfig',
dependencies = { 'williamboman/mason-lspconfig.nvim' },
dependencies = {
{
'williamboman/mason.nvim',
opts = {
ui = {
border = 'rounded',
icons = {
package_installed = icons.ui.check,
package_pending = icons.ui.play,
package_uninstalled = icons.ui.times,
},
},
},
},
{
'williamboman/mason-lspconfig.nvim',
dependencies = { 'williamboman/mason.nvim' },
},
{
'WhoIsSethDaniel/mason-tool-installer.nvim',
opts = {
ensure_installed = lsp_tools,
auto_update = true,
run_on_start = true,
},
},
},
init = function()
lsp_utils.customise_ui()
lsp_utils.setup_vim_diagnostics()
Expand All @@ -177,30 +204,35 @@ return {
end,
},
{
'williamboman/mason.nvim',
'stevearc/conform.nvim',
opts = {
ui = {
border = 'rounded',
icons = {
package_installed = icons.ui.check,
package_pending = icons.ui.play,
package_uninstalled = icons.ui.times,
notify_on_error = true,
format_on_save = function(bufnr)
if feat.Formatting:is_disabled(bufnr) then
return
end
return {
timeout_ms = 500,
lsp_fallback = true,
}
end,
formatters_by_ft = {
lua = { 'stylua' },
go = { 'gci' },
sh = { 'shfmt' },
markdown = { 'markdownlint' },
rust = { 'rustfmt' },
},
formatters = {
markdownlint = {
prepend_args = {
'--config',
vim.fn.expand('$HOME/dotfiles/markdownlint/markdownlint.yaml'),
},
},
},
},
},
{
'williamboman/mason-lspconfig.nvim',
dependencies = { 'williamboman/mason.nvim' },
},
{
'WhoIsSethDaniel/mason-tool-installer.nvim',
opts = {
ensure_installed = lsp_tools,
auto_update = true,
run_on_start = true,
},
},
{
'nvimtools/none-ls.nvim',
dependencies = {
Expand All @@ -226,22 +258,6 @@ return {
null_ls.builtins.diagnostics.zsh,
null_ls.builtins.diagnostics.actionlint,

-- formatting
null_ls.builtins.formatting.clang_format.with({
extra_args = {
'--style',
'{BasedOnStyle: llvm, IndentWidth: 4}',
},
}),
null_ls.builtins.formatting.stylua,
null_ls.builtins.formatting.shfmt,
null_ls.builtins.formatting.markdownlint.with({
extra_args = {
'--config',
vim.fn.expand('$HOME/dotfiles/markdownlint/markdownlint.yaml'),
},
}),

-- code actions
null_ls.builtins.code_actions.refactoring,
null_ls.builtins.code_actions.gomodifytags,
Expand Down
42 changes: 2 additions & 40 deletions nvim/lua/utils/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ local function configure_keymaps(bufnr)
keymap.set('n', '<leader>ck', vim.lsp.buf.signature_help, { desc = 'Signature help', buffer = bufnr })
keymap.set('i', '<c-k>', vim.lsp.buf.signature_help, { desc = 'Signature help', buffer = bufnr })
keymap.set('n', '<leader>cr', vim.lsp.buf.rename, { desc = 'Rename', buffer = bufnr })
keymap.set('n', '<leader>cf', vim.lsp.buf.format, { desc = 'Format', buffer = bufnr })
keymap.set({ 'v', 'n' }, '<leader>ca', vim.lsp.buf.code_action, { desc = 'Code action', buffer = bufnr })
keymap.set('n', '<leader>cl', vim.lsp.codelens.refresh, { desc = 'Refresh codelens', buffer = bufnr })
keymap.set('n', '<leader>rl', vim.lsp.codelens.run, { desc = 'Run codelens', buffer = bufnr })
Expand Down Expand Up @@ -137,44 +136,6 @@ local function configure_autocmds(client, bufnr)
callback = vim.lsp.buf.clear_references,
})
end

if client.supports_method('textDocument/formatting') then
vim.api.nvim_create_augroup('user_lsp_document_format', { clear = false })
vim.api.nvim_clear_autocmds({
group = 'user_lsp_document_format',
buffer = bufnr,
})
local null_ls_command_prefix = 'NULL_LS'
vim.api.nvim_create_autocmd('BufWritePre', {
group = 'user_lsp_document_format',
buffer = bufnr,
desc = 'format on save',
callback = function(_)
if feat.Formatting:is_disabled(bufnr) then
return
end
vim.lsp.buf.format({ bufnr = bufnr })

-- Workaround for gopls not organizing imports on vim.lsp.buf.format
-- Call the organizeImports codeActions for *.go files
if vim.bo.filetype == 'go' then
local params = vim.lsp.util.make_range_params()
params.context = { only = { 'source.organizeImports' } }
---@diagnostic disable-next-line: param-type-mismatch
local result = vim.lsp.buf_request_sync(bufnr, 'textDocument/codeAction', params, 5000)
for _, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
vim.lsp.util.apply_workspace_edit(r.edit, client.offset_encoding)
elseif r.command:sub(1, #null_ls_command_prefix) ~= null_ls_command_prefix then
vim.lsp.buf.execute_command(r.command)
end
end
end
end
end,
})
end
end

-- Use an on_attach function to only map the following keys
Expand Down Expand Up @@ -215,7 +176,8 @@ end

-- Create config that activates keymaps and enables snippet support
local function create_config(servers, server)
local capabilities = require('cmp_nvim_lsp').default_capabilities()
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())

local opts = {
capabilities = capabilities,
Expand Down

0 comments on commit 7a82e3b

Please sign in to comment.