diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 63 | ||||
-rw-r--r-- | test/completion/common.lua | 16 |
3 files changed, 49 insertions, 32 deletions
diff --git a/changelog.md b/changelog.md index f6546af8..7b6af194 100644 --- a/changelog.md +++ b/changelog.md @@ -113,6 +113,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. } } ``` +* `CHG` [#1533] supports completion with table field of function * `FIX` [#1567] * `FIX` [#1593] * `FIX` [#1595] @@ -131,6 +132,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. [#1344]: https://github.com/sumneko/lua-language-server/issues/1344 [#1458]: https://github.com/sumneko/lua-language-server/issues/1458 [#1484]: https://github.com/sumneko/lua-language-server/issues/1484 +[#1533]: https://github.com/sumneko/lua-language-server/issues/1533 [#1557]: https://github.com/sumneko/lua-language-server/issues/1557 [#1558]: https://github.com/sumneko/lua-language-server/issues/1558 [#1561]: https://github.com/sumneko/lua-language-server/issues/1561 diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 0f85bec5..5b92a3c1 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -1199,6 +1199,17 @@ local function insertDocEnum(state, pos, doc, enums) return enums end +local function buildInsertDocFunction(doc) + local args = {} + for i, arg in ipairs(doc.args) do + args[i] = ('${%d:%s}'):format(i, arg.name[1]) + end + return ("\z +function (%s)\ +\t$0\ +end"):format(table.concat(args, ', ')) +end + ---@param state parser.state ---@param pos integer ---@param src vm.node.object @@ -1220,6 +1231,26 @@ local function insertEnum(state, pos, src, enums, isInArray) description = src.comment, kind = define.CompletionItemKind.EnumMember, } + elseif src.type == 'doc.type.function' then + ---@cast src parser.object + local insertText = buildInsertDocFunction(src) + local description + if src.comment then + description = src.comment + else + local descText = insertText:gsub('%$%{%d+:([^}]+)%}', function (val) + return val + end):gsub('%$%{?%d+%}?', '') + description = markdown() + : add('lua', descText) + : string() + end + enums[#enums+1] = { + label = vm.getInfer(src):view(state.uri), + description = description, + kind = define.CompletionItemKind.Function, + insertText = insertText, + } elseif isInArray and src.type == 'doc.type.array' then for i, d in ipairs(vm.getDefs(src.node)) do insertEnum(state, pos, d, enums, isInArray) @@ -1433,17 +1464,6 @@ local function trySymbol(state, position, results) end end -local function buildInsertDocFunction(doc) - local args = {} - for i, arg in ipairs(doc.args) do - args[i] = ('${%d:%s}'):format(i, arg.name[1]) - end - return ("\z -function (%s)\ -\t$0\ -end"):format(table.concat(args, ', ')) -end - local function findCall(state, position) local call guide.eachSourceContain(state.ast, position, function (src) @@ -1534,27 +1554,6 @@ local function tryCallArg(state, position, results) local enums = {} for src in node:eachObject() do insertEnum(state, position, src, enums, arg and arg.type == 'table') - if src.type == 'doc.type.function' then - ---@cast src parser.object - local insertText = buildInsertDocFunction(src) - local description - if src.comment then - description = src.comment - else - local descText = insertText:gsub('%$%{%d+:([^}]+)%}', function (val) - return val - end):gsub('%$%{?%d+%}?', '') - description = markdown() - : add('lua', descText) - : string() - end - enums[#enums+1] = { - label = vm.getInfer(src):view(state.uri), - description = description, - kind = define.CompletionItemKind.Function, - insertText = insertText, - } - end end cleanEnums(enums, arg) for _, enum in ipairs(enums) do diff --git a/test/completion/common.lua b/test/completion/common.lua index 2438618e..dab31c31 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -3980,3 +3980,19 @@ TEST [[ } }, } + +TEST [[ +---@class A +---@field f fun(x: string): string + +---@type A +local t = { + f = <??> +} +]] +{ + { + label = 'fun(x: string):string', + kind = define.CompletionItemKind.Function, + } +} |