Repository navigation
feat: add completion for locals, Lua 5.2 libraries and EEex functions - #7
Merged
Merged
Conversation
Move `globalPath`, `withoutG` and `fieldWrites` from `hover/resolve.ts` to `syntax/globalPaths.ts`, so that completion can apply the same rules as hover (a library member the document redefines is not the library's). Hover is unchanged; the helpers get their own unit tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`Declaration.visible` gives the offsets where a local, local function, parameter, implicit `self` or `for` variable can be named, and `visibleAt` returns the innermost declaration of each name at an offset. Completion needs this at positions where no name is written yet. Rules (manual §3.5): a local is visible after its statement (not in its own initializer), a local function from its name, parameters and `self` in the function body, `for` variables in the loop body (not in the loop expressions), and the `until` condition sees the `repeat` body. A scope ends at its closing keyword; when the error-tolerant parser found none, at the end of the enclosing scope. Name resolution is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`completionSiteAt` tells what can be typed at an offset: a variable, a field
or method of a chain of names (`_G.string.`, `C:`), a method of a string
literal (`("x"):`), or nothing. It reads the tokens before the cursor rather
than the syntax tree, because the parser abandons the statement being typed.
Nothing is completed in comments, strings and numbers, outside the Lua chunks
of a .menu file, or where a name is declared: after `local`, `function`,
`for`, `goto` and an opening `::`, in the name lists of `local` and `for`, and
in parameter lists. `"x":` is not followed: Lua 5.2 only allows a
parenthesized string there (manual §3.4, prefixexp).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`renderSignature` gives the code line of a hover and `renderDocumentation` the rest (the documentation and the source link), so completion items can put the first in `detail` and the second in `documentation`. `renderHover` is built from the same parts and its output is unchanged (existing tests); a new test checks that the hover is exactly the two joined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`completionCandidates` lists what completion offers at a completion site,
following hover's rules so that a completed name hovers as offered:
- variables: visible locals, then globals the document assigns (any chunk of a
.menu file), the Lua 5.2 library globals the engine opens, EEex and game
functions, and keywords; each name once, from the first source that has it.
Only locals and keywords while a local `_ENV` is visible.
- keywords: the 22 reserved words (manual §3.1); in .menu expressions only the
7 an expression can contain (manual §3.4).
- `t.`, `t:` on globals (`_G.` prefixes removed): fields the document assigns,
library members unless the document assigns the module or defines that
member, EEex-Docs names after the operator they are documented with
(`EEex_Options_Option.new`, `C:AddGold`); `_G.` offers the globals.
- `("…"):`: the string library (manual §6.4).
Documentation is referred to by source and name (`documentationSource`) and
looked up again when an item is resolved (`documentationTarget`), so items
carry no document state. `knowledge.ts` gains listings of its entries.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`textDocument/completion` and `completionItem/resolve`, triggered by `.` and `:` (see src/server/completion/candidates.ts for what is offered where). - Items carry the hover's code line as `detail`. The documentation (manual text, the engine's note on `print`, or the EEex-Docs link) is added on resolve, from `data` naming its source, in the first format of the client's `completionItem.documentationFormat` that the server produces. - The edit range is the name around the cursor: sent once per list when the client lists `editRange` in `completionList.itemDefaults`, else per item; an insert/replace edit when the client has `insertReplaceSupport`, else an edit up to the cursor, which keeps the text after it. - `hoverFormat` becomes the shared `markupFormat` (its tests move with it), and the server builds its localized messages once for hover and completion. Integration tests (VS Code 1.91.0 and stable) cover locals in scope, document globals, library and game functions, the manual text on resolve, `C:` methods, comments and strings, and .menu chunks; fixtures are original. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README (status note and feature list), the editor setup guide's feature table and notes, CONTRIBUTING's layout table, and CLAUDE.md's overview and layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first completion in a document listed its globals by calling `definitions()` for each name, which scans every chunk: on the game's UI.MENU (3,436 Lua chunks, 727 assigned globals) that took 81 ms of a 90 ms first request, over the 50 ms budget. One pass in document order now records each name's first write, the same identifier `definitions(…)[0]` gives hover. First request: 90 ms -> 10 ms; later requests unchanged (0.06 ms median). A test pins the first-write rule, across .menu chunks too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found by the project review: the cursor context lexed the whole chunk up to the cursor on every request, so the time grew with the file (6 ms in the middle and 20 ms at the end of a 296 KB Lua file, heading for the 50 ms budget around 1 MB). Lexing now starts at the last top-level statement that starts before the cursor and no later than the first syntax error. Statements before the first error were parsed in step with the chunk, so they start at a token of the whole chunk; after an error the parser resumes at likely statement starts that can be inside an expression (`t.` then `z` on the next line is `t.z`), which the new equivalence test caught. The unit test compares the result at every offset of Lua and .menu sources with lexing from the chunk start. The same check over the game's UI.MENU (348,726 offsets) and a 296 KB Lua file built from its scripts (42,290 offsets) finds no difference. 296 KB file: 0.26 ms in the middle (was 6 ms), 9 ms at the end on the first request (was 20 ms). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
CI results for 83914dcEvery check is green. Times are compared with #6's final run (
Why the integration jobs are slower. The difference is not in the tests. Step times for minimum / stable:
The rest is job start-up. All of it is runner variance, and the jobs are within the 24–39 s range seen on #4 and #5, so nothing was rerun. The two path-gated jobs were skipped as designed, which saved about 30 s of CI time. 🤖 Generated with Claude Code |
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
The next step from
plan.mdafter #6: completion (textDocument/completion+completionItem/resolve). It is the firstplan.mdfeature not yet implemented (still missing: completion, formatting, semantic tokens, quick fixes, JSDoc), and it reuses hover's knowledge: the Lua 5.2 manual data, the EEex-Docs index and the scope analysis.Decisions taken with the maintainer before implementation:
.menufilesWhat is offered where
Completion follows hover's rules, so a completed name hovers as it was offered.
pri│, empty prefix)selfincluded), then globals the document assigns (any Lua chunk of a.menufile), Lua 5.2 library globals, EEex/game globals (EEex_*,Infinity_*), keywords. Each name once, from the first source that has it. While a local_ENVis visible: locals and keywords only..menuexpressions (enabled "…", …) onlynil false true function not and or(§3.4)._G.string.,math., … (also_G.string.)string = …) or defines that member (string.format = f), plus the fields the document assigns on itt./t:on any other globalt; EEex-Docs names after the operator they are documented with (EEex_Options_Option.new,C:AddGold: 387 methods afterC:)("…"):__index)."x":is not valid Lua 5.2, so it gets nothing..menufile, a name being declared (afterlocal,function,for,goto,::, in their name lists and in parameter lists)io,os,package/requireandcoroutineare never offered: the manual data has only the libraries the engine opens (IESDP).Items
detail: the hover's code line, e.g.local function tally(items),(parameter) prefix,string.format (formatstring, ···).documentation: added on resolve. It is the hover text without the code line: the manual text, the engine note forprint, or the EEex-Docs link. It comes in the first format of the client'scompletionItem.documentationFormatthat the server produces.datais only{ source, name }, so items keep no document state. On resolve it is looked up again and validated asunknown.completionList.itemDefaults.editRange, and is an insert/replace edit when the client supportsinsertReplaceSupport. Otherwise it is an edit up to the cursor, which keeps any text after it..and:...and::produce nothing, because the context looks at the previous token.Commits (one concern each; please review them one by one)
refactor(server): share the fields a document assigns on global tablesglobalPath/withoutG/fieldWritesmove fromhover/resolve.tstosyntax/globalPaths.ts; hover unchanged; own testsfeat(server): record where each local is visibleDeclaration.visible+visibleAtinscope.ts(manual §3.5 rules; a scope ends at its closing keyword, or with the enclosing scope when the keyword is missing); name resolution unchangedfeat(server): find the completion context at the cursorsyntax/completionContext.ts. Uses the lexer, not the tree, because the parser abandons the statement being typed.refactor(server): render hover signatures and documentation separatelyrenderSignature/renderDocumentation;renderHoveroutput unchanged, and a test pins hover = the two joinedfeat(server): collect completion candidatescompletion/candidates.ts(pure), with the rules abovefeat(server): add completionmarkupFormat(washoverFormat), fixtures, integration tests, both changelogsdocs: document completionperf(server): find the globals a document assigns in one passUI.MENUperf(server): lex from the statement being typed, not the chunk start/reviewfinding (below)Project
/reviewfindings and what was donet.thenzon the next line ist.z), hence the error bound.t:, fields assigned non-function values (t.x = 1) are offered as methods, becausefieldWritesdoesn't record the value kindsortTextonly works for up to 10 groupsAlso checked by the review:
vscodeimport insrc/serverorsrc/shared;engines.vscodechange;How it was tested
Locally (Linux/WSL2):
check-types;lint(0 warnings);test:unit(394 passing, up from 342);check-l10n(no new strings:detailreuses the localizedhover.kind.*labels).npm test: 40 passing on both VS Code 1.91.0 and stable (5 new completion tests). They cover:io/os/require/coroutinechecked absent;C:methods;.menuglobals across chunks, keywords limited to expression ones, and nothing outside the Lua.package,notices -- --check, thenvsce package+vsce ls(file list unchanged, 42 files).Equivalence audit of commit 9 (local, the game's
UI.MENU, gitignored, nothing copied): every offset gives the same site as lexing from the chunk start, with 0 mismatches:UI.MENU;Performance (
UI.MENU: 3,436 Lua chunks, 727 assigned globals; 288 requests spread over the file; 5 runs):The largest list (variable position) has 1,251 items; the budget is 50 ms per request.
Size (both built locally from the same tree;
mainin a temporary worktree):maindist/server/server.jsdist/client/extension.jsCI: results and timings against the baseline will be added in a comment once the run finishes.
Limitations
s:). String literals work:("x"):.C,EEex_Options_Option) is not offered as a global; its members are offered after it.C.vsC:: game object methods are offered after:only, the operator they are documented with.t:: lists every field the document assigns ont, including ones that are not functions (review nit above).Checklist
npm run check-types,npm run lintandnpm run test:unitpassnpm test(integration) passes, if activation, commands, settings or the LSP wiring changedvscode.l10n.t()/package.nls.json, andnpm run check-l10npasses (no new strings)CHANGELOG.mdanddocs/CHANGELOG.vsix.mdhave entries under[Unreleased](if users notice the change)🤖 Generated with Claude Code