diff options
-rw-r--r-- | script/core/completion.lua | 46 | ||||
-rw-r--r-- | test/completion/init.lua | 42 |
2 files changed, 54 insertions, 34 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua index 8bba81b2..ccbe401e 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -101,6 +101,14 @@ local function findSymbol(text, offset) return nil end +local function findNearestSource(ast, offset) + local source + guide.eachSourceContain(ast.ast, offset, function (src) + source = src + end) + return source +end + local function findTargetSymbol(text, offset, symbol) offset = skipSpace(text, offset) for i = offset, 1, -1 do @@ -1094,35 +1102,13 @@ local function checkEqualEnum(ast, text, offset, results) eqOrNeq = true end start = skipSpace(text, start - 1) - local source = guide.eachSourceContain(ast.ast, start, function (source) - if source.finish ~= start then - return - end - if source.type == 'getlocal' - or source.type == 'setlocal' - or source.type == 'local' - or source.type == 'getglobal' - or source.type == 'setglobal' - or source.type == 'getfield' - or source.type == 'setfield' - or source.type == 'getindex' - or source.type == 'setindex' - or source.type == 'tablefield' - or source.type == 'tableindex' - or source.type == 'call' then - return source - end - local parent = source.parent - if parent then - if parent.type == 'tablefield' - or parent.type == 'tableindex' then - return source - end - end - end) + local source = findNearestSource(ast, start) if not source then return end + if source.type == 'callargs' then + source = source.parent + end if source.type == 'call' and not eqOrNeq then return end @@ -1347,14 +1333,6 @@ local function getCallArgInfo(call, text, offset) return #call.args + 1, nil end -local function findNearestSource(ast, offset) - local source - guide.eachSourceContain(ast.ast, offset, function (src) - source = src - end) - return source -end - local function getFuncParamByCallIndex(func, index) if not func.args or #func.args == 0 then return nil diff --git a/test/completion/init.lua b/test/completion/init.lua index 2c1a4570..bd3aca25 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -2139,6 +2139,27 @@ x.a = $ TEST [[ ---@type table<string, "'a'"|"'b'"|"'c'"> +local x + +x['a'] = $ +]] +{ + { + label = "'a'", + kind = define.CompletionItemKind.EnumMember, + }, + { + label = "'b'", + kind = define.CompletionItemKind.EnumMember, + }, + { + label = "'c'", + kind = define.CompletionItemKind.EnumMember, + }, +} + +TEST [[ +---@type table<string, "'a'"|"'b'"|"'c'"> local x = { a = $ } @@ -2157,3 +2178,24 @@ local x = { kind = define.CompletionItemKind.EnumMember, }, } + +TEST [[ +---@type table<string, "'a'"|"'b'"|"'c'"> +local x = { + ['a'] = $ +} +]] +{ + { + label = "'a'", + kind = define.CompletionItemKind.EnumMember, + }, + { + label = "'b'", + kind = define.CompletionItemKind.EnumMember, + }, + { + label = "'c'", + kind = define.CompletionItemKind.EnumMember, + }, +} |