diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-03-16 21:08:04 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-03-16 21:08:04 +0800 |
commit | 268cfa9dd50292269c4c3bcc40093b54c6727bfa (patch) | |
tree | 248ff5c87ba38e8ee6fb54ce78eda06ffc0ca859 | |
parent | c5246d2c0a4150101b97838375df20d97a2f9c56 (diff) | |
download | lua-language-server-268cfa9dd50292269c4c3bcc40093b54c6727bfa.zip |
cleanup code
-rw-r--r-- | script/core/completion.lua | 74 | ||||
-rw-r--r-- | test/completion/init.lua | 22 |
2 files changed, 62 insertions, 34 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua index 8aa02c0e..02724626 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -1284,45 +1284,14 @@ local function getFuncParamByCallIndex(func, index) return func.args[index] end -local function checkTableLiteralField(ast, text, offset, call, funcs, index, results) - local source = findNearestSource(ast, offset) - if source.type ~= 'table' - and (not source.parent or source.parent.type ~= 'table') then - return - end - if call.node and call.node.type == 'getmethod' then - index = index + 1 - end +local function checkTableLiteralField(ast, text, offset, tbl, fields, results) local mark = {} - local fields = {} - local tbl = source - if source.type ~= 'table' then - tbl = source.parent - end - if tbl.parent ~= call.args then - return - end for _, field in ipairs(vm.getDefFields(tbl, 0)) do local name = guide.getKeyName(field) if name then mark[name] = true end end - for _, func in ipairs(funcs) do - local param = getFuncParamByCallIndex(func, index) - if not param then - goto CONTINUE - end - local defs = vm.getDefFields(param, 0) - for _, field in ipairs(defs) do - local name = guide.getKeyName(field) - if name and not mark[name] then - mark[name] = true - fields[#fields+1] = field - end - end - ::CONTINUE:: - end table.sort(fields, function (a, b) return guide.getKeyName(a) < guide.getKeyName(b) end) @@ -1337,7 +1306,8 @@ local function checkTableLiteralField(ast, text, offset, call, funcs, index, res end if left then for _, field in ipairs(fields) do - if matchKey(left, guide.getKeyName(field)) then + local name = guide.getKeyName(field) + if not mark[name] and matchKey(left, guide.getKeyName(field)) then results[#results+1] = { label = guide.getKeyName(field), kind = define.CompletionItemKind.Property, @@ -1354,6 +1324,42 @@ local function checkTableLiteralField(ast, text, offset, call, funcs, index, res end end +local function checkTableLiteralFieldByCall(ast, text, offset, call, funcs, index, results) + local source = findNearestSource(ast, offset) + if source.type ~= 'table' + and (not source.parent or source.parent.type ~= 'table') then + return + end + if call.node and call.node.type == 'getmethod' then + index = index + 1 + end + local mark = {} + local fields = {} + local tbl = source + if source.type ~= 'table' then + tbl = source.parent + end + if tbl.parent ~= call.args then + return + end + for _, func in ipairs(funcs) do + local param = getFuncParamByCallIndex(func, index) + if not param then + goto CONTINUE + end + local defs = vm.getDefFields(param, 0) + for _, field in ipairs(defs) do + local name = guide.getKeyName(field) + if name and not mark[name] then + mark[name] = true + fields[#fields+1] = field + end + end + ::CONTINUE:: + end + checkTableLiteralField(ast, text, offset, tbl, fields, results) +end + local function tryCallArg(ast, text, offset, results) local call = findCall(ast, text, offset) if not call then @@ -1375,7 +1381,7 @@ local function tryCallArg(ast, text, offset, results) for _, enum in ipairs(myResults) do results[#results+1] = enum end - checkTableLiteralField(ast, text, offset, call, defs, argIndex, results) + checkTableLiteralFieldByCall(ast, text, offset, call, defs, argIndex, results) end local function getComment(ast, offset) diff --git a/test/completion/init.lua b/test/completion/init.lua index b42cd15b..89cad975 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -2373,3 +2373,25 @@ f(1, {}, {}, { kind = define.CompletionItemKind.Property, } } + +--TEST [[ +-----@class C +-----@field x number +-----@field y number +-- +-----@type C +--local t = { +-- $ +--} +-- +--]] +--{ +-- { +-- label = 'x', +-- kind = define.CompletionItemKind.Property, +-- }, +-- { +-- label = 'y', +-- kind = define.CompletionItemKind.Property, +-- } +--} |