Skip to content

Commit d3ba148

Browse files
authored
Merge pull request #302 from uhziel/diagnostic-undefined-field
提升undefined-field速度,寻找fields改为只会查找定义的 getFieldsOfDocClassAnyNotGet()
2 parents 335822c + 46274a0 commit d3ba148

File tree

4 files changed

+78
-55
lines changed

4 files changed

+78
-55
lines changed

script/core/diagnostics/undefined-field.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ return function (uri, callback)
2929
local allDocClass = {}
3030
for i = 1, #infers do
3131
local infer = infers[i]
32-
if infer.type ~= '_G' then
32+
if infer.type ~= '_G' and infer.type ~= 'any' then
3333
local inferSource = infer.source
3434
if inferSource.type == 'doc.class' then
3535
addTo(allDocClass, inferSource)
@@ -49,17 +49,13 @@ return function (uri, callback)
4949
return allDocClass
5050
end
5151

52-
---@param allDocClass table int
5352
local function getAllFieldsFromAllDocClass(allDocClass)
5453
local fields = {}
5554
local empty = true
5655
for _, docClass in ipairs(allDocClass) do
57-
local refs = vm.getFields(docClass)
56+
local refs = vm.getFieldsOfDocClassAnyNotGet(docClass)
5857

5958
for _, ref in ipairs(refs) do
60-
if ref.type == 'getfield' or ref.type == 'getmethod' then
61-
goto CONTINUE
62-
end
6359
local name = vm.getKeyName(ref)
6460
if not name or vm.getKeyType(ref) ~= 'string' then
6561
goto CONTINUE

script/parser/guide.lua

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ _ENV = nil
2929

3030
local m = {}
3131

32-
m.ANY = {}
32+
m.ANY = {"ANY"}
33+
34+
m.ANYNOTGET = {"ANYNOTGET"}
3335

3436
local blockTypes = {
3537
['while'] = true,
@@ -1246,6 +1248,52 @@ function m.isDocClass(source)
12461248
return source.type == 'doc.class'
12471249
end
12481250

1251+
function m.isSet(source)
1252+
local tp = source.type
1253+
if tp == 'setglobal'
1254+
or tp == 'local'
1255+
or tp == 'setlocal'
1256+
or tp == 'setfield'
1257+
or tp == 'setmethod'
1258+
or tp == 'setindex'
1259+
or tp == 'tablefield'
1260+
or tp == 'tableindex' then
1261+
return true
1262+
end
1263+
if tp == 'call' then
1264+
local special = m.getSpecial(source.node)
1265+
if special == 'rawset' then
1266+
return true
1267+
end
1268+
end
1269+
return false
1270+
end
1271+
1272+
function m.isGet(source)
1273+
local tp = source.type
1274+
if tp == 'getglobal'
1275+
or tp == 'getlocal'
1276+
or tp == 'getfield'
1277+
or tp == 'getmethod'
1278+
or tp == 'getindex' then
1279+
return true
1280+
end
1281+
if tp == 'call' then
1282+
local special = m.getSpecial(source.node)
1283+
if special == 'rawget' then
1284+
return true
1285+
end
1286+
end
1287+
return false
1288+
end
1289+
1290+
function m.getSpecial(source)
1291+
if not source then
1292+
return nil
1293+
end
1294+
return source.special
1295+
end
1296+
12491297
--- 根据函数的调用参数,获取:调用,参数索引
12501298
function m.getCallAndArgIndex(callarg)
12511299
local callargs = callarg.parent
@@ -2291,6 +2339,11 @@ function m.checkSameSimpleName(ref, sm)
22912339
if sm == m.ANY then
22922340
return true
22932341
end
2342+
2343+
if sm == m.ANYNOTGET and not m.isGet(ref) then
2344+
return true
2345+
end
2346+
22942347
if m.getSimpleName(ref) == sm then
22952348
return true
22962349
end
@@ -4007,10 +4060,11 @@ function m.requestDefinition(obj, interface, deep)
40074060
end
40084061

40094062
--- 请求对象的域
4010-
function m.requestFields(obj, interface, deep)
4063+
---@param filterKey nil|string|table nilfields不做限制;stringfields必须同名;table取值为guild.ANYSETfields必须满足isSet()
4064+
function m.requestFields(obj, interface, deep, filterKey)
40114065
local status = m.status(nil, interface, deep)
40124066

4013-
m.searchFields(status, obj)
4067+
m.searchFields(status, obj, filterKey)
40144068

40154069
return status.results, status.share.count
40164070
end

script/vm/eachField.lua

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local guide = require 'parser.guide'
44
local await = require 'await'
55
local config = require 'config'
66

7-
local function getFields(source, deep)
7+
local function getFields(source, deep, filterKey)
88
local unlock = vm.lock('eachField', source)
99
if not unlock then
1010
return {}
@@ -19,18 +19,19 @@ local function getFields(source, deep)
1919
deep = config.config.intelliSense.searchDepth + (deep or 0)
2020

2121
await.delay()
22-
local results = guide.requestFields(source, vm.interface, deep)
22+
local results = guide.requestFields(source, vm.interface, deep, filterKey)
2323

2424
unlock()
2525
return results
2626
end
2727

28-
local function getFieldsBySource(source, deep)
28+
local function getFieldsBySource(source, deep, filterKey)
2929
deep = deep or -999
3030
local cache = vm.getCache('eachField')[source]
3131
if not cache or cache.deep < deep then
32-
cache = getFields(source, deep)
32+
cache = getFields(source, deep, filterKey)
3333
cache.deep = deep
34+
cache.filterKey = filterKey
3435
vm.getCache('eachField')[source] = cache
3536
end
3637
return cache
@@ -46,12 +47,18 @@ function vm.getFields(source, deep)
4647
or getFieldsBySource(source, deep)
4748
vm.getCache('eachFieldOfGlobal')[name] = cache
4849
return cache
49-
elseif guide.isDocClass(source) then
50-
local cache = vm.getCache('eachFieldOfDocClass')[source]
51-
or getFieldsBySource(source, deep)
52-
vm.getCache('eachFieldOfDocClass')[source] = cache
53-
return cache
5450
else
5551
return getFieldsBySource(source, deep)
5652
end
5753
end
54+
55+
function vm.getFieldsOfDocClassAnyNotGet(source, deep)
56+
if not guide.isDocClass(source) then
57+
return {}
58+
end
59+
60+
local cache = vm.getCache('eachFieldOfDocClass')[source]
61+
or getFieldsBySource(source, deep, guide.ANYNOTGET)
62+
vm.getCache('eachFieldOfDocClass')[source] = cache
63+
return cache
64+
end

script/vm/vm.lua

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,11 @@ function m.lock(tp, source)
3939
end
4040

4141
function m.isSet(src)
42-
local tp = src.type
43-
if tp == 'setglobal'
44-
or tp == 'local'
45-
or tp == 'setlocal'
46-
or tp == 'setfield'
47-
or tp == 'setmethod'
48-
or tp == 'setindex'
49-
or tp == 'tablefield'
50-
or tp == 'tableindex' then
51-
return true
52-
end
53-
if tp == 'call' then
54-
local special = m.getSpecial(src.node)
55-
if special == 'rawset' then
56-
return true
57-
end
58-
end
59-
return false
42+
return guide.isSet(src)
6043
end
6144

6245
function m.isGet(src)
63-
local tp = src.type
64-
if tp == 'getglobal'
65-
or tp == 'getlocal'
66-
or tp == 'getfield'
67-
or tp == 'getmethod'
68-
or tp == 'getindex' then
69-
return true
70-
end
71-
if tp == 'call' then
72-
local special = m.getSpecial(src.node)
73-
if special == 'rawget' then
74-
return true
75-
end
76-
end
77-
return false
46+
return guide.isGet(src)
7847
end
7948

8049
function m.getArgInfo(source)
@@ -95,10 +64,7 @@ function m.getArgInfo(source)
9564
end
9665

9766
function m.getSpecial(source)
98-
if not source then
99-
return nil
100-
end
101-
return source.special
67+
return guide.getSpecial(source)
10268
end
10369

10470
function m.getKeyName(source)

0 commit comments

Comments
 (0)