summaryrefslogtreecommitdiff
path: root/script/workspace/require-path.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-01-16 21:06:06 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-01-16 21:06:06 +0800
commit5aaecf51b9fccddf52ed3e4749757a03819fae61 (patch)
tree87ceb62c6eb926cb54d8ba794697f0d3ddd0b1e3 /script/workspace/require-path.lua
parenta6798e6adb81da6b7f833bc39da0a6368178a090 (diff)
downloadlua-language-server-5aaecf51b9fccddf52ed3e4749757a03819fae61.zip
support `---@meta [name]`
once declared `name`, user can only require this file by declared name meta file can not be required with name `_`
Diffstat (limited to 'script/workspace/require-path.lua')
-rw-r--r--script/workspace/require-path.lua39
1 files changed, 35 insertions, 4 deletions
diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua
index f65af868..d7ca8e26 100644
--- a/script/workspace/require-path.lua
+++ b/script/workspace/require-path.lua
@@ -59,12 +59,24 @@ end
---@param path string
---@return require-manager.visibleResult[]
function mt:getRequireResultByPath(path)
+ local vm = require 'vm'
local uri = furi.encode(path)
+ local result = {}
+ if vm.isMetaFile(uri) then
+ local metaName = vm.getMetaName(uri)
+ if metaName then
+ if vm.isMetaFileRequireable(uri) then
+ result[#result+1] = {
+ name = metaName,
+ }
+ end
+ return result
+ end
+ end
local searchers = config.get(self.scp.uri, 'Lua.runtime.path')
local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
local libUri = files.getLibraryUri(self.scp.uri, uri)
local libraryPath = libUri and furi.decode(libUri)
- local result = {}
for _, searcher in ipairs(searchers) do
local isAbsolute = searcher:match '^[/\\]'
or searcher:match '^%a+%:'
@@ -157,14 +169,29 @@ end
--- 查找符合指定require name的所有uri
---@param name string
---@return uri[]
----@return table<uri, string>
+---@return table<uri, string>?
function mt:searchUrisByRequireName(name)
+ local vm = require 'vm'
local searchers = config.get(self.scp.uri, 'Lua.runtime.path')
local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
local separator = config.get(self.scp.uri, 'Lua.completion.requireSeparator')
local path = name:gsub('%' .. separator, '/')
local results = {}
local searcherMap = {}
+ local excludes = {}
+
+ for uri in files.eachFile(self.scp.uri) do
+ if vm.isMetaFileRequireable(uri) then
+ local metaName = vm.getMetaName(uri)
+ if metaName == name then
+ results[#results+1] = uri
+ return results
+ end
+ if metaName then
+ excludes[uri] = true
+ end
+ end
+ end
for _, searcher in ipairs(searchers) do
local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%')))
@@ -172,7 +199,9 @@ function mt:searchUrisByRequireName(name)
local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '')
for uri in files.eachFile(self.scp.uri) do
if not searcherMap[uri]
- and util.stringEndWith(uri, tail) then
+ and not excludes[uri]
+ and util.stringEndWith(uri, tail)
+ and (not vm.isMetaFile(uri) or vm.isMetaFileRequireable(uri)) then
local parentUri = files.getLibraryUri(self.scp.uri, uri) or self.scp.uri
if parentUri == nil or parentUri == '' then
parentUri = furi.encode '/'
@@ -223,7 +252,7 @@ function mt:findUrisByRequireName(suri, name)
for _, uri in ipairs(cache.results) do
if uri ~= suri then
results[#results+1] = uri
- searcherMap[uri] = cache.searcherMap[uri]
+ searcherMap[uri] = cache.searcherMap and cache.searcherMap[uri]
end
end
return results, searcherMap
@@ -242,6 +271,8 @@ end
---@param uri uri
---@param name string
+---@return uri[]
+---@return table<uri, string>?
function m.findUrisByRequireName(uri, name)
local scp = scope.getScope(uri)
---@type require-manager