diff --git a/changelog.md b/changelog.md index 9f42bd85f..e2c6c632f 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## Unreleased * `FIX` `need-check-nil` diagnostic is no longer reported on safe navigation access (e.g. `x?.field`, `f?.()`, `t?.[key]`), since the optional access itself already handles the nil check. Note that a non-safe access chained after a safe one (e.g. `x.upper()?.field`) still reports, because the safe access only protects its own result. +* `FIX` Go to definition on a table field assigned an anonymous function (e.g. `A.c = function() end`) now returns a single definition at the field name, instead of two candidates (the field name and the function value) [#2451](https://github.com/LuaLS/lua-language-server/issues/2451) ## 3.19.1 `2026-08-14` diff --git a/script/core/definition.lua b/script/core/definition.lua index 7158284f3..0176e0528 100644 --- a/script/core/definition.lua +++ b/script/core/definition.lua @@ -231,6 +231,25 @@ return function (uri, offset) return nil end + -- Drop a redundant function-value target when the owning assignment's name node is also a target. + -- Collapses `A.c = function() end` to a single definition at the name `c`. + local targetMark = {} + for _, res in ipairs(results) do + targetMark[res.target] = true + end + for i = #results, 1, -1 do + local target = results[i].target + if target.type == 'function' then + local parent = target.parent + if parent and guide.isAssign(parent) then + local owner = parent.field or parent.method or parent.index or parent.variable or parent + if targetMark[owner] then + table.remove(results, i) + end + end + end + end + sortResults(results) jumpSource(results) diff --git a/test/definition/field.lua b/test/definition/field.lua index 5b5b67f92..36f4740cf 100644 --- a/test/definition/field.lua +++ b/test/definition/field.lua @@ -21,3 +21,15 @@ local t = X print(t.x.) ]] + +TEST [[ +A = {} +A. = function() end +A.() +]] + +TEST [[ +local A = {} +A. = function() end +A.() +]] diff --git a/test/definition/function.lua b/test/definition/function.lua index 95dd1b57c..f5d32e783 100644 --- a/test/definition/function.lua +++ b/test/definition/function.lua @@ -24,6 +24,6 @@ end ]] TEST [[ -local = +local = function () end () ]]