diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-21 10:37:06 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-21 10:37:06 +0800 |
commit | 9711dcfcafceca6426e650a99f9d580ba6be58c1 (patch) | |
tree | 61e57de6e5793b6150b0463d5e76a676809a7a05 /server | |
parent | 8254afb1d359e8db10883f56f1474c7a7a7e3779 (diff) | |
download | lua-language-server-9711dcfcafceca6426e650a99f9d580ba6be58c1.zip |
会优先找相似路径的文件
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/vm.lua | 4 | ||||
-rw-r--r-- | server/src/workspace.lua | 44 |
2 files changed, 41 insertions, 7 deletions
diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index b04089af..50de4041 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -1228,7 +1228,7 @@ function mt:loadRequires() for value, strValue in pairs(copy) do local str = strValue.value if type(str) == 'string' then - local uri = self.lsp.workspace:searchPath(str) + local uri = self.lsp.workspace:searchPath(self.uri, str) -- 如果循环require,这里会返回nil -- 会当场编译VM local destVM = self.lsp:loadVM(uri) @@ -1246,7 +1246,7 @@ function mt:tryLoadRequires() for value, strValue in pairs(self.requires) do local str = strValue.value if type(str) == 'string' then - local uri = self.lsp.workspace:searchPath(str) + local uri = self.lsp.workspace:searchPath(self.uri, str) -- 如果取不到VM(不编译),则做个标记,之后再取一次 local destVM = self.lsp:getVM(uri) if destVM then diff --git a/server/src/workspace.lua b/server/src/workspace.lua index 81afeadd..76b5af51 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -45,6 +45,25 @@ local function uriEncode(path) return 'file:///' .. table.concat(names, '/') end +local function split(str, sep) + local t = {} + for s in str:gmatch('[^' .. sep .. ']+') do + t[#t+1] = s + end + return t +end + +local function similarity(a, b) + local ta = split(a, '/\\') + local tb = split(b, '/\\') + for i = 1, #ta do + if ta[i] ~= tb[i] then + return i - 1 + end + end + return #ta +end + local mt = {} mt.__index = mt @@ -93,7 +112,7 @@ function mt:removeFile(uri) self.files[name] = nil end -function mt:searchPath(str) +function mt:searchPath(baseUri, str) if self.loaded[str] then return self.loaded[str] end @@ -102,16 +121,31 @@ function mt:searchPath(str) for i, luapath in ipairs(self.luapath) do searchers[i] = luapath:gsub('%?', str):lower() end + + local results = {} for filename, uri in pairs(self.files) do for _, searcher in ipairs(searchers) do if filename:sub(-#searcher) == searcher then - self.loaded[str] = uri - self.lsp:readText(uri, fs.path(filename)) - return uri + results[#results+1] = uri end end end - return nil + + if #results == 0 then + return nil + end + local uri + if #results == 1 then + uri = results[1] + else + table.sort(results, function (a, b) + return similarity(a, baseUri) > similarity(b, baseUri) + end) + uri = results[1] + end + self.loaded[str] = uri + self.lsp:readText(uri, uriDecode(uri)) + return uri end function mt:reset() |