Skip to content

Commit

Permalink
fix: improve textDocument/definition ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Aug 14, 2024
1 parent 01c6b61 commit d57f537
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 3rd/EmmyLuaCodeStyle
Submodule EmmyLuaCodeStyle updated 158 files
2 changes: 1 addition & 1 deletion 3rd/bee.lua
Submodule bee.lua updated 159 files
2 changes: 1 addition & 1 deletion 3rd/json.lua
2 changes: 1 addition & 1 deletion 3rd/lovr-api
Submodule lovr-api updated 297 files
2 changes: 1 addition & 1 deletion 3rd/luamake
Submodule luamake updated 48 files
+4 −4 .github/workflows/publish.yml
+5 −10 README.md
+1 −1 bee.lua
+1 −1 compile/install.bat
+29 −24 compile/install.sh
+1 −3 compile/msvc/find_msvc.bat
+2 −0 compile/msvc/find_winsdk.bat
+1 −1 compile/msvc/setpath.bat
+38 −40 compile/ninja/android.ninja
+38 −40 compile/ninja/freebsd.ninja
+39 −41 compile/ninja/linux.ninja
+39 −40 compile/ninja/macos.ninja
+66 −80 compile/ninja/mingw.ninja
+75 −90 compile/ninja/msvc.ninja
+41 −43 compile/ninja/netbsd.ninja
+38 −40 compile/ninja/openbsd.ninja
+1 −1 main.lua
+10 −11 make.lua
+22 −23 scripts/action.lua
+1 −7 scripts/command/shell.lua
+1 −6 scripts/compiler/clang.lua
+0 −1 scripts/compiler/emcc.lua
+10 −10 scripts/compiler/gcc.lua
+9 −11 scripts/compiler/msvc.lua
+0 −68 scripts/compiler/wasi.lua
+1 −1 scripts/env/find_msvc.lua
+2 −3 scripts/env/ndk.lua
+0 −2 scripts/env/wasi.lua
+0 −16 scripts/fsutil.lua
+26 −12 scripts/glob.lua
+3 −2 scripts/globals.lua
+0 −6 scripts/log.lua
+5 −1 scripts/main.lua
+11 −21 scripts/ninja_writer.lua
+22 −34 scripts/pathutil.lua
+1 −1 scripts/perf.lua
+9 −6 scripts/sandbox.lua
+137 −0 scripts/simulator.lua
+1 −1 scripts/version.lua
+0 −331 scripts/workspace.lua
+418 −215 scripts/writer.lua
+11 −16 tools/lua54/lua.h
+3 −10 tools/lua54/luaconf.h
+0 −303 tools/lua55/lauxlib.h
+0 −553 tools/lua55/lua.h
+0 −9 tools/lua55/lua.hpp
+0 −802 tools/lua55/luaconf.h
+0 −65 tools/lua55/lualib.h
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## 3.10.3
`2024-8-8`
Expand Down
63 changes: 59 additions & 4 deletions script/core/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,number>
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -230,7 +285,7 @@ return function (uri, offset)
return nil
end

sortResults(results)
sortResults(results, uri)
jumpSource(results)

return results
Expand Down

0 comments on commit d57f537

Please sign in to comment.