diff options
-rw-r--r-- | changelog.md | 8 | ||||
-rw-r--r-- | script/core/semantic-tokens.lua | 26 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 3 | ||||
-rw-r--r-- | script/vm/compiler.lua | 12 | ||||
-rw-r--r-- | test/type_inference/init.lua | 9 |
5 files changed, 42 insertions, 16 deletions
diff --git a/changelog.md b/changelog.md index 0bed9c82..0ead8edc 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,14 @@ -- `a` and `b` is `string` here end) ``` +* `NEW` using `---@overload` as class constructor + ```lua + ---@class Class + ---@overload fun():Class + local mt + + local x = mt() --> x is `Class` here + ``` * `FIX` [#1051](https://github.com/sumneko/lua-language-server/issues/1051) * `FIX` [#1072](https://github.com/sumneko/lua-language-server/issues/1072) * `FIX` [#1077](https://github.com/sumneko/lua-language-server/issues/1077) diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index 568bb222..3b7b77ee 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -165,21 +165,17 @@ local Care = util.switch() -- 5. Class declaration -- only search this local if loc.bindDocs then - for i = #loc.bindDocs, 1, -1 do - local doc = loc.bindDocs[i] - if doc.type == 'doc.type' then - break - end - if doc.type == "doc.class" and doc.bindSources then - for _, src in ipairs(doc.bindSources) do - if src == loc then - results[#results+1] = { - start = source.start, - finish = source.finish, - type = define.TokenTypes.class, - } - return - end + local isParam = source.parent.type == 'funcargs' + or source.parent.type == 'in' + if not isParam then + for _, doc in ipairs(loc.bindDocs) do + if doc.type == 'doc.class' then + results[#results+1] = { + start = source.start, + finish = source.finish, + type = define.TokenTypes.class, + } + return end end end diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 7c669d1f..4cb8b520 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1320,7 +1320,8 @@ local function isNextLine(binded, doc) if lastDoc.type == 'doc.class' or lastDoc.type == 'doc.field' then if doc.type ~= 'doc.field' - and doc.type ~= 'doc.comment' then + and doc.type ~= 'doc.comment' + and doc.type ~= 'doc.overload' then return false end end diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index eea6e093..ef91b4b1 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -840,6 +840,18 @@ local function compileLocalBase(source) vm.setNode(source, globalMgr.getGlobal('type', 'integer')) end + if source.bindDocs then + local isParam = source.parent.type == 'funcargs' + or source.parent.type == 'in' + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.overload' then + if not isParam then + vm.setNode(source, vm.compileNode(doc)) + end + end + end + end + baseNode:merge(vm.getNode(source)) vm.removeNode(source) diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 8f4b0441..b63e03c1 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -1512,3 +1512,12 @@ local function f() end local <?x?> = f() ]] + +TEST 'AA' [[ +---@class AA +---@overload fun():AA +local AAA + + +local <?x?> = AAA() +]] |