diff --git a/config/native/lua/konrad/lsp/capability_handlers/format.lua b/config/native/lua/konrad/lsp/capability_handlers/format.lua index 7af93f1..864ba1d 100644 --- a/config/native/lua/konrad/lsp/capability_handlers/format.lua +++ b/config/native/lua/konrad/lsp/capability_handlers/format.lua @@ -1,4 +1,4 @@ -local format_is_enabled = true; +local format_is_enabled = true local M = {} @@ -9,15 +9,17 @@ M.setup = function(data) local bufnr = data.bufnr local client = data.client - vim.api.nvim_create_user_command("AutoFormatToggle", - function() - format_is_enabled = not format_is_enabled - print('Setting autoformatting to: ' .. tostring(format_is_enabled)) - end, { - desc = "Enable/disable autoformat with lsp", - }) + -- Use LSP as the handler for formatexpr. + vim.bo[bufnr].formatexpr = "v:lua.vim.lsp.formatexpr()" + + vim.api.nvim_create_user_command("AutoFormatToggle", function() + format_is_enabled = not format_is_enabled + print("Setting autoformatting to: " .. tostring(format_is_enabled)) + end, { + desc = "Enable/disable autoformat with lsp", + }) - vim.api.nvim_create_autocmd('BufWritePre', { + vim.api.nvim_create_autocmd("BufWritePre", { desc = "AutoFormat on save", group = augroup, buffer = bufnr, @@ -32,15 +34,13 @@ M.setup = function(data) end, }) - vim.api.nvim_buf_create_user_command(bufnr, "Format", - function() - vim.lsp.buf.format({ - async = false, - id = client.id, - bufnr = bufnr, - }) - end, - { desc = 'Format current buffer with LSP' }) + vim.api.nvim_buf_create_user_command(bufnr, "Format", function() + vim.lsp.buf.format({ + async = false, + id = client.id, + bufnr = bufnr, + }) + end, { desc = "Format current buffer with LSP" }) return { commands = { "AutoFormatToggle" }, diff --git a/config/native/lua/konrad/lsp/event_handlers.lua b/config/native/lua/konrad/lsp/event_handlers.lua index 6697bff..ad220a3 100644 --- a/config/native/lua/konrad/lsp/event_handlers.lua +++ b/config/native/lua/konrad/lsp/event_handlers.lua @@ -1,7 +1,7 @@ -local telescope = require('telescope.builtin') -local keymapper = require('konrad.lsp.keymapper') -local registry = require('konrad.lsp.registry') -local augroups = require('konrad.lsp.augroups') +local telescope = require("telescope.builtin") +local keymapper = require("konrad.lsp.keymapper") +local registry = require("konrad.lsp.registry") +local augroups = require("konrad.lsp.augroups") local M = {} @@ -11,13 +11,13 @@ M.detach = function(client, bufnr) augroups.del_autocmds_for_buf(client, bufnr) if client.supports_method("textDocument/codeLens") then - require('konrad.lsp.capability_handlers.codelens').detach() + require("konrad.lsp.capability_handlers.codelens").detach() end -- TODO not sure why this always returns true -- if client.supports_method("textDocument/inlayHint") then if client.server_capabilities.inlayHintProvider then - require('konrad.lsp.capability_handlers.inlayhints').detach() + require("konrad.lsp.capability_handlers.inlayhints").detach() end registry.deregister(client, bufnr) @@ -38,28 +38,22 @@ M.attach = function(client, bufnr) bufnr = bufnr, client = client, } - -- Enable completion triggered by - vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' if client.supports_method("textDocument/codeAction") then vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts_with_desc("Code Action")) end if client.supports_method("textDocument/codeLens") then - registry.register_once("CodeLens", register_data, require('konrad.lsp.capability_handlers.codelens').attach) + registry.register_once("CodeLens", register_data, require("konrad.lsp.capability_handlers.codelens").attach) end - if client.supports_method("textDocument/formatting") then - registry.register_once("Formatting", register_data, require('konrad.lsp.capability_handlers.format').setup) - end - - if client.supports_method("textDocument/documentHighlight") then - registry.register_once("DocumentHighlighting", register_data, - require('konrad.lsp.capability_handlers.documenthighlight').setup) + if client.supports_method("textDocument/completion") then + -- Enable completion triggered by + vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc" end - if client.supports_method("textDocument/documentSymbol") then - registry.register_once("Navic", register_data, require("konrad.lsp.capability_handlers.navic").setup) + if client.supports_method("textDocument/formatting") then + registry.register_once("Formatting", register_data, require("konrad.lsp.capability_handlers.format").setup) end if client.supports_method("textDocument/declaration") then @@ -67,18 +61,30 @@ M.attach = function(client, bufnr) end if client.supports_method("textDocument/definition") then + vim.bo[bufnr].tagfunc = "v:lua.vim.lsp.tagfunc" vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts_with_desc("Go To Definition")) vim.keymap.set("n", "fd", telescope.lsp_definitions, opts_with_desc("Telescope [D]efinitions")) end + if client.supports_method("textDocument/documentHighlight") then + registry.register_once( + "DocumentHighlighting", + register_data, + require("konrad.lsp.capability_handlers.documenthighlight").setup + ) + end + + if client.supports_method("textDocument/documentSymbol") then + registry.register_once("Navic", register_data, require("konrad.lsp.capability_handlers.navic").setup) + end + if client.supports_method("textDocument/hover") then vim.keymap.set("n", "K", vim.lsp.buf.hover, opts_with_desc("Hover")) end if client.supports_method("textDocument/implementation") then vim.keymap.set("n", "gp", vim.lsp.buf.implementation, opts_with_desc("Go To Implementation")) - vim.keymap.set("n", "fp", telescope.lsp_implementations, - opts_with_desc("Telescope Im[p]lementations")) + vim.keymap.set("n", "fp", telescope.lsp_implementations, opts_with_desc("Telescope Im[p]lementations")) end if client.supports_method("textDocument/references") then @@ -97,26 +103,35 @@ M.attach = function(client, bufnr) if client.supports_method("textDocument/typeDefinition") then vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, opts_with_desc("Go To Type Definition")) - vim.keymap.set("n", "fT", telescope.lsp_type_definitions, - opts_with_desc("Telescope [T]ype Definitions")) + vim.keymap.set( + "n", + "fT", + telescope.lsp_type_definitions, + opts_with_desc("Telescope [T]ype Definitions") + ) end if client.supports_method("workspaceSymbol/resolve") then vim.keymap.set("n", "ws", vim.lsp.buf.workspace_symbol, opts_with_desc("Search workspace symbols")) - vim.keymap.set("n", "fws", telescope.lsp_workspace_symbols, - opts_with_desc("Telescope [W]orkspace [S]ymbols")) + vim.keymap.set( + "n", + "fws", + telescope.lsp_workspace_symbols, + opts_with_desc("Telescope [W]orkspace [S]ymbols") + ) end -- TODO not sure why this always returns true -- if client.supports_method("textDocument/inlayHint") then if client.server_capabilities.inlayHintProvider then - registry.register_once("InlayHints", register_data, require('konrad.lsp.capability_handlers.inlayhints').attach) + registry.register_once("InlayHints", register_data, require("konrad.lsp.capability_handlers.inlayhints").attach) end vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts_with_desc("Add Workspace Folder")) vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts_with_desc("Remove Workspace Folder")) - vim.keymap.set("n", "wl", function() P(vim.lsp.buf.list_workspace_folders()) end, - opts_with_desc("List Workspace Folders")) + vim.keymap.set("n", "wl", function() + P(vim.lsp.buf.list_workspace_folders()) + end, opts_with_desc("List Workspace Folders")) end return M