From f17dfe8c0f8cb1368ad426edf837f2ce78181ee1 Mon Sep 17 00:00:00 2001 From: Peter Cardenas <16930781+PeterCardenas@users.noreply.github.com> Date: Sun, 18 May 2025 22:43:15 -0700 Subject: [PATCH] fix(definition): canonicalize function definition --- changelog.md | 1 + script/core/definition.lua | 15 +++++++++++++++ test/definition/function.lua | 4 +++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index d929b7919..174cf625a 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ * `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available * `FIX` support hex color codes with `#` in `textDocument/documentColor` +* `FIX` resolve single definition for local functions when previously two were provided [#2451](https://github.com/LuaLS/lua-language-server/issues/2451) ## 3.14.0 `2025-4-7` diff --git a/script/core/definition.lua b/script/core/definition.lua index 75a1433f5..6141a91c2 100644 --- a/script/core/definition.lua +++ b/script/core/definition.lua @@ -157,6 +157,8 @@ return function (uri, offset) checkSee(source, results) local defs = vm.getDefs(source) + ---@type table + local results_set = {} for _, src in ipairs(defs) do if src.type == 'global' then @@ -218,6 +220,19 @@ return function (uri, offset) goto CONTINUE end + results_set[src] = true + ::CONTINUE:: + end + for src, _ in pairs(results_set) do + -- If the node is a child of the same line, canonicalize the definition to the parent node. + if results_set[src.parent] then + local parent_line = math.floor(src.parent.start / 10000) + local src_line = math.floor(src.start / 10000) + if parent_line == src_line then + goto CONTINUE + end + end + local root = guide.getRoot(src) results[#results+1] = { target = src, uri = root.uri, diff --git a/test/definition/function.lua b/test/definition/function.lua index 95dd1b57c..009e30002 100644 --- a/test/definition/function.lua +++ b/test/definition/function.lua @@ -24,6 +24,8 @@ end ]] TEST [[ -local = +local = function () end + = function () end + = function () end () ]]