Skip to content

fix(definition): canonicalize function definition #3184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- Add all new changes here. They will be moved under a version at release -->
* `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`
Expand Down
15 changes: 15 additions & 0 deletions script/core/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ return function (uri, offset)
checkSee(source, results)

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

for _, src in ipairs(defs) do
if src.type == 'global' then
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion test/definition/function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ end
]]

TEST [[
local <!f!> = <!function () end!>
local <!f!> = function () end
<!f!> = function () end
<!f!> = function () end
<?f?>()
]]
Loading