diff options
-rw-r--r-- | changelog.md | 7 | ||||
-rw-r--r-- | script/core/completion.lua | 14 | ||||
-rw-r--r-- | script/parser/guide.lua | 12 | ||||
-rw-r--r-- | test/completion/init.lua | 50 |
4 files changed, 73 insertions, 10 deletions
diff --git a/changelog.md b/changelog.md index ce3d5608..24cf3698 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,13 @@ ## 2.3.0 * `NEW` `VSCode`: click the status bar icon to operate: * run workspace diagnostics +* `NEW` `LuaDoc`: supports `[1]` as field: + ```lua + ---@class position + ---@field [1] number + ---@field [2] number + ---@field [3] number + ``` * `FIX` loading workspace may hang * `FIX` `debug.getuservalue` and `debug.setuservalue` should not exist in `Lua 5.1` * `FIX` infer of `---@type class[][]` diff --git a/script/core/completion.lua b/script/core/completion.lua index f2eb3356..285adb38 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -398,7 +398,7 @@ local function checkModule(ast, word, offset, results) end end -local function checkFieldFromFieldToIndex(name, parent, word, start, offset) +local function checkFieldFromFieldToIndex(name, src, parent, word, start, offset) if name:match '^[%a_][%w_]*$' then return nil end @@ -411,10 +411,16 @@ local function checkFieldFromFieldToIndex(name, parent, word, start, offset) else wordStart = offset - #word + 1 end + local newText + if vm.getKeyType(src) == 'string' then + newText = ('[%q]'):format(name) + else + newText = ('[%s]'):format(name) + end textEdit = { start = wordStart, finish = offset, - newText = ('[%q]'):format(name), + newText = newText, } local nxt = parent.next if nxt then @@ -487,7 +493,7 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul newText = name, } else - textEdit, additionalTextEdits = checkFieldFromFieldToIndex(name, parent, word, start, offset) + textEdit, additionalTextEdits = checkFieldFromFieldToIndex(name, src, parent, word, start, offset) end results[#results+1] = { label = name, @@ -509,7 +515,7 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res local count = 0 for _, src in ipairs(refs) do local name = vm.getKeyName(src) - if not name or vm.getKeyType(src) ~= 'string' then + if not name then goto CONTINUE end if isSameSource(ast, src, start) then diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 108bccf5..8d681fdc 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -888,11 +888,11 @@ function m.getKeyName(obj) elseif tp == 'doc.alias' then return obj.alias[1] elseif tp == 'doc.field' then - return obj.field[1] + return tostring(obj.field[1]) elseif tp == 'doc.field.name' then - return obj[1] + return tostring(obj[1]) elseif tp == 'doc.type.field' then - return obj.name[1] + return tostring(obj.name[1]) elseif tp == 'dummy' then return obj[1] end @@ -952,14 +952,14 @@ function m.getKeyType(obj) elseif tp == 'doc.alias' then return 'string' elseif tp == 'doc.field' then - return 'string' + return type(obj.field[1]) elseif tp == 'doc.type.field' then - return 'string' + return type(obj.name[1]) elseif tp == 'dummy' then return 'string' end if tp == 'doc.field.name' then - return 'string' + return type(obj[1]) end return m.getKeyTypeOfLiteral(obj) end diff --git a/test/completion/init.lua b/test/completion/init.lua index 69241afd..9c87ab33 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -2615,3 +2615,53 @@ f(fun$) kind = define.CompletionItemKind.Snippet, } } + +TEST [[ +---@type {[1]: number} +local t + +t.$ +]] +{ + { + label = '1', + kind = define.CompletionItemKind.Field, + textEdit = { + newText = '[1]', + start = 35, + finish = 34, + }, + additionalTextEdits = { + { + start = 34, + finish = 34, + newText = '', + }, + }, + } +} + +TEST [[ +---@type {[1]: number} +local t + +t.$ +]] +{ + { + label = '1', + kind = define.CompletionItemKind.Field, + textEdit = { + newText = '[1]', + start = 35, + finish = 34, + }, + additionalTextEdits = { + { + start = 34, + finish = 34, + newText = '', + }, + }, + } +} |