diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-13 18:22:25 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-13 18:22:25 +0800 |
commit | 0354b01b082384279945c6ef02be8f297b3e9919 (patch) | |
tree | dc978663da73550efe2bcb861a7906def3819e7a | |
parent | 426b30ed25c597554169474ff26543b8ac500652 (diff) | |
download | lua-language-server-0354b01b082384279945c6ef02be8f297b3e9919.zip |
自动完成require路径
-rw-r--r-- | script-beta/core/completion.lua | 48 | ||||
-rw-r--r-- | script-beta/workspace/require-path.lua | 27 | ||||
-rw-r--r-- | script-beta/workspace/workspace.lua | 5 | ||||
-rw-r--r-- | test-beta/crossfile/completion.lua | 13 | ||||
-rw-r--r-- | test-beta/crossfile/hover.lua | 2 |
5 files changed, 81 insertions, 14 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index 3152a793..3e272f72 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -17,6 +17,8 @@ local await = require 'await' local parser = require 'parser' local keyWordMap = require 'core.keyword' local workspace = require 'workspace' +local furi = require 'file-uri' +local rpath = require 'workspace.require-path' local stackID = 0 local resolveID = 0 @@ -457,7 +459,8 @@ local function isAfterLocal(text, start) end local function checkUri(ast, text, offset, results) - local uris = guide.eachSourceContain(ast.ast, offset, function (source) + local collect = {} + guide.eachSourceContain(ast.ast, offset, function (source) if source.type ~= 'string' then return end @@ -476,17 +479,48 @@ local function checkUri(ast, text, offset, results) return end if lib.name == 'require' then - return workspace.findUrisByRequirePath(literal, false) + for uri in files.eachFile() do + local path = workspace.getRelativePath(uri) + local infos = rpath.getVisiblePath(path, config.config.runtime.path) + for _, info in ipairs(infos) do + if matchKey(literal, info.expect) then + if not collect[info.expect] then + collect[info.expect] = { + textEdit = { + start = source.start + #source[2], + finish = source.finish - #source[2], + } + } + end + -- TODO 翻译 + collect[info.expect][#collect[info.expect]+1] = ([=[* (%s)[%s] (假设搜索路径包含 `%s`)]=]):format( + path, + files.getOriginUri(uri), + info.searcher + ) + end + end + end elseif lib.name == 'dofile' or lib.name == 'loadfile' then return workspace.findUrisByFilePath(literal, false) end end) - if not uris then - return - end - for _, uri in ipairs(uris) do - + for label, infos in util.sortPairs(collect) do + local mark = {} + local des = {} + for _, info in ipairs(infos) do + if not mark[info] then + mark[info] = true + des[#des+1] = info + end + end + results[#results+1] = { + label = label, + kind = ckind.Reference, + description = table.concat(des, '\n'), + textEdit = infos.textEdit, + } end end diff --git a/script-beta/workspace/require-path.lua b/script-beta/workspace/require-path.lua index c21f598a..283e0112 100644 --- a/script-beta/workspace/require-path.lua +++ b/script-beta/workspace/require-path.lua @@ -22,11 +22,34 @@ local function getOnePath(path, searcher) end function m.getVisiblePath(path, searchers) + path = path:gsub('^[/\\]+', '') if not m.cache[path] then local result = {} m.cache[path] = result - for _, searcher in ipairs(searchers) do - end + local pos = 1 + repeat + local cutedPath = path:sub(pos) + local head + if pos > 1 then + head = path:sub(1, pos - 1) + end + pos = path:match('[/\\]+()', pos) + for _, searcher in ipairs(searchers) do + local expect = getOnePath(cutedPath, searcher) + if expect then + if head then + searcher = head .. searcher + end + result[#result+1] = { + searcher = searcher, + expect = expect, + } + end + end + if not pos then + break + end + until not pos end return m.cache[path] end diff --git a/script-beta/workspace/workspace.lua b/script-beta/workspace/workspace.lua index 59a42f2b..85221aca 100644 --- a/script-beta/workspace/workspace.lua +++ b/script-beta/workspace/workspace.lua @@ -7,6 +7,7 @@ local glob = require 'glob' local platform = require 'bee.platform' local await = require 'await' local diagnostic = require 'provider.diagnostic' +local rpath = require 'workspace.require-path' local m = {} m.type = 'workspace' @@ -193,12 +194,14 @@ end function m.getRelativePath(uri) local path = furi.decode(uri) - return fs.relative(fs.path(path), fs.path(m.path)):string() + local relative = fs.relative(fs.path(path), fs.path(m.path)):string() + return relative:gsub('^[/\\]+', '') end function m.reload() m.preloadVersion = m.preloadVersion + 1 files.removeAll() + rpath.flush() await.create(function () await.setDelayer(function () return m.preloadVersion diff --git a/test-beta/crossfile/completion.lua b/test-beta/crossfile/completion.lua index 8ac3ac48..b20f652c 100644 --- a/test-beta/crossfile/completion.lua +++ b/test-beta/crossfile/completion.lua @@ -126,21 +126,28 @@ TEST { }, completion = { { + label = 'aaa', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { label = 'abc', kind = CompletionItemKind.Reference, - documentation = 'abc.lua', textEdit = EXISTS, }, { label = 'abc.aaa', kind = CompletionItemKind.Reference, - documentation = 'abc/aaa.lua', textEdit = EXISTS, }, { label = 'abcde', kind = CompletionItemKind.Reference, - documentation = 'xxx/abcde.lua', + textEdit = EXISTS, + }, + { + label = 'xxx.abcde', + kind = CompletionItemKind.Reference, textEdit = EXISTS, }, } diff --git a/test-beta/crossfile/hover.lua b/test-beta/crossfile/hover.lua index b7881618..d73aeb7f 100644 --- a/test-beta/crossfile/hover.lua +++ b/test-beta/crossfile/hover.lua @@ -85,7 +85,7 @@ TEST { }, hover = { label = '1 个字节', - description = [[[a.lua](file:///a.lua)]], + description = [[* [a.lua](file:///a.lua)]], } } |