diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-05 17:07:02 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-05 17:07:02 +0800 |
commit | 2434cef378cd2a5748356c427cf843a61e925ad9 (patch) | |
tree | 04f6a4b7897776fe8cb323b81a2ce18d64f6ad49 /server | |
parent | a9507cd6a329d2be66035ca2019ded070fc7ad33 (diff) | |
download | lua-language-server-2434cef378cd2a5748356c427cf843a61e925ad9.zip |
hover支持表
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/hover.lua | 103 | ||||
-rw-r--r-- | server/src/core/hover_function.lua | 2 | ||||
-rw-r--r-- | server/src/core/hover_name.lua | 4 | ||||
-rw-r--r-- | server/src/vm/value.lua | 2 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 2 | ||||
-rw-r--r-- | server/test/example/vm.lua | 2 | ||||
-rw-r--r-- | server/test/hover/init.lua | 2 |
7 files changed, 50 insertions, 67 deletions
diff --git a/server/src/core/hover.lua b/server/src/core/hover.lua index a18ba00b..b328ac00 100644 --- a/server/src/core/hover.lua +++ b/server/src/core/hover.lua @@ -15,12 +15,12 @@ local OriginTypes = { ['function'] = true, } -local function buildLibArgs(lib, oo, select) +local function buildLibArgs(lib, object, select) if not lib.args then return '' end local start - if oo then + if object then start = 2 if select then select = select + 1 @@ -190,8 +190,8 @@ local function buildEnum(lib) return table.concat(strs) end -local function getFunctionHoverAsLib(name, lib, oo, select) - local args, argLabel = buildLibArgs(lib, oo, select) +local function getFunctionHoverAsLib(name, lib, object, select) + local args, argLabel = buildLibArgs(lib, object, select) local returns = buildLibReturns(lib) local enum = buildEnum(lib) local tip = lib.description @@ -259,9 +259,9 @@ local function findClass(value) end) end -local function unpackTable(result) +local function unpackTable(value) local lines = {} - result.value:eachField(function (key, field) + value:eachChild(function (key, child) local kType = type(key) if kType == 'table' then key = ('[*%s]'):format(key:getType()) @@ -271,36 +271,40 @@ local function unpackTable(result) key = ('[%s]'):format(key) end - local value = field.value - if not value then - local str = (' %s: %s,'):format(key, 'any') - if str == ' [*any]: any,' then - goto CONTINUE - end - lines[#lines+1] = str - goto CONTINUE - end - - local vType = type(value:getValue()) - if vType == 'boolean' or vType == 'integer' or vType == 'number' or vType == 'string' then - local str = (' %s: %s = %q,'):format(key, value:getType(), value:getValue()) - lines[#lines+1] = str + local vType = type(child:getLiteral()) + if vType == 'boolean' + or vType == 'integer' + or vType == 'number' + or vType == 'string' + then + lines[#lines+1] = ('%s: %s = %q,'):format(key, child:getType(), child:getLiteral()) else - local str = (' %s: %s,'):format(key, value:getType()) - if str == ' [*any]: any,' then - goto CONTINUE - end - lines[#lines+1] = str + lines[#lines+1] = ('%s: %s,'):format(key, child:getType()) end - ::CONTINUE:: end) if #lines == 0 then return '{}' end - table.sort(lines) - table.insert(lines, 1, '{') - lines[#lines+1] = '}' - return table.concat(lines, '\r\n') + + -- 整理一下表 + local cleaned = {} + local used = {} + for _, line in ipairs(lines) do + if used[line] then + goto CONTINUE + end + used[line] = true + if line == '[*any]: any' then + goto CONTINUE + end + cleaned[#cleaned+1] = ' ' .. line + :: CONTINUE :: + end + + table.sort(cleaned) + table.insert(cleaned, 1, '{') + cleaned[#cleaned+1] = '}' + return table.concat(cleaned, '\r\n') end local function getValueHover(source, name, value, lib) @@ -320,8 +324,8 @@ local function getValueHover(source, name, value, lib) local tip local literal if lib then - --value = lib.code or (lib.value and ('%q'):format(lib.value)) - --tip = lib.description + value = lib.code or (lib.value and ('%q'):format(lib.value)) + tip = lib.description else literal = value:getLiteral() and ('%q'):format(value:getLiteral()) end @@ -378,9 +382,11 @@ local function hoverAsValue(source, lsp, select) local hover if value:getType() == 'function' then + local object = source:get 'object' if lib then + hover = getFunctionHoverAsLib(name, lib, object, select) else - hover = getFunctionHover(name, value:getFunction(), source:get 'object', select) + hover = getFunctionHover(name, value:getFunction(), object, select) end else hover = getValueHover(source, name, value, lib) @@ -393,38 +399,13 @@ local function hoverAsValue(source, lsp, select) return hover end -local function hoverAsVar(result, source, lsp, select) - local lib, fullKey = findLib(result) - local valueType = lib and lib.type - if valueType then - if type(valueType) == 'table' then - valueType = valueType[1] - end - else - valueType = result.value:getType() or 'nil' - end - local name = fullKey or buildValueName(result, source) - local hover - if valueType == 'function' then - if lib then - hover = getFunctionHoverAsLib(name, lib, oo, select) - else - hover = getFunctionHover(name, result.value, source.object, select) - end - else - hover = getValueHover(name, valueType, result, lib) - end - if not hover then - return - end - hover.name = name - return hover -end - return function (source, lsp, select) if not source then return nil end + if source.type ~= 'name' then + return + end if source:bindValue() then return hoverAsValue(source, lsp, select) end diff --git a/server/src/core/hover_function.lua b/server/src/core/hover_function.lua index 1b313f55..6be90b06 100644 --- a/server/src/core/hover_function.lua +++ b/server/src/core/hover_function.lua @@ -26,7 +26,7 @@ local function buildValueArgs(func, object, select) end end local max - if func.built then + if func.source then max = #names else max = math.max(#names, #values) diff --git a/server/src/core/hover_name.lua b/server/src/core/hover_name.lua index 485ce69a..4faab3f1 100644 --- a/server/src/core/hover_name.lua +++ b/server/src/core/hover_name.lua @@ -26,6 +26,8 @@ return function (source) chars[i] = ':' elseif obj.type == '.' then chars[i] = '.' + else + chars[i] = '*' .. obj.type end if obj == declarat then break @@ -51,6 +53,8 @@ return function (source) chars[i] = ':' elseif obj.type == '.' then chars[i] = '.' + else + chars[i] = '*' .. obj.type end end -- 这里有个特殊处理 diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index a151adc3..6684c691 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -223,11 +223,9 @@ end function mt:setFunction(func) self._func = func - self:setType('function', 1.0) end function mt:getFunction() - self:setType('function', 0.5) return self._func end diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index c147439a..7168d0eb 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -306,6 +306,7 @@ end function mt:call(value, values, source) local lib = value:getLib() local func = value:getFunction() + value:setType('function', 0.5) if not func then return end @@ -972,6 +973,7 @@ function mt:createFunction(source) local value = createValue('function', source) local func = createFunction(source) value:setFunction(func) + value:setType('function', 1.0) if source:getUri() == self.uri then self.funcs[#self.funcs+1] = func end diff --git a/server/test/example/vm.lua b/server/test/example/vm.lua index eb882710..f2f4144d 100644 --- a/server/test/example/vm.lua +++ b/server/test/example/vm.lua @@ -408,8 +408,6 @@ function mt:buildFunction(exp, object) return func end - func.built = true - self:scopePush(exp) self.chunk:push() self.chunk:cut 'dots' diff --git a/server/test/hover/init.lua b/server/test/hover/init.lua index e5686c9e..642b8216 100644 --- a/server/test/hover/init.lua +++ b/server/test/hover/init.lua @@ -234,7 +234,7 @@ TEST [[ for <?c?> in io.lines() do end ]] -[[local c: string]] +[[local c: number]] TEST [[ local function f() |