Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 16, 2021
1 parent 8b0d382 commit ab5e1b3
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 55 deletions.
3 changes: 2 additions & 1 deletion script/core/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local files = require 'files'
local vm = require 'vm'
local findSource = require 'core.find-source'
local guide = require 'parser.guide'
local rpath = require 'workspace.require-path'

local function sortResults(results)
-- 先按照顺序排序
Expand Down Expand Up @@ -74,7 +75,7 @@ local function checkRequire(source, offset)
return nil
end
if libName == 'require' then
return workspace.findUrisByRequirePath(literal)
return rpath.findUrisByRequirePath(literal)
elseif libName == 'dofile'
or libName == 'loadfile' then
return workspace.findUrisByFilePath(literal)
Expand Down
4 changes: 2 additions & 2 deletions script/core/diagnostics/different-requires.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local guide = require 'parser.guide'
local lang = require 'language'
local config = require 'config'
local vm = require 'vm'
local ws = require 'workspace'
local rpath = require 'workspace.require-path'

return function (uri, callback)
local state = files.getState(uri)
Expand All @@ -21,7 +21,7 @@ return function (uri, callback)
return
end
local literal = arg1[1]
local results = ws.findUrisByRequirePath(literal)
local results = rpath.findUrisByRequirePath(literal)
if not results or #results ~= 1 then
return
end
Expand Down
3 changes: 2 additions & 1 deletion script/core/hover/description.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ local lang = require 'language'
local util = require 'utility'
local guide = require 'parser.guide'
local noder = require 'core.noder'
local rpath = require 'workspace.require-path'

local function collectRequire(mode, literal)
local rootPath = ws.path or ''
local result, searchers
if mode == 'require' then
result, searchers = ws.findUrisByRequirePath(literal)
result, searchers = rpath.findUrisByRequirePath(literal)
elseif mode == 'dofile'
or mode == 'loadfile' then
result = ws.findUrisByFilePath(literal)
Expand Down
4 changes: 2 additions & 2 deletions script/core/searcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local noder = require 'core.noder'
local guide = require 'parser.guide'
local files = require 'files'
local generic = require 'core.generic'
local ws = require 'workspace'
local rpath = require 'workspace.require-path'
local vm = require 'vm.vm'
local collector = require 'core.collector'
local util = require 'utility'
Expand Down Expand Up @@ -796,7 +796,7 @@ function m.searchRefsByID(status, suri, expect, mode)
if not requireName then
return
end
local uris = ws.findUrisByRequirePath(requireName)
local uris = rpath.findUrisByRequirePath(requireName)
footprint(status, 'require:', requireName)
for i = 1, #uris do
local ruri = uris[i]
Expand Down
3 changes: 2 additions & 1 deletion script/core/type-definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local vm = require 'vm'
local findSource = require 'core.find-source'
local guide = require 'parser.guide'
local infer = require 'core.infer'
local rpath = require 'workspace.require-path'

local function sortResults(results)
-- 先按照顺序排序
Expand Down Expand Up @@ -75,7 +76,7 @@ local function checkRequire(source, offset)
return nil
end
if libName == 'require' then
return workspace.findUrisByRequirePath(literal)
return rpath.findUrisByRequirePath(literal)
elseif libName == 'dofile'
or libName == 'loadfile' then
return workspace.findUrisByFilePath(literal)
Expand Down
4 changes: 2 additions & 2 deletions script/vm/getLinks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local guide = require 'parser.guide'
---@class vm
local vm = require 'vm.vm'
local files = require 'files'
local rpath = require 'workspace.require-path'

local function getFileLinks(uri)
local ws = require 'workspace'
local links = {}
local state = files.getState(uri)
if not state then
Expand All @@ -20,7 +20,7 @@ local function getFileLinks(uri)
if not args or not args[1] or args[1].type ~= 'string' then
return
end
local uris = ws.findUrisByRequirePath(args[1][1])
local uris = rpath.findUrisByRequirePath(args[1][1])
for _, u in ipairs(uris) do
if not links[u] then
links[u] = {}
Expand Down
45 changes: 45 additions & 0 deletions script/workspace/require-path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,51 @@ function m.getVisiblePath(path)
return m.cache[path]
end

--- 查找符合指定require path的所有uri
---@param path string
function m.findUrisByRequirePath(path)
if type(path) ~= 'string' then
return {}
end
local vm = require 'vm'
local cache = vm.getCache 'findUrisByRequirePath'
if cache[path] then
return cache[path].results, cache[path].searchers
end
tracy.ZoneBeginN('findUrisByRequirePath')
local results = {}
local mark = {}
local searchers = {}
for uri in files.eachDll() do
local opens = files.getDllOpens(uri) or {}
for _, open in ipairs(opens) do
if open == path then
results[#results+1] = uri
end
end
end

local input = path:gsub('%.', '/')
:gsub('%%', '%%%%')
for _, luapath in ipairs(config.get 'Lua.runtime.path') do
local part = workspace.normalize(luapath:gsub('%?', input))
local uris, posts = workspace.findUrisByFilePath(part)
for _, uri in ipairs(uris) do
if not mark[uri] then
mark[uri] = true
results[#results+1] = uri
searchers[uri] = posts[uri] .. luapath
end
end
end
tracy.ZoneEnd()
cache[path] = {
results = results,
searchers = searchers,
}
return results, searchers
end

function m.flush()
m.cache = {}
end
Expand Down
45 changes: 0 additions & 45 deletions script/workspace/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,51 +429,6 @@ function m.findUrisByFilePath(path)
return results, posts
end

--- 查找符合指定require path的所有uri
---@param path string
function m.findUrisByRequirePath(path)
if type(path) ~= 'string' then
return {}
end
local vm = require 'vm'
local cache = vm.getCache 'findUrisByRequirePath'
if cache[path] then
return cache[path].results, cache[path].searchers
end
tracy.ZoneBeginN('findUrisByRequirePath')
local results = {}
local mark = {}
local searchers = {}
for uri in files.eachDll() do
local opens = files.getDllOpens(uri) or {}
for _, open in ipairs(opens) do
if open == path then
results[#results+1] = uri
end
end
end

local input = path:gsub('%.', '/')
:gsub('%%', '%%%%')
for _, luapath in ipairs(config.get 'Lua.runtime.path') do
local part = m.normalize(luapath:gsub('%?', input))
local uris, posts = m.findUrisByFilePath(part)
for _, uri in ipairs(uris) do
if not mark[uri] then
mark[uri] = true
results[#results+1] = uri
searchers[uri] = posts[uri] .. luapath
end
end
end
tracy.ZoneEnd()
cache[path] = {
results = results,
searchers = searchers,
}
return results, searchers
end

function m.normalize(path)
if not path then
return nil
Expand Down
23 changes: 22 additions & 1 deletion test/crossfile/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,10 @@ print(t.<?x?>)
}

local originRuntimePath = config.get 'Lua.runtime.path'

config.set('Lua.runtime.path', {
'./?.lua'
})

TEST {
{
path = 'a.lua',
Expand All @@ -891,4 +891,25 @@ print(t.<?x?>)
}
}

--config.set('Lua.runtime.path', {
-- '/home/?.lua'
--})
--TEST {
-- {
-- path = '/home/a.lua',
-- content = [[
--return {
-- <!x!> = 1,
--}
--]],
-- },
-- {
-- path = 'b.lua',
-- content = [[
--local t = require 'a'
--print(t.<?x?>)
-- ]]
-- }
--}

config.set('Lua.runtime.path', originRuntimePath)

0 comments on commit ab5e1b3

Please sign in to comment.