diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-03-04 16:57:57 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-03-04 16:57:57 +0800 |
commit | 171ed306f84850be46620145358dcfdf37387de2 (patch) | |
tree | 614bf1479866b23e6663e71daf4cb8bb0b4024f9 | |
parent | 9451329b33de1e7b69f2793ac5f94715826c9895 (diff) | |
download | lua-language-server-171ed306f84850be46620145358dcfdf37387de2.zip |
update
-rw-r--r-- | script/core/definition.lua | 6 | ||||
-rw-r--r-- | script/vm/compiler.lua | 77 | ||||
-rw-r--r-- | script/vm/getDef.lua | 14 | ||||
-rw-r--r-- | test/definition/bug.lua | 77 | ||||
-rw-r--r-- | test/definition/luadoc.lua | 82 |
5 files changed, 155 insertions, 101 deletions
diff --git a/script/core/definition.lua b/script/core/definition.lua index b089bdab..687c071e 100644 --- a/script/core/definition.lua +++ b/script/core/definition.lua @@ -156,6 +156,12 @@ return function (uri, offset) goto CONTINUE end end + if src.type == 'doc.class' then + src = src.class + end + if src.type == 'doc.alias' then + src = src.alias + end if src.type == 'doc.class.name' or src.type == 'doc.alias.name' then if source.type ~= 'doc.type.name' diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 3d17cef4..84de7058 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -229,14 +229,23 @@ local function selectNode(source, list, index) end local function bindDocs(source) + local hasFounded = false local isParam = source.parent.type == 'funcargs' for _, doc in ipairs(source.bindDocs) do if doc.type == 'doc.type' then if not isParam then + hasFounded = true + m.setNode(source, m.compileNode(doc)) + end + end + if doc.type == 'doc.param' then + if isParam and source[1] == doc.param[1] then + hasFounded = true m.setNode(source, m.compileNode(doc)) end end end + return hasFounded end local compilerMap = util.switch() @@ -245,32 +254,50 @@ local compilerMap = util.switch() : case 'integer' : case 'number' : case 'string' + : case 'doc.type.function' + : case 'doc.type.table' + : call(function (source) + localMgr.declareLocal(source) + m.setNode(source, source) + end) : case 'function' : call(function (source) localMgr.declareLocal(source) m.setNode(source, source) + + if source.bindDocs then + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.overload' then + m.setNode(source, m.compileNode(doc)) + end + end + end end) : case 'local' : call(function (source) m.setNode(source, source) - if source.value then - m.setNode(source, m.compileNode(source.value)) + local hasMarkDoc + if source.bindDocs then + hasMarkDoc = bindDocs(source) end - if source.ref then + if source.ref and not hasMarkDoc then for _, ref in ipairs(source.ref) do if ref.type == 'setlocal' then m.setNode(source, m.compileNode(ref.value)) end end end - if source.dummy then + if source.dummy and not hasMarkDoc then m.setNode(source, m.compileNode(source.method.node)) end - if source.bindDocs then - bindDocs(source) + if source.value then + if not hasMarkDoc or guide.isLiteral(source.value) then + m.setNode(source, m.compileNode(source.value)) + end end -- function x.y(self, ...) --> function x:y(...) if source[1] == 'self' + and not hasMarkDoc and source.parent.type == 'funcargs' and source.parent[1] == source then local setfield = source.parent.parent.parent @@ -345,6 +372,44 @@ local compilerMap = util.switch() : call(function (source) m.setNode(source, m.compileNode(source.extends)) end) + : case 'doc.extends.name' + : call(function (source) + local name = source[1] + local uri = guide.getUri(source) + local type = globalMgr.declareGlobal('type', name, uri) + type:addGet(uri, source) + m.setNode(source, type) + end) + : case 'doc.param' + : call(function (source) + m.setNode(source, m.compileNode(source.extends)) + end) + : case 'doc.vararg' + : call(function (source) + m.setNode(source, m.compileNode(source.vararg)) + end) + : case '...' + : call(function (source) + local func = source.parent.parent + if func.type ~= 'function' then + return + end + if not func.bindDocs then + return + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.vararg' then + m.setNode(source, m.compileNode(doc)) + end + if doc.type == 'doc.param' and doc.param[1] == '...' then + m.setNode(source, m.compileNode(doc)) + end + end + end) + : case 'doc.overload' + : call(function (source) + m.setNode(source, m.compileNode(source.overload)) + end) : getMap() ---@param source parser.object diff --git a/script/vm/getDef.lua b/script/vm/getDef.lua index 8b3e610d..9ba90eb1 100644 --- a/script/vm/getDef.lua +++ b/script/vm/getDef.lua @@ -97,11 +97,7 @@ local searchFieldMap = util.switch() end end if node.cate == 'type' then - compiler.getClassFields(node, key, function (field) - if field.type == 'doc.field' then - pushResult(field.field) - end - end) + compiler.getClassFields(node, key, pushResult) end end) : case 'local' @@ -194,7 +190,13 @@ local function searchByNode(source, pushResult) return end for n in compiler.eachNode(node) do - pushResult(n) + if n.type == 'global' and n.cate == 'type' then + for _, set in ipairs(n:getSets()) do + pushResult(set) + end + else + pushResult(n) + end end end diff --git a/test/definition/bug.lua b/test/definition/bug.lua index 58c2ed59..b564f764 100644 --- a/test/definition/bug.lua +++ b/test/definition/bug.lua @@ -251,80 +251,3 @@ local <!v!> = t[a] t[a] = <?v?> ]] - -TEST [[ ----@class A ----@field x number - ----@class B: A ----@field <!x!> boolean - ----@type B -local t - -t.<?x?> -]] - -TEST [[ ----@class A ----@field <!x!> number - ----@class B: A - ----@type B -local t - -t.<?x?> -]] - -TEST [[ ----@class A -local A - -function A:x() end - ----@class B: A -local B - -<!function B:x() end!> - ----@type B -local t - -local <!<?v?>!> = t.x -]] - -TEST [[ ----@class A -local A - -<!function A:x() end!> - ----@class B: A -local B - ----@type B -local t - -local <!<?v?>!> = t.x -]] - --- TODO ---TEST [[ ------@class A ---local A --- ------@return A ---function A:x() end --- ------@class B: A ---local <!B!> --- ------@return B ---function B:x() end --- ------@type B ---local t --- ---local <!<?v?>!> = t.x() ---]] diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua index 8124261c..118eff5e 100644 --- a/test/definition/luadoc.lua +++ b/test/definition/luadoc.lua @@ -17,7 +17,7 @@ local x TEST [[ ---@class Class -local <!t!> +local t ---@type Class local <?<!x!>?> ]] @@ -35,21 +35,59 @@ obj:<?cast?>() TEST [[ ---@class A -local <!mt!> = {} -function mt:cast() -end +---@field x number ----@type A -local <!obj!> -<?obj?>:cast() +---@class B: A +---@field <!x!> boolean + +---@type B +local t + +t.<?x?> ]] TEST [[ ----@type A -local <?<!obj!>?> +---@class A +---@field <!x!> number +---@class B: A + +---@type B +local t + +t.<?x?> +]] + +TEST [[ ---@class A -local <!mt!> +local A + +function A:x() end + +---@class B: A +local B + +<!function B:x() end!> + +---@type B +local t + +local <!<?v?>!> = t.x +]] + +TEST [[ +---@class A +local A + +<!function A:x() end!> + +---@class B: A +local B + +---@type B +local t + +local <!<?v?>!> = t.x ]] TEST [[ @@ -116,11 +154,12 @@ print(<?f?>) TEST [[ local function f() - return 1 + local x + return x end ---@class Class -local <!mt!> +local mt ---@type Class local <?<!x!>?> = f() @@ -212,6 +251,25 @@ TEST [[ ]] TEST [[ +---@class A +local A + +---@return A +function A:x() end + +---@class B: A +local <!B!> + +---@return B +function B:x() end + +---@type B +local t + +local <!<?v?>!> = t.x() +]] + +TEST [[ ---@return <!fun()!> local function f() end |