diff --git a/3rd/EmmyLuaCodeStyle b/3rd/EmmyLuaCodeStyle index abdea2d41..22c131005 160000 --- a/3rd/EmmyLuaCodeStyle +++ b/3rd/EmmyLuaCodeStyle @@ -1 +1 @@ -Subproject commit abdea2d412437e2f12a38e8179cd18af61c4429a +Subproject commit 22c1310055fafeda6a331e924438f88d0608e6d3 diff --git a/3rd/bee.lua b/3rd/bee.lua index 8c01c7d79..6bddb34b4 160000 --- a/3rd/bee.lua +++ b/3rd/bee.lua @@ -1 +1 @@ -Subproject commit 8c01c7d79612d47f47f17d80304e66ae14d7b953 +Subproject commit 6bddb34b4f076bcb6207c53454e65568c173ca10 diff --git a/3rd/json.lua b/3rd/json.lua index 21c9584d3..bd7b7787b 160000 --- a/3rd/json.lua +++ b/3rd/json.lua @@ -1 +1 @@ -Subproject commit 21c9584d30fa36c542c98b6b1410393318583712 +Subproject commit bd7b7787bb8b586e59b5afe5886dd1b76c86eb56 diff --git a/3rd/love-api b/3rd/love-api index 853639288..04d21fc58 160000 --- a/3rd/love-api +++ b/3rd/love-api @@ -1 +1 @@ -Subproject commit 853639288547618dece86c3a8e52348fe304eba2 +Subproject commit 04d21fc58e5e025373862a4383d55d4dcb6fdb9c diff --git a/3rd/lovr-api b/3rd/lovr-api index e89c753e1..0cc2dc0cd 160000 --- a/3rd/lovr-api +++ b/3rd/lovr-api @@ -1 +1 @@ -Subproject commit e89c753e1c2849b7533481fcf058095f8e050b9f +Subproject commit 0cc2dc0cd6eefd640f5d27eecc8d211a489b7613 diff --git a/3rd/luamake b/3rd/luamake index c086f35cf..295950da7 160000 --- a/3rd/luamake +++ b/3rd/luamake @@ -1 +1 @@ -Subproject commit c086f35cfad0236f74ba380d51f211c52a2c8abc +Subproject commit 295950da763e2bf75956c56fdc7c8c2877aac4ad diff --git a/changelog.md b/changelog.md index 096b644ab..7e5d6aa37 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `FIX` Improved ordering of results for `textDocument/definition`. ## 3.10.3 `2024-8-8` diff --git a/script/core/definition.lua b/script/core/definition.lua index 3619916e6..04176e32e 100644 --- a/script/core/definition.lua +++ b/script/core/definition.lua @@ -7,18 +7,73 @@ local rpath = require 'workspace.require-path' local jumpSource = require 'core.jump-source' local wssymbol = require 'core.workspace-symbol' -local function sortResults(results) +--- @param s string +--- @return string[] +local function split(s) + local r = {} + s:gsub('[^/]+', function (w) + r[#r+1] = w:gsub("~1", "/"):gsub("~0", "~") + end) + return r +end + +--- Returns the Levenshtein distance between the two given string arrays +--- @param a string[] +--- @param b string[] +--- @return number +local function levenshteinDistance(a, b) + local a_len, b_len = #a, #b + local matrix = {} --- @type integer[][] + + -- Initialize the matrix + for i = 1, a_len + 1 do + matrix[i] = { [1] = i } + end + + for j = 1, b_len + 1 do + matrix[1][j] = j + end + + -- Compute the Levenshtein distance + for i = 1, a_len do + for j = 1, b_len do + local cost = (a[i] == b[j]) and 0 or 1 + matrix[i + 1][j + 1] = + math.min(matrix[i][j + 1] + 1, matrix[i + 1][j] + 1, matrix[i][j] + cost) + end + end + + -- Return the Levenshtein distance + return matrix[a_len + 1][b_len + 1] +end + +--- @param path1 string +--- @param path2 string +--- @return number +local function pathSimilarityRatio(path1, path2) + local parts1 = split(path1) + local parts2 = split(path2) + local distance = levenshteinDistance(parts1, parts2) + return distance * 2 / (#parts1 + #parts2) +end + +local function sortResults(results, uri) -- 先按照顺序排序 + -- Sort in order first + local simularity_cache = {} --- @type table table.sort(results, function (a, b) local u1 = guide.getUri(a.target) local u2 = guide.getUri(b.target) if u1 == u2 then return a.target.start < b.target.start else - return u1 < u2 + simularity_cache[u1] = simularity_cache[u1] or pathSimilarityRatio(uri, u1) + simularity_cache[u2] = simularity_cache[u2] or pathSimilarityRatio(uri, u2) + return simularity_cache[u1] < simularity_cache[u2] end end) -- 如果2个结果处于嵌套状态,则取范围小的那个 + -- If two results are nested, take the one with the smaller range local lf, lu for i = #results, 1, -1 do local res = results[i].target @@ -141,7 +196,7 @@ return function (uri, offset) local results = {} local uris = checkRequire(source) if uris then - for i, uri in ipairs(uris) do + for _, uri in ipairs(uris) do results[#results+1] = { uri = uri, source = source, @@ -230,7 +285,7 @@ return function (uri, offset) return nil end - sortResults(results) + sortResults(results, uri) jumpSource(results) return results