From 15127f310eb3860f598c06bc977d6838c835a3f8 Mon Sep 17 00:00:00 2001 From: Walter Jenkins Date: Mon, 1 Dec 2025 12:19:37 -0600 Subject: [PATCH] fix issue with gd --- lua/core/ui.lua | 1 + lua/plugins/lsp-keymaps.lua | 110 ++++++++++++++++++++++++++++-------- lua/plugins/lsp.lua | 62 ++++++++++---------- lua/plugins/mason.lua | 5 +- 4 files changed, 121 insertions(+), 57 deletions(-) diff --git a/lua/core/ui.lua b/lua/core/ui.lua index d6131d67..d9a29d67 100644 --- a/lua/core/ui.lua +++ b/lua/core/ui.lua @@ -53,6 +53,7 @@ return { -- ✅ Disable project module to avoid error opts.config.project = { enable = false } + pcall(vim.keymap.del, "n", "g") end, }, } diff --git a/lua/plugins/lsp-keymaps.lua b/lua/plugins/lsp-keymaps.lua index c0b67895..d4c2ec30 100644 --- a/lua/plugins/lsp-keymaps.lua +++ b/lua/plugins/lsp-keymaps.lua @@ -1,49 +1,113 @@ -- lua/plugins/lsp-keymaps.lua return { -- Attach to the existing LSP config plugin - 'neovim/nvim-lspconfig', + "neovim/nvim-lspconfig", -- We just need this to run once; inside we create an LspAttach autocmd - event = 'VeryLazy', + event = "VeryLazy", config = function() - -- Only define the autocmd once - vim.api.nvim_create_augroup('UserLspKeymaps', { clear = true }) + local group = vim.api.nvim_create_augroup("UserLspKeymaps", { clear = true }) - vim.api.nvim_create_autocmd('LspAttach', { - group = 'UserLspKeymaps', + vim.api.nvim_create_autocmd("LspAttach", { + group = group, callback = function(ev) local bufnr = ev.buf - local opts = function(extra) - return vim.tbl_extend('force', { buffer = bufnr, silent = true, noremap = true }, extra or {}) + + -- Helper to build opts for this buffer + local function opts(desc) + return { + buffer = bufnr, + silent = true, + noremap = true, + desc = desc, + } + end + + -- Smarter "go to definition": + -- - Ask for definition + -- - If it points to an import line in the same file, fall back to implementation + local function goto_definition_preferring_source() + local params = vim.lsp.util.make_position_params() + + local function jump_to(loc) + if not loc then return end + vim.lsp.util.jump_to_location(loc, "utf-8") + end + + vim.lsp.buf_request(bufnr, "textDocument/definition", params, function(err, result) + if err or not result then + return + end + + local locations = result + if not vim.tbl_islist(locations) then + locations = { locations } + end + if #locations == 0 then + return + end + + local first = locations[1] + local uri = first.uri or first.targetUri + local range = first.range or first.targetRange + + if not (uri and range) then + return jump_to(first) + end + + local def_bufnr = vim.uri_to_bufnr(uri) + local cur_bufnr = bufnr + + -- If TS says "definition is this import in the same file", try implementation instead. + if def_bufnr == cur_bufnr then + local line_nr = range.start.line + local line = vim.api.nvim_buf_get_lines(def_bufnr, line_nr, line_nr + 1, false)[1] or "" + if line:match("^%s*import") then + vim.lsp.buf_request(bufnr, "textDocument/implementation", params, function(err2, impl) + if err2 or not impl then + return jump_to(first) + end + local impl_locs = impl + if not vim.tbl_islist(impl_locs) then + impl_locs = { impl_locs } + end + jump_to(impl_locs[1] or first) + end) + return + end + end + + jump_to(first) + end) end -- Core LSP maps - vim.keymap.set('n', 'cl', 'LspInfo', opts { desc = 'LSP Info' }) + vim.keymap.set("n", "cl", "LspInfo", opts("LSP Info")) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts { desc = 'Go to Definition' }) - vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts { desc = 'References' }) - vim.keymap.set('n', 'gI', vim.lsp.buf.implementation, opts { desc = 'Go to Implementation' }) - vim.keymap.set('n', 'gy', vim.lsp.buf.type_definition, opts { desc = 'Go to Type Definition' }) - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts { desc = 'Go to Declaration' }) - vim.keymap.set('n', 'gK', vim.lsp.buf.signature_help, opts { desc = 'Signature Help' }) + vim.keymap.set("n", "gd", goto_definition_preferring_source, opts("Go to Definition")) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts("References")) + vim.keymap.set("n", "gI", vim.lsp.buf.implementation, opts("Go to Implementation")) + vim.keymap.set("n", "gy", vim.lsp.buf.type_definition, opts("Go to Type Definition")) + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts("Go to Declaration")) + vim.keymap.set("n", "gK", vim.lsp.buf.signature_help, opts("Signature Help")) - vim.keymap.set('i', '', vim.lsp.buf.signature_help, opts { desc = 'Signature Help' }) + vim.keymap.set("i", "", vim.lsp.buf.signature_help, opts("Signature Help")) - vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts { desc = 'Code Action' }) + vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts("Code Action")) -- Rename symbol - vim.keymap.set('n', 'cr', vim.lsp.buf.rename, opts { desc = 'Rename' }) + vim.keymap.set("n", "cr", vim.lsp.buf.rename, opts("Rename")) - -- Rename file via Snacks, but **safely** - vim.keymap.set('n', 'cR', function() - local ok, snacks = pcall(require, 'snacks') + -- Rename file via Snacks, but safely + vim.keymap.set("n", "cR", function() + local ok, snacks = pcall(require, "snacks") if ok and snacks.rename and snacks.rename.rename_file then snacks.rename.rename_file() else - print 'Snacks rename not available' + print("Snacks rename not available") end - end, opts { desc = 'Rename File' }) + end, opts("Rename File")) end, }) end, diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 016b536e..953fedee 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -71,37 +71,37 @@ return { }, }) - -- ============================== - -- TypeScript / JavaScript (ts_ls OR tsserver fallback) - -- ============================== - local ts_server = lspconfig.ts_ls or lspconfig.tsserver - if ts_server then - ts_server.setup({}) - end - - -- ============================== - -- Astro (guard if missing) - -- ============================== - if lspconfig.astro then - local function get_typescript_lib() - local mason_ts = vim.fs.normalize( - "~/.local/share/nvim/mason/packages/typescript-language-server/node_modules/typescript/lib" - ) - if vim.fn.isdirectory(mason_ts) == 1 then return mason_ts end - - local global_ts = (vim.fn.system("npm root -g"):gsub("\n", "")) .. "/typescript/lib" - if vim.fn.isdirectory(global_ts) == 1 then return global_ts end - - return vim.fs.normalize( - "~/.local/share/nvim/mason/packages/astro-language-server/node_modules/typescript/lib" - ) - end - - lspconfig.astro.setup({ - init_options = { typescript = { tsdk = get_typescript_lib() } }, - }) - end - + -- -- ============================== + -- -- TypeScript / JavaScript (ts_ls OR tsserver fallback) + -- -- ============================== + -- local ts_server = lspconfig.ts_ls or lspconfig.tsserver + -- if ts_server then + -- ts_server.setup({}) + -- end + -- + -- -- ============================== + -- -- Astro (guard if missing) + -- -- ============================== + -- if lspconfig.astro then + -- local function get_typescript_lib() + -- local mason_ts = vim.fs.normalize( + -- "~/.local/share/nvim/mason/packages/typescript-language-server/node_modules/typescript/lib" + -- ) + -- if vim.fn.isdirectory(mason_ts) == 1 then return mason_ts end + -- + -- local global_ts = (vim.fn.system("npm root -g"):gsub("\n", "")) .. "/typescript/lib" + -- if vim.fn.isdirectory(global_ts) == 1 then return global_ts end + -- + -- return vim.fs.normalize( + -- "~/.local/share/nvim/mason/packages/astro-language-server/node_modules/typescript/lib" + -- ) + -- end + -- + -- lspconfig.astro.setup({ + -- init_options = { typescript = { tsdk = get_typescript_lib() } }, + -- }) + -- end + -- -- ============================== -- templ (register config if missing) -- ============================== diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua index 0cb8b35d..64596f57 100644 --- a/lua/plugins/mason.lua +++ b/lua/plugins/mason.lua @@ -1,4 +1,3 @@ - -- lua/plugins/mason.lua return { { @@ -13,9 +12,9 @@ return { lazy = false, dependencies = { "williamboman/mason.nvim" }, opts = { - ensure_installed = { "gopls", "ts_ls", "templ", "astro" }, + ensure_installed = { "gopls", "templ", "vtsls" }, -- 👈 add this automatic_installation = true, - automatic_setup = false, -- IMPORTANT: don't auto-setup servers + automatic_setup = false, }, }, }