diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-25 09:42:19 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-25 09:42:19 +0800 |
commit | af44e932a9d73a43b5d6524712856b3e5c1ca462 (patch) | |
tree | fcc989650c533be9fda8a8ea04438a7e8a789f17 | |
parent | 15aa26dfdd8e79f297f8c781c7de1ab228099c7e (diff) | |
download | lua-language-server-af44e932a9d73a43b5d6524712856b3e5c1ca462.zip |
require列出的模块会根据相似度排序
-rw-r--r-- | server/src/matcher/completion.lua | 2 | ||||
-rw-r--r-- | server/src/workspace.lua | 33 | ||||
-rw-r--r-- | server/test/crossfile/completion.lua | 26 |
3 files changed, 52 insertions, 9 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua index 6debe930..8b309880 100644 --- a/server/src/matcher/completion.lua +++ b/server/src/matcher/completion.lua @@ -226,7 +226,7 @@ local function searchAsArg(vm, inCall, inString, callback) if not vm.lsp or not vm.lsp.workspace then return end - local results = vm.lsp.workspace:matchPath(inString[1]) + local results = vm.lsp.workspace:matchPath(vm.uri, inString[1]) if not results then return end diff --git a/server/src/workspace.lua b/server/src/workspace.lua index f87912e6..ba324aad 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -161,33 +161,50 @@ function mt:convertPathAsRequire(filename, start) return list end -function mt:matchPath(input) +function mt:matchPath(baseUri, input) input = input:lower() local first = input:match '[^%.]+' if not first then return nil end + local baseName = self:uriDecode(baseUri):string():lower() local rootLen = #self.root:string() - local results = {} + local map = {} for filename in pairs(self.files) do local start = filename:find('/' .. first, rootLen + 1, true) if start then local list = self:convertPathAsRequire(filename, start + 1) if list then for _, str in ipairs(list) do - if not results[str] and #str >= #input then - results[str] = true - results[#results+1] = str + if #str >= #input then + if not map[str] + or similarity(filename, baseName) > similarity(map[str], baseName) + then + map[str] = filename + end end end end end end - if #results == 0 then + + local list = {} + for str in pairs(map) do + list[#list+1] = str + end + if #list == 0 then return nil end - table.sort(results) - return results + table.sort(list, function (a, b) + local sa = similarity(map[a], baseName) + local sb = similarity(map[b], baseName) + if sa == sb then + return a < b + else + return sa > sb + end + end) + return list end function mt:searchPath(baseUri, str) diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index 180745cb..15b19176 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -191,3 +191,29 @@ TEST { }, } } + +TEST { + { + path = 'x000.lua', + content = '', + }, + { + path = 'abc/x111.lua', + content = '', + }, + { + path = 'abc/test.lua', + content = 'require "x@"', + main = true, + }, + completion = { + { + label = 'x111', + kind = CompletionItemKind.Module, + }, + { + label = 'x000', + kind = CompletionItemKind.Module, + }, + } +} |