Skip to content

Commit 6194e11

Browse files
committed
update semantic-tokens
1 parent 6357645 commit 6194e11

File tree

2 files changed

+68
-88
lines changed

2 files changed

+68
-88
lines changed

script/core/semantic-tokens.lua

Lines changed: 66 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,19 @@ local Care = util.switch()
9191
modifieres = modifiers,
9292
}
9393
end)
94+
: case 'local'
9495
: case 'getlocal'
9596
: case 'setlocal'
9697
: call(function (source, options, results)
97-
local loc = source.node
98+
if source.locPos then
99+
results[#results+1] = {
100+
start = source.locPos,
101+
finish = source.locPos + #'local',
102+
type = define.TokenTypes.keyword,
103+
modifieres = define.TokenModifiers.declaration,
104+
}
105+
end
106+
local loc = source.node or source
98107
-- 1. 值为函数的局部变量 | Local variable whose value is a function
99108
if loc.refs then
100109
for _, ref in ipairs(loc.refs) do
@@ -108,19 +117,27 @@ local Care = util.switch()
108117
end
109118
end
110119
end
111-
-- 2. 对象 | Object
112-
if source.parent.type == 'getmethod'
113-
or source.parent.type == 'setmethod'
114-
and source.parent.node == source then
120+
-- 3. 特殊变量 | Special variableif source[1] == '_ENV' then
121+
if loc[1] == '_ENV' then
122+
results[#results+1] = {
123+
start = source.start,
124+
finish = source.finish,
125+
type = define.TokenTypes.variable,
126+
modifieres = define.TokenModifiers.readonly,
127+
}
115128
return
116129
end
117-
-- 3. 特殊变量 | Special variable
118-
if source[1] == '_ENV'
119-
or source[1] == 'self' then
130+
if loc[1] == 'self' then
131+
results[#results+1] = {
132+
start = source.start,
133+
finish = source.finish,
134+
type = define.TokenTypes.variable,
135+
modifieres = define.TokenModifiers.abstract,
136+
}
120137
return
121138
end
122139
-- 4. 函数的参数 | Function parameters
123-
if loc.parent and loc.parent.type == 'funcargs' then
140+
if source.parent and source.parent.type == 'funcargs' then
124141
results[#results+1] = {
125142
start = source.start,
126143
finish = source.finish,
@@ -207,99 +224,57 @@ local Care = util.switch()
207224
and source.parent.node == source then
208225
return
209226
end
210-
local isLocal = loc.parent ~= guide.getRoot(loc)
227+
local mod
228+
if source.type == 'local' then
229+
mod = define.TokenModifiers.declaration
230+
end
211231
-- 8. 其他 | Other
212232
results[#results+1] = {
213233
start = source.start,
214234
finish = source.finish,
215235
type = define.TokenTypes.variable,
216-
modifieres = isLocal and define.TokenModifiers['local'] or nil,
236+
modifieres = mod,
217237
}
218238
end)
219-
: case 'local'
220-
: call(function (source, options, results) -- Local declaration, i.e. "local x", "local y = z", or "local function() end"
221-
if source[1] == '_ENV'
222-
or source[1] == 'self' then
223-
return
224-
end
225-
if source.parent and source.parent.type == 'funcargs' then
226-
results[#results+1] = {
227-
start = source.start,
228-
finish = source.finish,
229-
type = define.TokenTypes.parameter,
230-
modifieres = define.TokenModifiers.declaration,
231-
}
232-
return
233-
end
234-
if source.value then
235-
local isFunction = false
236-
237-
if options.isEnhanced then
238-
isFunction = source.value.type == 'function' or infer.hasType(source.value, 'function')
239-
else
240-
isFunction = source.value.type == 'function'
241-
end
242-
243-
if isFunction then
244-
-- Function declaration, either a new one or an alias for another one
239+
: case 'function'
240+
: case 'ifblock'
241+
: case 'elseifblock'
242+
: case 'elseblock'
243+
: case 'do'
244+
: case 'for'
245+
: case 'loop'
246+
: case 'in'
247+
: case 'while'
248+
: case 'repeat'
249+
: call(function (source, options, results)
250+
local keyword = source.keyword
251+
if keyword then
252+
for i = 1, #keyword, 2 do
245253
results[#results+1] = {
246-
start = source.start,
247-
finish = source.finish,
248-
type = define.TokenTypes['function'],
249-
modifieres = define.TokenModifiers.declaration,
254+
start = keyword[i],
255+
finish = keyword[i + 1],
256+
type = define.TokenTypes.keyword,
250257
}
251-
return
252-
end
253-
end
254-
if source.value and source.value.type == 'table' and source.bindDocs then
255-
for _, doc in ipairs(source.bindDocs) do
256-
if doc.type == "doc.class" then
257-
-- Class declaration (explicit)
258-
results[#results+1] = {
259-
start = source.start,
260-
finish = source.finish,
261-
type = define.TokenTypes.class,
262-
modifieres = define.TokenModifiers.declaration,
263-
}
264-
return
265-
end
266258
end
267259
end
268-
if source.attrs then
269-
for _, attr in ipairs(source.attrs) do
270-
local name = attr[1]
271-
if name == 'const' then
272-
results[#results+1] = {
273-
start = source.start,
274-
finish = source.finish,
275-
type = define.TokenTypes.variable,
276-
modifieres = define.TokenModifiers.declaration | define.TokenModifiers.static,
277-
}
278-
return
279-
elseif name == 'close' then
280-
results[#results+1] = {
281-
start = source.start,
282-
finish = source.finish,
283-
type = define.TokenTypes.variable,
284-
modifieres = define.TokenModifiers.declaration | define.TokenModifiers.abstract,
285-
}
286-
return
287-
end
288-
end
289-
end
290-
291-
local isLocal = source.parent ~= guide.getRoot(source)
292-
local modifiers = define.TokenModifiers.declaration
293-
294-
if isLocal then
295-
modifiers = modifiers | define.TokenModifiers.definition
260+
end)
261+
: case 'if'
262+
: call(function (source, options, results)
263+
local offset = guide.positionToOffset(options.state, source.finish)
264+
if options.text:sub(offset - 2, offset) == 'end' then
265+
results[#results+1] = {
266+
start = source.finish - #'end',
267+
finish = source.finish,
268+
type = define.TokenTypes.keyword,
269+
}
296270
end
297-
271+
end)
272+
: case 'return'
273+
: call(function (source, options, results)
298274
results[#results+1] = {
299275
start = source.start,
300-
finish = source.finish,
301-
type = define.TokenTypes.variable,
302-
modifieres = modifiers,
276+
finish = source.start + #'return',
277+
type = define.TokenTypes.keyword,
303278
}
304279
end)
305280
: case 'doc.return.name'
@@ -386,6 +361,9 @@ return function (uri, start, finish)
386361
end
387362

388363
local options = {
364+
uri = uri,
365+
state = state,
366+
text = files.getText(uri),
389367
isEnhanced = config.get(uri, 'Lua.color.mode') == 'SemanticEnhanced',
390368
}
391369

script/parser/newparser.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,7 @@ local function compileExpAsAction(exp)
27962796
end
27972797

27982798
local function parseLocal()
2799+
local locPos = getPosition(Tokens[Index], 'left')
27992800
Index = Index + 2
28002801
skipSpace()
28012802
local word = peekWord()
@@ -2828,6 +2829,7 @@ local function parseLocal()
28282829
return nil
28292830
end
28302831
local loc = createLocal(name, parseLocalAttrs())
2832+
loc.locPos = locPos
28312833
loc.effect = maxinteger
28322834
pushActionIntoCurrentChunk(loc)
28332835
skipSpace()

0 commit comments

Comments
 (0)