diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-12 21:15:27 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-12 21:15:27 +0800 |
commit | d76d63f7063376852ee9c8ea6795abde09149851 (patch) | |
tree | 5067140e9d592e5e401fe448186592026e59a133 /server | |
parent | 3ce89009f9e37193a00aa3eefb09f46eaff409c7 (diff) | |
download | lua-language-server-d76d63f7063376852ee9c8ea6795abde09149851.zip |
非函数的hover
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/hover.lua | 32 | ||||
-rw-r--r-- | server/src/matcher/vm.lua | 11 | ||||
-rw-r--r-- | server/test/hover/init.lua | 57 |
3 files changed, 88 insertions, 12 deletions
diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua index 750cf778..d4d9d04b 100644 --- a/server/src/matcher/hover.lua +++ b/server/src/matcher/hover.lua @@ -270,9 +270,41 @@ local function buildValueFunctionHover(result, source) ]]):format(title) end +local function buildValueSimpleHover(result, source) + local type = result.value.type + if type == 'nil' then + type = 'any' + end + local resType = result.type + if resType == 'field' then + local field = result + local stack = 0 + while field.parent do + field = field.parent + stack = stack + 1 + end + if field.value.ENV then + if stack > 1 then + resType = 'global field' + else + resType = 'global' + end + else + resType = 'local field' + end + end + return ([[ +```lua +%s: %s +``` +]]):format(resType, type) +end + local function getValueHover(result, source) if result.value.type == 'function' then return buildValueFunctionHover(result, source) + else + return buildValueSimpleHover(result, source) end end diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index 16034eaf..4936fc0f 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -539,9 +539,14 @@ function mt:getLibValue(lib, parentType, v) end function mt:getName(name, source) - local var = self.scope.locals[name] - or self:getField(self:getValue(self.scope.locals._ENV), name, source) - return var + local loc = self.scope.locals[name] + if loc then + return loc + end + local ENV = self.scope.locals._ENV + local global = self:getField(self:getValue(ENV), name, source) + global.parent = ENV + return global end function mt:getIndex(obj) diff --git a/server/test/hover/init.lua b/server/test/hover/init.lua index 55931339..e3ad7be2 100644 --- a/server/test/hover/init.lua +++ b/server/test/hover/init.lua @@ -4,27 +4,34 @@ local matcher = require 'matcher' rawset(_G, 'TEST', true) function TEST(script) - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script) - local vm = matcher.vm(ast) - assert(vm) - local result = matcher.hover(vm, pos) - assert(result) + return function (expect) + local start = script:find('<?', 1, true) + local finish = script:find('?>', 1, true) + local pos = (start + finish) // 2 + 1 + local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + local ast = parser:ast(new_script) + local vm = matcher.vm(ast) + assert(vm) + local result = matcher.hover(vm, pos) + assert(result) + expect = expect:gsub('^[\r\n]*(.-)[\r\n]*$', '%1') + result = result:gsub('```lua', ''):gsub('```', ''):gsub('^[\r\n]*(.-)[\r\n]*$', '%1') + assert(expect == result) + end end TEST [[ local function <?x?>(a, b) end ]] +"function x(a: any, b: any)" TEST [[ local function x(a, b) end <?x?>() ]] +"function x(a: any, b: any)" TEST [[ local mt = {} @@ -38,6 +45,10 @@ local obj = setmetatable({}, mt) obj:<?init?>(1, '测试') ]] +[[ +function mt:init(a: number, b: string, c: any) + -> table +]] TEST [[ local mt = {} @@ -52,6 +63,10 @@ local obj = setmetatable({}, mt) obj:init(1, '测试') obj.<?init?>(obj, 1, '测试') ]] +[[ +function mt.init(self: table, a: number, b: string, c: any) + -> table +]] TEST [[ function obj.xxx() @@ -59,7 +74,31 @@ end obj.<?xxx?>() ]] +"function obj.xxx()" TEST [[ obj.<?xxx?>() ]] +"function obj.xxx()" + +TEST [[ +local <?x?> = 1 +]] +"local: number" + +TEST [[ +<?x?> = 1 +]] +"global: number" + +TEST [[ +local t = {} +t.<?x?> = 1 +]] +"local field: number" + +TEST [[ +t = {} +t.<?x?> = 1 +]] +"global field: number" |