diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 59 | ||||
-rw-r--r-- | test/completion/common.lua | 30 |
3 files changed, 76 insertions, 15 deletions
diff --git a/changelog.md b/changelog.md index 35b38f02..b3616a00 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,7 @@ * `FIX` [#1805] * `FIX` [#1808] * `FIX` [#1811] +* `FIX` [#1824] [#831]: https://github.com/sumneko/lua-language-server/issues/831 [#1729]: https://github.com/sumneko/lua-language-server/issues/1729 @@ -27,6 +28,7 @@ [#1805]: https://github.com/sumneko/lua-language-server/issues/1805 [#1808]: https://github.com/sumneko/lua-language-server/issues/1808 [#1811]: https://github.com/sumneko/lua-language-server/issues/1811 +[#1824]: https://github.com/sumneko/lua-language-server/issues/1824 `2022-11-29` ## 3.6.4 diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index ea5e31eb..c1b942b0 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -83,9 +83,9 @@ local function findNearestSource(state, position) return source end -local function findNearestTableField(state, position) - local uri = state.uri - local text = files.getText(uri) +local function findNearestTable(state, position) + local uri = state.uri + local text = files.getText(uri) if not text then return nil end @@ -101,13 +101,47 @@ local function findNearestTableField(state, position) local sposition = guide.offsetToPosition(state, soffset) local source guide.eachSourceContain(state.ast, sposition, function (src) - if src.type == 'table' - or src.type == 'tablefield' - or src.type == 'tableindex' - or src.type == 'tableexp' then + if src.type == 'table' then source = src end end) + + if not source then + return nil + end + + for _, field in ipairs(source) do + if field.start <= position and (field.range or field.finish) >= position then + if field.type == 'tableexp' then + if field.value.type == 'getlocal' + or field.value.type == 'getglobal' then + if field.finish >= position then + return source + else + return nil + end + end + end + if field.type == 'tablefield' then + if field.finish >= position then + return source + else + return nil + end + end + if field.type == 'tableindex' then + if field.index.type == 'string' then + if field.index.finish >= position then + return source + else + return nil + end + end + end + return nil + end + end + return source end @@ -1557,20 +1591,15 @@ local function tryCallArg(state, position, results) end local function tryTable(state, position, results) - local source = findNearestTableField(state, position) - if not source then + local tbl = findNearestTable(state, position) + if not tbl then return false end - if source.type ~= 'table' - and (not source.parent or source.parent.type ~= 'table') then + if tbl.type ~= 'table' then return end local mark = {} local fields = {} - local tbl = source - if source.type ~= 'table' then - tbl = source.parent - end local defs = vm.getFields(tbl) for _, field in ipairs(defs) do diff --git a/test/completion/common.lua b/test/completion/common.lua index 0bf9c606..7e4aac61 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -4084,3 +4084,33 @@ local foo foo = {"<??>"} ]] (EXISTS) + +TEST [[ +---@class c +---@field abc fun() +---@field abc2 fun() + +---@param p c +local function f(p) end + +f({ + abc = function(s) + local abc3 + abc<??> + end, +}) +]] +{ + { + label = 'abc3', + kind = define.CompletionItemKind.Variable, + }, + { + label = 'abc', + kind = define.CompletionItemKind.Text, + }, + { + label = 'abc2', + kind = define.CompletionItemKind.Text, + }, +} |