Skip to content

Commit f17dfe8

Browse files
committed
fix(definition): canonicalize function definition
1 parent 759e8fb commit f17dfe8

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<!-- Add all new changes here. They will be moved under a version at release -->
55
* `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available
66
* `FIX` support hex color codes with `#` in `textDocument/documentColor`
7+
* `FIX` resolve single definition for local functions when previously two were provided [#2451](https://github.com/LuaLS/lua-language-server/issues/2451)
78

89
## 3.14.0
910
`2025-4-7`

script/core/definition.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ return function (uri, offset)
157157
checkSee(source, results)
158158

159159
local defs = vm.getDefs(source)
160+
---@type table<parser.object, boolean>
161+
local results_set = {}
160162

161163
for _, src in ipairs(defs) do
162164
if src.type == 'global' then
@@ -218,6 +220,19 @@ return function (uri, offset)
218220
goto CONTINUE
219221
end
220222

223+
results_set[src] = true
224+
::CONTINUE::
225+
end
226+
for src, _ in pairs(results_set) do
227+
-- If the node is a child of the same line, canonicalize the definition to the parent node.
228+
if results_set[src.parent] then
229+
local parent_line = math.floor(src.parent.start / 10000)
230+
local src_line = math.floor(src.start / 10000)
231+
if parent_line == src_line then
232+
goto CONTINUE
233+
end
234+
end
235+
local root = guide.getRoot(src)
221236
results[#results+1] = {
222237
target = src,
223238
uri = root.uri,

test/definition/function.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ end
2424
]]
2525

2626
TEST [[
27-
local <!f!> = <!function () end!>
27+
local <!f!> = function () end
28+
<!f!> = function () end
29+
<!f!> = function () end
2830
<?f?>()
2931
]]

0 commit comments

Comments
 (0)