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 /script-beta/core | |
parent | 96e499f4c62d7e444c74a4afebfd02211f3f9c16 (diff) | |
download | lua-language-server-51185264ab76b43571689dd717b3e685d4ff9c73.zip |
更新hover
Diffstat (limited to 'script-beta/core')
-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 |
3 files changed, 61 insertions, 26 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 |