diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2024-08-19 12:19:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-19 12:19:09 +0800 |
commit | 97d2408f7eb03fcb875896d99584e132d0c9585d (patch) | |
tree | e122c1d88abe97d478ce27a4238a8cdada93aa43 | |
parent | 3a34b724f138ccd545cd9354d5e090d2c76789c2 (diff) | |
parent | 82af2563d568b087bd0059f00d8178dd92af7324 (diff) | |
download | lua-language-server-97d2408f7eb03fcb875896d99584e132d0c9585d.zip |
Merge pull request #2804 from xuhuanzy/dev
`enum`增强
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/diagnostics/undefined-field.lua | 33 | ||||
-rw-r--r-- | script/vm/compiler.lua | 35 |
3 files changed, 70 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index fcc471ce..590d38ee 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,8 @@ ## Unreleased <!-- Add all new changes here. They will be moved under a version at release --> +* `NEW` using `enum (partial)`, it suggests all fields with the same `enum` type rather than just the fields from the current table. +* `NEW` When using `enum["<key>" or <index>]`, undefined fields will raise an 'undefined' error. * `FIX` Renaming files in the directory leads to the auto-correction in "require" adding extra characters. ## 3.10.4 diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua index ab5d4d54..edb65909 100644 --- a/script/core/diagnostics/undefined-field.lua +++ b/script/core/diagnostics/undefined-field.lua @@ -52,6 +52,39 @@ return function (uri, callback) } end end + ---@async + local function checkUndefinedFieldByIndexEnum(src) + await.delay() + local isEnum = false + for _, node in ipairs(vm.compileNode(src.node)) do + local docs = node.bindDocs + if docs then + for _, doc in ipairs(docs) do + if doc.type == "doc.enum" then + isEnum = true + break + end + end + end + end + if not isEnum then + return + end + if vm.hasDef(src) then + return + end + local keyName = guide.getKeyName(src) + if not keyName then + return + end + local message = lang.script('DIAG_UNDEF_FIELD', guide.getKeyName(src)) + callback { + start = src.index.start, + finish = src.index.finish, + message = message, + } + end guide.eachSourceType(ast.ast, 'getfield', checkUndefinedField) guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField) + guide.eachSourceType(ast.ast, 'getindex', checkUndefinedFieldByIndexEnum) end diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 54390450..e0cb54c7 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -189,6 +189,41 @@ local searchFieldSwitch = util.switch() end end end + local docs = source.bindDocs + if docs then + for _, doc in ipairs(docs) do + if doc.type == 'doc.enum' then + if not vm.docHasAttr(doc, 'partial') then + return + end + for _, def in ipairs(vm.getDefs(doc)) do + if def.type ~= 'doc.enum' then + goto CONTINUE + end + local tbl = def.bindSource + if not tbl then + return + end + for _, field in ipairs(tbl) do + if field.type == 'tablefield' + or field.type == 'tableindex' then + if not field.value then + goto CONTINUE + end + local fieldKey = guide.getKeyName(field) + if key == vm.ANY + or key == fieldKey then + hasFiled = true + pushResult(field) + end + ::CONTINUE:: + end + end + ::CONTINUE:: + end + end + end + end end) : case 'string' : case 'doc.type.string' |