Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum增强 #2804

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions script/core/diagnostics/undefined-field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading