diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-12-05 17:20:53 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-12-05 17:20:53 +0800 |
commit | 51185264ab76b43571689dd717b3e685d4ff9c73 (patch) | |
tree | 1fe952e64d6bdef81f280eafdfbe3d27e022ca5c | |
parent | 96e499f4c62d7e444c74a4afebfd02211f3f9c16 (diff) | |
download | lua-language-server-51185264ab76b43571689dd717b3e685d4ff9c73.zip |
更新hover
-rw-r--r-- | script-beta/core/hover/init.lua | 3 | ||||
-rw-r--r-- | script-beta/core/hover/label.lua | 17 | ||||
-rw-r--r-- | script-beta/core/hover/table.lua | 67 | ||||
-rw-r--r-- | script-beta/vm/getValue.lua | 47 | ||||
-rw-r--r-- | test-beta/hover/init.lua | 50 |
5 files changed, 124 insertions, 60 deletions
diff --git a/script-beta/core/hover/init.lua b/script-beta/core/hover/init.lua index ae4df936..fd8654e3 100644 --- a/script-beta/core/hover/init.lua +++ b/script-beta/core/hover/init.lua @@ -48,7 +48,8 @@ return function (uri, offset) or source.type == 'setglobal' or source.type == 'getglobal' or source.type == 'field' - or source.type == 'method' then + or source.type == 'method' + or source.type == 'string' then return getHover(source) end end) diff --git a/script-beta/core/hover/label.lua b/script-beta/core/hover/label.lua index e9b987e1..799ad662 100644 --- a/script-beta/core/hover/label.lua +++ b/script-beta/core/hover/label.lua @@ -89,6 +89,21 @@ local function asLibrary(source) end end +local function asString(source) + local str = source[1] + if type(str) ~= 'string' then + return '' + end + local len = #str + local charLen = utf8.len(str, 1, -1, true) + -- TODO 翻译 + if len == charLen then + return ('%d 个字节'):format(len) + else + return ('%d 个字节,%d 个字符'):format(len, charLen) + end +end + return function (source, caller) if source.type == 'function' then return asFunction(source, caller) @@ -107,5 +122,7 @@ return function (source, caller) or source.type == 'field' or source.type == 'method' then return asField(source) + elseif source.type == 'string' then + return asString(source) end end diff --git a/script-beta/core/hover/table.lua b/script-beta/core/hover/table.lua index 466d572b..f625d973 100644 --- a/script-beta/core/hover/table.lua +++ b/script-beta/core/hover/table.lua @@ -18,37 +18,54 @@ local function getField(info) local class = getClass(info.source) local literal = vm.getLiteral(info.source) local key = getKey(info) - local label - if literal then - label = ('%s: %s = %s'):format(key, class or type, util.viewLiteral(literal)) - else - label = ('%s: %s'):format(key, class or type) + return key, class or type, literal +end + +local function mergeLiteral(a, b) + if not a then + return b + end + local view = util.viewLiteral(a) + if not b then + return { view, [view] = true } + end + if b[view] then + return b end - return label, key + b[view] = true + b[#b+1] = view + return b end return function (source) - local fields = {} - local keys = {} + local literals = {} + local classes = {} vm.eachField(source, function (info) - local field, key = getField(info) - fields[#fields+1] = field - keys[field] = key + local key, class, literal = getField(info) + classes[key] = vm.mergeTypeViews(class, classes[key]) + literals[key] = mergeLiteral(literal, literals[key]) end) - local fieldsBuf - if #fields == 0 then - fieldsBuf = '{}' - else - table.sort(fields, function (a, b) - return keys[a] < keys[b] - end) - local lines = {} - lines[#lines+1] = '{' - for _, field in ipairs(fields) do - lines[#lines+1] = ' ' .. field .. ',' + if not next(classes) then + return '{}' + end + local keys = {} + for k in pairs(classes) do + keys[#keys+1] = k + end + table.sort(keys) + local lines = {} + lines[#lines+1] = '{' + for _, key in ipairs(keys) do + local class = classes[key] + local literal = literals[key] + if literal then + table.sort(literal) + local view = table.concat(literal, '|') + lines[#lines+1] = (' %s: %s = %s,'):format(key, class, view) + else + lines[#lines+1] = (' %s: %s,'):format(key, class) end - lines[#lines+1] = '}' - fieldsBuf = table.concat(lines, '\n') end - return fieldsBuf + lines[#lines+1] = '}' + return table.concat(lines, '\n') end diff --git a/script-beta/vm/getValue.lua b/script-beta/vm/getValue.lua index 67954360..f4b1ff68 100644 --- a/script-beta/vm/getValue.lua +++ b/script-beta/vm/getValue.lua @@ -868,21 +868,7 @@ function vm.hasType(source, type) return false end -function vm.viewType(values) - if not values then - return 'any' - end - if type(values) ~= 'table' then - return values or 'any' - end - local types = {} - for i = 1, #values do - local tp = values[i].type - if not types[tp] then - types[tp] = true - types[#types+1] = tp - end - end +local function viewTypes(types) if #types == 0 then return 'any' end @@ -909,6 +895,37 @@ function vm.viewType(values) return table.concat(types, '|') end +function vm.viewType(values) + if not values then + return 'any' + end + if type(values) ~= 'table' then + return values or 'any' + end + local types = {} + for i = 1, #values do + local tp = values[i].type + if not types[tp] and tp ~= 'any' then + types[tp] = true + types[#types+1] = tp + end + end + return viewTypes(types) +end + +function vm.mergeTypeViews(...) + local types = {} + for _, view in ipairs {...} do + for tp in view:gmatch '[^|]+' do + if not types[tp] and tp ~= 'any' then + types[tp] = true + types[#types+1] = tp + end + end + end + return viewTypes(types) +end + function vm.getType(source) local values = vm.getValue(source) return vm.viewType(values) diff --git a/test-beta/hover/init.lua b/test-beta/hover/init.lua index 28f5a0d4..a8bdd3d3 100644 --- a/test-beta/hover/init.lua +++ b/test-beta/hover/init.lua @@ -317,13 +317,14 @@ function mt:add(a: any, b: any) TEST [[ local <?t?> = - 1000 ]] -[[local t: number = -1000]] +[[local t: integer = -1000]] -TEST [[ -for <?c?> in io.lines() do -end -]] -[[local c: string]] +-- TODO 暂不支持 +--TEST [[ +--for <?c?> in io.lines() do +--end +--]] +--[[local c: string]] TEST [[ local function f() @@ -347,9 +348,9 @@ local n: any ]] TEST [[ -(<?'xxx'?>):sub() +local s = <?'abc中文'?> ]] -(nil) +[[9 个字节,5 个字符]] TEST [[ local <?t?> = { @@ -360,9 +361,20 @@ local <?t?> = { ]] [[ local t: { - a: number = 1, - b: number = 2, - c: number = 3, + a: integer = 1, + b: integer = 2, + c: integer = 3, +} +]] + +TEST [[ +local <?t?> = {} +t.a = 1 +t.a = true +]] +[[ +local t: { + a: boolean|integer = 1|true, } ]] @@ -380,14 +392,14 @@ local <?t?> = { ]] [[ local t: { - ["012"]: number = 8, - [*function]: number = 6, - [*table]: number = 5, - [001]: number = 2, - [5.5]: number = 4, - [true]: number = 3, - a: number = 1, - b: number = 7, + ["012"]: integer = 8, + [*function]: integer = 6, + [*table]: integer = 5, + [001]: integer = 2, + [5.5]: integer = 4, + [true]: integer = 3, + a: integer = 1, + b: integer = 7, } ]] |