Skip to content

Commit bc92c4d

Browse files
committed
add doc.type.code
1 parent f8f9b68 commit bc92c4d

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

script/core/semantic-tokens.lua

+9
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ local Care = util.switch()
675675
type = define.TokenTypes.variable,
676676
}
677677
end)
678+
: case 'doc.type.code'
679+
: call(function (source, options, results)
680+
results[#results+1] = {
681+
start = source.start,
682+
finish = source.finish,
683+
type = define.TokenTypes.string,
684+
modifieres = define.TokenModifiers.abstract,
685+
}
686+
end)
678687

679688
local function buildTokens(uri, results)
680689
local tokens = {}

script/parser/luadoc.lua

+35-10
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ local Parser = re.compile([[
1212
Main <- (Token / Sp)*
1313
Sp <- %s+
1414
X16 <- [a-fA-F0-9]
15-
Token <- Integer / Name / String / Symbol
15+
Token <- Integer / Name / String / Code / Symbol
1616
Name <- ({} {%name} {})
1717
-> Name
1818
Integer <- ({} {[0-9]+} !'.' {})
1919
-> Integer
20+
Code <- ({} '`' { (!'`' .)*} '`' {})
21+
-> Code
2022
String <- ({} StringDef {})
2123
-> String
2224
StringDef <- {'"'}
@@ -48,7 +50,7 @@ EChar <- 'a' -> ea
4850
/ ([0-9] [0-9]? [0-9]?) -> Char10
4951
/ ('u{' {X16*} '}') -> CharUtf8
5052
Symbol <- ({} {
51-
[:|,<>()?+#`{}]
53+
[:|,<>()?+#{}]
5254
/ '[]'
5355
/ '...'
5456
/ '['
@@ -114,6 +116,13 @@ Symbol <- ({} {
114116
TokenFinishs[Ci] = finish - 1
115117
TokenContents[Ci] = math.tointeger(content)
116118
end,
119+
Code = function (start, content, finish)
120+
Ci = Ci + 1
121+
TokenTypes[Ci] = 'code'
122+
TokenStarts[Ci] = start
123+
TokenFinishs[Ci] = finish - 1
124+
TokenContents[Ci] = content
125+
end,
117126
Symbol = function (start, content, finish)
118127
Ci = Ci + 1
119128
TokenTypes[Ci] = 'symbol'
@@ -534,6 +543,22 @@ local function parseString(parent)
534543
return str
535544
end
536545

546+
local function parseCode(parent)
547+
local tp, content = peekToken()
548+
if not tp or tp ~= 'code' then
549+
return nil
550+
end
551+
nextToken()
552+
local code = {
553+
type = 'doc.type.code',
554+
start = getStart(),
555+
finish = getFinish(),
556+
parent = parent,
557+
[1] = content,
558+
}
559+
return code
560+
end
561+
537562
local function parseInteger(parent)
538563
local tp, content = peekToken()
539564
if not tp or tp ~= 'integer' then
@@ -584,23 +609,16 @@ function parseTypeUnit(parent)
584609
local result = parseFunction(parent)
585610
or parseTable(parent)
586611
or parseString(parent)
612+
or parseCode(parent)
587613
or parseInteger(parent)
588614
or parseBoolean(parent)
589615
or parseDots('doc.type.name', parent)
590616
or parseParen(parent)
591617
if not result then
592-
local literal = checkToken('symbol', '`', 1)
593-
if literal then
594-
nextToken()
595-
end
596618
result = parseName('doc.type.name', parent)
597619
if not result then
598620
return nil
599621
end
600-
if literal then
601-
result.literal = true
602-
nextSymbolOrError '`'
603-
end
604622
end
605623
while true do
606624
local newResult = parseTypeUnitSign(parent, result)
@@ -1418,6 +1436,13 @@ local function bindGeneric(binded)
14181436
src.type = 'doc.generic.name'
14191437
end
14201438
end)
1439+
guide.eachSourceType(doc, 'doc.type.code', function (src)
1440+
local name = src[1]
1441+
if generics[name] then
1442+
src.type = 'doc.generic.name'
1443+
src.literal = true
1444+
end
1445+
end)
14211446
end
14221447
end
14231448
end

script/vm/compiler.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,7 @@ local compilerSwitch = util.switch()
12901290
: case 'doc.type.integer'
12911291
: case 'doc.type.string'
12921292
: case 'doc.type.boolean'
1293+
: case 'doc.type.code'
12931294
: call(function (source)
12941295
vm.setNode(source, source)
12951296
end)

script/vm/infer.lua

+4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ local viewNodeSwitch = util.switch()
135135
: call(function (source, infer)
136136
return ('%q'):format(source[1])
137137
end)
138+
: case 'doc.type.code'
139+
: call(function (source, infer)
140+
return ('`%s`'):format(source[1])
141+
end)
138142
: case 'doc.type.function'
139143
: call(function (source, infer, uri)
140144
infer._hasDocFunction = true

test/hover/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -1921,3 +1921,11 @@ end
19211921
[[
19221922
(upvalue) x: unknown
19231923
]]
1924+
1925+
TEST [[
1926+
---@type `123 ????` | ` x | y `
1927+
local <?x?>
1928+
]]
1929+
[[
1930+
local x: ` x | y `|`123 ????`
1931+
]]

test/type_inference/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -2370,3 +2370,8 @@ TEST 'boolean|table' [[
23702370
---@type tp
23712371
local <?x?>
23722372
]]
2373+
2374+
TEST '`1`|`true`' [[
2375+
---@type `1` | `true`
2376+
local <?x?>
2377+
]]

0 commit comments

Comments
 (0)