Skip to content

Commit aace084

Browse files
committed
fix: improve textDocument/definition ordering
1 parent aaf1624 commit aace084

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# changelog
22

33
## Unreleased
4-
<!-- Add all new changes here. They will be moved under a version at release -->
4+
* `FIX` Improved ordering of results for `textDocument/definition`.
55

66
## 3.10.3
77
`2024-8-8`

script/core/definition.lua

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,73 @@ local rpath = require 'workspace.require-path'
77
local jumpSource = require 'core.jump-source'
88
local wssymbol = require 'core.workspace-symbol'
99

10-
local function sortResults(results)
10+
--- @param s string
11+
--- @return string[]
12+
local function split(s)
13+
local r = {}
14+
s:gsub('[^/]+', function (w)
15+
r[#r+1] = w:gsub("~1", "/"):gsub("~0", "~")
16+
end)
17+
return r
18+
end
19+
20+
--- Returns the Levenshtein distance between the two given string arrays
21+
--- @param a string[]
22+
--- @param b string[]
23+
--- @return number
24+
local function levenshtein_distance(a, b)
25+
local a_len, b_len = #a, #b
26+
local matrix = {} --- @type integer[][]
27+
28+
-- Initialize the matrix
29+
for i = 1, a_len + 1 do
30+
matrix[i] = { [1] = i }
31+
end
32+
33+
for j = 1, b_len + 1 do
34+
matrix[1][j] = j
35+
end
36+
37+
-- Compute the Levenshtein distance
38+
for i = 1, a_len do
39+
for j = 1, b_len do
40+
local cost = (a[i] == b[j]) and 0 or 1
41+
matrix[i + 1][j + 1] =
42+
math.min(matrix[i][j + 1] + 1, matrix[i + 1][j] + 1, matrix[i][j] + cost)
43+
end
44+
end
45+
46+
-- Return the Levenshtein distance
47+
return matrix[a_len + 1][b_len + 1]
48+
end
49+
50+
--- @param path1 string
51+
--- @param path2 string
52+
--- @return number
53+
local function path_similarity_ratio(path1, path2)
54+
local parts1 = split(path1)
55+
local parts2 = split(path2)
56+
local distance = levenshtein_distance(parts1, parts2)
57+
return distance * 2 / (#parts1 + #parts2)
58+
end
59+
60+
local function sortResults(results, uri)
1161
-- 先按照顺序排序
62+
-- Sort in order first
63+
local simularity_cache = {} --- @type table<string,number>
1264
table.sort(results, function (a, b)
1365
local u1 = guide.getUri(a.target)
1466
local u2 = guide.getUri(b.target)
1567
if u1 == u2 then
1668
return a.target.start < b.target.start
1769
else
18-
return u1 < u2
70+
simularity_cache[u1] = simularity_cache[u1] or path_similarity_ratio(uri, u1)
71+
simularity_cache[u2] = simularity_cache[u2] or path_similarity_ratio(uri, u2)
72+
return simularity_cache[u1] < simularity_cache[u2]
1973
end
2074
end)
2175
-- 如果2个结果处于嵌套状态,则取范围小的那个
76+
-- If two results are nested, take the one with the smaller range
2277
local lf, lu
2378
for i = #results, 1, -1 do
2479
local res = results[i].target
@@ -141,7 +196,7 @@ return function (uri, offset)
141196
local results = {}
142197
local uris = checkRequire(source)
143198
if uris then
144-
for i, uri in ipairs(uris) do
199+
for _, uri in ipairs(uris) do
145200
results[#results+1] = {
146201
uri = uri,
147202
source = source,
@@ -230,7 +285,7 @@ return function (uri, offset)
230285
return nil
231286
end
232287

233-
sortResults(results)
288+
sortResults(results, uri)
234289
jumpSource(results)
235290

236291
return results

0 commit comments

Comments
 (0)