diff options
Diffstat (limited to 'script-beta')
-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 |
3 files changed, 70 insertions, 10 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 |