diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-10-30 15:32:11 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-10-30 15:32:11 +0800 |
commit | aca5d65499c3af0fe0d73cd7ce69e7de2248b1e8 (patch) | |
tree | 19c2e6d7cb9e699aaa3e1438c4e12c44d430dbe3 | |
parent | 705e882703b837e44b1c6689d1fd90c7778f2c0f (diff) | |
download | lua-language-server-aca5d65499c3af0fe0d73cd7ce69e7de2248b1e8.zip |
优化 table 的hover性能
-rw-r--r-- | script-beta/core/hover/label.lua | 8 | ||||
-rw-r--r-- | script-beta/core/hover/name.lua | 4 | ||||
-rw-r--r-- | script-beta/core/hover/table.lua | 75 | ||||
-rw-r--r-- | script-beta/parser/guide.lua | 8 | ||||
-rw-r--r-- | script-beta/vm/getClass.lua | 2 | ||||
-rw-r--r-- | test-beta/hover/init.lua | 8 | ||||
-rw-r--r-- | test-beta/type_inference/init.lua | 2 |
7 files changed, 61 insertions, 46 deletions
diff --git a/script-beta/core/hover/label.lua b/script-beta/core/hover/label.lua index 2c0bdcd1..be73eec2 100644 --- a/script-beta/core/hover/label.lua +++ b/script-beta/core/hover/label.lua @@ -31,10 +31,10 @@ end local function asValue(source, title) local name = buildName(source) - local infers = vm.getInfers(source) - local type = vm.getInferType(source) - local class = vm.getClass(source) - local literal = vm.getInferLiteral(source) + local infers = vm.getInfers(source, 'deep') + local type = vm.getInferType(source, 'deep') + local class = vm.getClass(source, 'deep') + local literal = vm.getInferLiteral(source, 'deep') local cont if vm.hasInferType(source, 'table', 'deep') then cont = buildTable(source) diff --git a/script-beta/core/hover/name.lua b/script-beta/core/hover/name.lua index c30cf554..4d5b015c 100644 --- a/script-beta/core/hover/name.lua +++ b/script-beta/core/hover/name.lua @@ -17,14 +17,14 @@ local function asLocal(source) end local function asMethod(source) - local class = vm.getClass(source.node) + local class = vm.getClass(source.node, 'deep') local node = class or guide.getName(source.node) or '?' local method = guide.getName(source) return ('%s:%s'):format(node, method) end local function asField(source) - local class = vm.getClass(source.node) + local class = vm.getClass(source.node, 'deep') local node = class or guide.getName(source.node) or '?' local method = guide.getName(source) return ('%s.%s'):format(node, method) diff --git a/script-beta/core/hover/table.lua b/script-beta/core/hover/table.lua index fbe765b2..87c3b3e1 100644 --- a/script-beta/core/hover/table.lua +++ b/script-beta/core/hover/table.lua @@ -36,20 +36,7 @@ local function getKey(src) return ('[%s]'):format(key) end -local function getField(src, marker) - if src.type == 'table' - or src.type == 'function' then - return nil - end - if src.parent then - if src.parent.type == 'tableindex' - or src.parent.type == 'setindex' - or src.parent.type == 'getindex' then - if src.parent.index == src then - src = src.parent - end - end - end +local function getFieldFast(src) local value = guide.getObjectValue(src) or src if not value then return 'any' @@ -81,16 +68,48 @@ local function getField(src, marker) end return value.type, util.viewLiteral(literal) end - marker() - local tp = vm.getInferType(value) - local class = vm.getClass(value) - local literal = vm.getInferLiteral(value) +end + +local function getFieldFull(src) + local tp = vm.getInferType(src) + local class = vm.getClass(src) + local literal = vm.getInferLiteral(src) if type(literal) == 'string' and #literal >= 50 then literal = literal:sub(1, 47) .. '...' end return class or tp, literal end +local function getField(src, timeUp, mark, key) + if src.type == 'table' + or src.type == 'function' then + return nil + end + if src.parent then + if src.parent.type == 'tableindex' + or src.parent.type == 'setindex' + or src.parent.type == 'getindex' then + if src.parent.index == src then + src = src.parent + end + end + end + local tp, literal + tp, literal = getFieldFast(src) + if tp then + return tp, literal + end + if timeUp or mark[key] then + return nil + end + mark[key] = true + tp, literal = getFieldFull(src) + if tp then + return tp, literal + end + return nil +end + local function buildAsHash(classes, literals) local keys = {} for k in pairs(classes) do @@ -213,27 +232,21 @@ return function (source) if not key then goto CONTINUE end - if mark[key] then - goto CONTINUE - end if not classes[key] then classes[key] = {} end if not literals[key] then literals[key] = {} end - if TEST or os.clock() - clock <= 5 then - local class, literal = getField(src, function () - mark[key] = true - end) - if literal == 'nil' then - literal = nil - end - classes[key][#classes[key]+1] = class - literals[key][#literals[key]+1] = literal - else + if not TEST and os.clock() - clock > 5 then timeUp = true end + local class, literal = getField(src, timeUp, mark, key) + if literal == 'nil' then + literal = nil + end + classes[key][#classes[key]+1] = class + literals[key][#literals[key]+1] = literal ::CONTINUE:: end diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua index 7144ebbe..e98a5d4d 100644 --- a/script-beta/parser/guide.lua +++ b/script-beta/parser/guide.lua @@ -19,13 +19,13 @@ local select = select local osClock = os.clock local DEVELOP = _G.DEVELOP local log = log -local debug = debug +local _G = _G local function logWarn(...) log.warn(...) end -_ENV = nil +local _ENV = nil local m = {} @@ -3529,7 +3529,9 @@ function m.searchInfer(status, obj) return end - m.inferByDef(status, obj) + if status.deep then + m.inferByDef(status, obj) + end m.inferBySet(status, obj) m.inferByCall(status, obj) m.inferByGetTable(status, obj) diff --git a/script-beta/vm/getClass.lua b/script-beta/vm/getClass.lua index 1948922d..bd7f6f7c 100644 --- a/script-beta/vm/getClass.lua +++ b/script-beta/vm/getClass.lua @@ -30,7 +30,7 @@ local function getClass(source, classes, depth, deep) return end local value = guide.getObjectValue(source) or source - if deep and value == source then + if not deep then if value and value.type == 'string' then classes[#classes+1] = value[1] end diff --git a/test-beta/hover/init.lua b/test-beta/hover/init.lua index 0e9a9009..78e53bc7 100644 --- a/test-beta/hover/init.lua +++ b/test-beta/hover/init.lua @@ -181,7 +181,7 @@ local <?obj?> = setmetatable({}, mt) ]] [[ local obj: class { - __index: class, + __index: table, name: string = "class", } ]] @@ -196,7 +196,7 @@ local <?obj?> = setmetatable({}, mt) [[ local obj: class { TYPE: string = "class", - __index: class, + __index: table, } ]] @@ -210,7 +210,7 @@ local <?obj?> = setmetatable({}, mt) [[ local obj: class { Class: string = "class", - __index: class, + __index: table, } ]] @@ -487,7 +487,7 @@ local <?self?> = setmetatable({ ]] [[ local self: obj { - __index: obj, + __index: table, __name: string = "obj", id: integer = 1, remove: function, diff --git a/test-beta/type_inference/init.lua b/test-beta/type_inference/init.lua index 867293f8..6f500f39 100644 --- a/test-beta/type_inference/init.lua +++ b/test-beta/type_inference/init.lua @@ -30,7 +30,7 @@ function TEST(wanted) files.setText('', newScript) local source = getSource(pos) assert(source) - local result = vm.getInferType(source) + local result = vm.getInferType(source, 'deep') assert(wanted == result) end end |