diff --git a/nvim/ftplugin/c.lua b/nvim/ftplugin/c.lua index d4da81b..e91f0f4 100644 --- a/nvim/ftplugin/c.lua +++ b/nvim/ftplugin/c.lua @@ -1,6 +1,6 @@ -- Exit if the language server isn't available if vim.fn.executable('clangd') ~= 1 then - return + return end local root_files = { @@ -8,9 +8,12 @@ local root_files = { 'src', } +local lsp = require('user.lsp') + vim.lsp.start { name = 'clangd', cmd = { 'clangd' }, root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - capabilities = require('user.lsp').make_client_capabilities(), + capabilities = lsp.make_client_capabilities(), + on_attach = lsp.on_attach, } diff --git a/nvim/ftplugin/java.lua b/nvim/ftplugin/java.lua index 38cfd50..0589933 100644 --- a/nvim/ftplugin/java.lua +++ b/nvim/ftplugin/java.lua @@ -8,6 +8,8 @@ local root_files = { 'Main.java', } +local lsp = require('user.lsp') + -- Tab Width vim.bo.tabstop = 4 vim.bo.shiftwidth = 4 @@ -17,7 +19,8 @@ vim.lsp.start { name = 'jdtls', cmd = { 'jdtls' }, root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - capabilities = require('user.lsp').make_client_capabilities(), + capabilities = lsp.make_client_capabilities(), + on_attach = lsp.on_attach, settings = { java = { format = { diff --git a/nvim/ftplugin/nix.lua b/nvim/ftplugin/nix.lua index e277e32..7fee20e 100644 --- a/nvim/ftplugin/nix.lua +++ b/nvim/ftplugin/nix.lua @@ -8,9 +8,12 @@ local root_files = { '.git', } +local lsp = require('user.lsp') + vim.lsp.start { name = 'nixd', cmd = { 'nixd' }, root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - capabilities = require('user.lsp').make_client_capabilities(), + capabilities = lsp.make_client_capabilities(), + on_attach = lsp.on_attach, } diff --git a/nvim/lua/user/lsp.lua b/nvim/lua/user/lsp.lua index 63de9b2..fac4fe4 100644 --- a/nvim/lua/user/lsp.lua +++ b/nvim/lua/user/lsp.lua @@ -20,4 +20,30 @@ function M.make_client_capabilities() return capabilities end +---Setup LSP keymaps for the current buffer +---@param bufnr number Buffer number +function M.setup_keymaps(bufnr) + -- Create a shorthand for mapping keys + local opts = { noremap = true, silent = true, buffer = bufnr } + + -- Code actions + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, opts) + -- Quick fix + vim.keymap.set('n', 'qf', vim.lsp.buf.code_action, opts) + -- Apply first available code action + vim.keymap.set('n', 'af', function() + vim.lsp.buf.code_action { + filter = function(action) + return action.isPreferred + end, + apply = true, + } + end, opts) +end + +-- Attach keymaps when LSP client attaches to a buffer +function M.on_attach(client, bufnr) + M.setup_keymaps(bufnr) +end + return M diff --git a/nvim/plugin/keymaps.lua b/nvim/plugin/keymaps.lua index 2de9540..a3e0751 100644 --- a/nvim/plugin/keymaps.lua +++ b/nvim/plugin/keymaps.lua @@ -57,9 +57,6 @@ vim.api.nvim_set_keymap( { noremap = true, silent = true, desc = 'debugger [r]epl' } ) --- LSP Fix -vim.keymap.set('n', 'f', vim.lsp.buf.code_action, { noremap = true, silent = true, desc = 'Code Action' }) - -- Window Resizing vim.keymap.set('n', '', 'vertical resize -2', { noremap = true, silent = true }) vim.keymap.set('n', '', 'vertical resize +2', { noremap = true, silent = true })