Skip to content

Commit 5aaecf5

Browse files
committed
support ---@meta [name]
once declared `name`, user can only require this file by declared name meta file can not be required with name `_`
1 parent a6798e6 commit 5aaecf5

27 files changed

+194
-26
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `CHG` definition: supports finding definitions for `@class` and `@alias`, since they may be defined multi times
1010
* `CHG` rename: supports `@field`
1111
* `CHG` improve patch for `.luarc.json`
12+
* `CHG` `---@meta [name]`: once declared `name`, user can only require this file by declared name. meta file can not be required with name `_`
1213
* `CHG` remove telemetry
1314
* `FIX` [#831]
1415
* `FIX` [#1729]

meta/template/basic.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta _
22

33
---#DES 'arg'
44
---@type string[]

meta/template/bit.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta bit
33

44
---@version JIT
55
---@class bitlib

meta/template/bit32.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if VERSION ~= 5.2 then DISABLE() end
2-
---@meta
2+
---@meta bit32
33

44
---@version 5.2
55
---#DES 'bit32'

meta/template/builtin.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta _
22

33
---@class unknown
44
---@class any

meta/template/coroutine.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta coroutine
22

33
---#DES 'coroutine'
44
---@class coroutinelib

meta/template/debug.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta debug
22

33
---#DES 'debug'
44
---@class debuglib

meta/template/ffi.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta ffi
33

44
---@class ffi.namespace*: table
55
---@field [string] function

meta/template/io.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta io
22

33
---#DES 'io'
44
---@class iolib

meta/template/jit.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta jit
33

44
---@version JIT
55
---@class jitlib

meta/template/math.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta math
22

33
---#DES 'math'
44
---@class mathlib

meta/template/os.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta os
22

33
---#DES 'os'
44
---@class oslib

meta/template/package.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta package
22

33
---#if VERSION >=5.4 then
44
---#DES 'require>5.4'

meta/template/string.buffer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta string.buffer
33

44
---@version JIT
55
--- The string buffer library allows high-performance manipulation of string-like data.

meta/template/string.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta string
22

33
---#DES 'string'
44
---@class stringlib

meta/template/table.clear.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta table.clear
33

44
---@version JIT
55
---#DES 'table.clear'
66
---@param tab table
77
local function clear(tab) end
88

9-
return clear
9+
return clear

meta/template/table.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@meta
1+
---@meta table
22

33
---#DES 'table'
44
---@class tablelib

meta/template/table.new.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if not JIT then DISABLE() end
2-
---@meta
2+
---@meta table.new
33

44
---@version JIT
55
---#DES 'table.new'
@@ -8,4 +8,4 @@
88
---@return table
99
local function new(narray, nhash) end
1010

11-
return new
11+
return new

meta/template/utf8.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---#if VERSION <= 5.2 then DISABLE() end
2-
---@meta
2+
---@meta utf8
33

44
---@version >5.3
55
---#DES 'utf8'

script/core/semantic-tokens.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,14 @@ local Care = util.switch()
694694
type = define.TokenTypes.operator,
695695
}
696696
end)
697+
: case 'doc.meta.name'
698+
: call(function (source, options, results)
699+
results[#results+1] = {
700+
start = source.start,
701+
finish = source.finish,
702+
type = define.TokenTypes.namespace,
703+
}
704+
end)
697705

698706
---@param state table
699707
---@param results table

script/parser/guide.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ local childMap = {
178178
['doc.cast'] = {'name', '#casts'},
179179
['doc.cast.block'] = {'extends'},
180180
['doc.operator'] = {'op', 'exp', 'extends'},
181+
['doc.meta'] = {'name'},
181182
}
182183

183184
---@type table<string, fun(obj: parser.object, list: parser.object[])>

script/parser/luadoc.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,10 @@ local docSwitch = util.switch()
11161116
end)
11171117
: case 'meta'
11181118
: call(function ()
1119+
local requireName = parseName('doc.meta.name')
11191120
return {
11201121
type = 'doc.meta',
1122+
name = requireName,
11211123
start = getFinish(),
11221124
finish = getFinish(),
11231125
}

script/vm/doc.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,38 @@ function vm.isMetaFile(uri)
4444
for _, doc in ipairs(status.ast.docs) do
4545
if doc.type == 'doc.meta' then
4646
cache.isMeta = true
47+
cache.metaName = doc.name
4748
return true
4849
end
4950
end
5051
return false
5152
end
5253

54+
---@param uri uri
55+
---@return string?
56+
function vm.getMetaName(uri)
57+
if not vm.isMetaFile(uri) then
58+
return nil
59+
end
60+
local cache = files.getCache(uri)
61+
if not cache then
62+
return nil
63+
end
64+
if not cache.metaName then
65+
return nil
66+
end
67+
return cache.metaName[1]
68+
end
69+
70+
---@param uri uri
71+
---@return boolean
72+
function vm.isMetaFileRequireable(uri)
73+
if not vm.isMetaFile(uri) then
74+
return false
75+
end
76+
return vm.getMetaName(uri) ~= '_'
77+
end
78+
5379
---@param doc parser.object
5480
---@return table<string, boolean>?
5581
function vm.getValidVersions(doc)

script/workspace/require-path.lua

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@ end
5959
---@param path string
6060
---@return require-manager.visibleResult[]
6161
function mt:getRequireResultByPath(path)
62+
local vm = require 'vm'
6263
local uri = furi.encode(path)
64+
local result = {}
65+
if vm.isMetaFile(uri) then
66+
local metaName = vm.getMetaName(uri)
67+
if metaName then
68+
if vm.isMetaFileRequireable(uri) then
69+
result[#result+1] = {
70+
name = metaName,
71+
}
72+
end
73+
return result
74+
end
75+
end
6376
local searchers = config.get(self.scp.uri, 'Lua.runtime.path')
6477
local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
6578
local libUri = files.getLibraryUri(self.scp.uri, uri)
6679
local libraryPath = libUri and furi.decode(libUri)
67-
local result = {}
6880
for _, searcher in ipairs(searchers) do
6981
local isAbsolute = searcher:match '^[/\\]'
7082
or searcher:match '^%a+%:'
@@ -157,22 +169,39 @@ end
157169
--- 查找符合指定require name的所有uri
158170
---@param name string
159171
---@return uri[]
160-
---@return table<uri, string>
172+
---@return table<uri, string>?
161173
function mt:searchUrisByRequireName(name)
174+
local vm = require 'vm'
162175
local searchers = config.get(self.scp.uri, 'Lua.runtime.path')
163176
local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
164177
local separator = config.get(self.scp.uri, 'Lua.completion.requireSeparator')
165178
local path = name:gsub('%' .. separator, '/')
166179
local results = {}
167180
local searcherMap = {}
181+
local excludes = {}
182+
183+
for uri in files.eachFile(self.scp.uri) do
184+
if vm.isMetaFileRequireable(uri) then
185+
local metaName = vm.getMetaName(uri)
186+
if metaName == name then
187+
results[#results+1] = uri
188+
return results
189+
end
190+
if metaName then
191+
excludes[uri] = true
192+
end
193+
end
194+
end
168195

169196
for _, searcher in ipairs(searchers) do
170197
local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%')))
171198
fspath = workspace.normalize(fspath)
172199
local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '')
173200
for uri in files.eachFile(self.scp.uri) do
174201
if not searcherMap[uri]
175-
and util.stringEndWith(uri, tail) then
202+
and not excludes[uri]
203+
and util.stringEndWith(uri, tail)
204+
and (not vm.isMetaFile(uri) or vm.isMetaFileRequireable(uri)) then
176205
local parentUri = files.getLibraryUri(self.scp.uri, uri) or self.scp.uri
177206
if parentUri == nil or parentUri == '' then
178207
parentUri = furi.encode '/'
@@ -223,7 +252,7 @@ function mt:findUrisByRequireName(suri, name)
223252
for _, uri in ipairs(cache.results) do
224253
if uri ~= suri then
225254
results[#results+1] = uri
226-
searcherMap[uri] = cache.searcherMap[uri]
255+
searcherMap[uri] = cache.searcherMap and cache.searcherMap[uri]
227256
end
228257
end
229258
return results, searcherMap
@@ -242,6 +271,8 @@ end
242271

243272
---@param uri uri
244273
---@param name string
274+
---@return uri[]
275+
---@return table<uri, string>?
245276
function m.findUrisByRequireName(uri, name)
246277
local scp = scope.getScope(uri)
247278
---@type require-manager

test/completion/common.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3538,7 +3538,7 @@ TEST [[
35383538
require '<??>'
35393539
]]
35403540
(function (results)
3541-
assert(#results == 11, require 'utility'.dump(results))
3541+
assert(#results == 9, require 'utility'.dump(results))
35423542
end)
35433543

35443544
TEST [[

test/crossfile/completion.lua

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local define = require 'proto.define'
1010
rawset(_G, 'TEST', true)
1111

1212
local CompletionItemKind = define.CompletionItemKind
13+
local NeedRemoveMeta = false
1314

1415
local EXISTS = {}
1516

@@ -87,7 +88,9 @@ function TEST(data)
8788
assert(result ~= nil)
8889
result.complete = nil
8990
result.enableCommon = nil
90-
removeMetas(result)
91+
if NeedRemoveMeta then
92+
removeMetas(result)
93+
end
9194
for _, item in ipairs(result) do
9295
if item.id then
9396
local r = core.resolve(item.id)
@@ -439,6 +442,51 @@ TEST {
439442

440443
config.set(nil, 'Lua.runtime.path', originRuntimePath)
441444

445+
TEST {
446+
{
447+
path = 'abc.lua',
448+
content = '---@meta _',
449+
},
450+
{
451+
path = 'test.lua',
452+
content = 'require "a<??>"',
453+
main = true,
454+
},
455+
completion = nil
456+
}
457+
458+
TEST {
459+
{
460+
path = 'abc.lua',
461+
content = '---@meta xxxxx',
462+
},
463+
{
464+
path = 'test.lua',
465+
content = 'require "a<??>"',
466+
main = true,
467+
},
468+
completion = nil
469+
}
470+
471+
TEST {
472+
{
473+
path = 'abc.lua',
474+
content = '---@meta xxxxx',
475+
},
476+
{
477+
path = 'test.lua',
478+
content = 'require "xx<??>"',
479+
main = true,
480+
},
481+
completion = {
482+
{
483+
label = 'xxxxx',
484+
kind = CompletionItemKind.File,
485+
textEdit = EXISTS,
486+
}
487+
}
488+
}
489+
442490
TEST {
443491
{
444492
path = 'a.lua',
@@ -561,6 +609,8 @@ TEST {
561609
completion = nil,
562610
}
563611

612+
NeedRemoveMeta = true
613+
564614
TEST {
565615
{ path = 'f/a.lua' },
566616
{ path = 'f/b.lua' },

0 commit comments

Comments
 (0)