Skip to content

feat: add completion for locals, Lua 5.2 libraries and EEex functions - #7

Merged
4Luke4 merged 9 commits into
mainfrom
feat/completion
Oct 29, 2026
Merged

4Luke4 merged 9 commits into
mainfrom
feat/completion

Conversation

@4Luke4

@4Luke4 4Luke4 commented Oct 29, 2026

Copy link
Copy Markdown
Owner

What and why

The next step from plan.md after #6: completion (textDocument/completion + completionItem/resolve). It is the first plan.md feature 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:

Question Decision
Next feature Completion
Sources Names from this document (visible locals, globals it assigns, Lua keywords); Lua 5.2 libraries (globals, module members, string methods); EEex and game functions (name and link only, as in hover)
Not in scope Globals from other workspace files; MENU keywords; snippets (nothing that doesn't come from the manual, IESDP or EEex-Docs)
.menu files Completion only inside the Lua the engine compiles

What is offered where

Completion follows hover's rules, so a completed name hovers as it was offered.

Cursor Items
Variable position (pri│, empty prefix) Visible locals (the innermost of each name, the implicit self included), then globals the document assigns (any Lua chunk of a .menu file), Lua 5.2 library globals, EEex/game globals (EEex_*, Infinity_*), keywords. Each name once, from the first source that has it. While a local _ENV is visible: locals and keywords only.
Keywords The 22 reserved words (manual §3.1). In .menu expressions (enabled "…", …) only nil false true function not and or (§3.4).
_G. The globals
string., math., … (also _G.string.) The manual's members, unless the document assigns the module table (string = …) or defines that member (string.format = f), plus the fields the document assigns on it
t. / t: on any other global Fields the document assigns on t; EEex-Docs names after the operator they are documented with (EEex_Options_Option.new, C:AddGold: 387 methods after C:)
("…"): The string library (§6.4: strings use it as __index). "x": is not valid Lua 5.2, so it gets nothing.
Members of a local, a call or an index Nothing (no type inference, same as hover)
Comment, string, number, outside the Lua of a .menu file, a name being declared (after local, function, for, goto, ::, in their name lists and in parameter lists) Nothing

io, os, package/require and coroutine are 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 for print, or the EEex-Docs link. It comes in the first format of the client's completionItem.documentationFormat that the server produces. data is only { source, name }, so items keep no document state. On resolve it is looked up again and validated as unknown.
  • Sort groups: locals 0, document 1, library 2, EEex 3, keywords 4.
  • Edit range: the name around the cursor. It is sent once per list when the client supports completionList.itemDefaults.editRange, and is an insert/replace edit when the client supports insertReplaceSupport. Otherwise it is an edit up to the cursor, which keeps any text after it.
  • Trigger characters: . and :. .. and :: produce nothing, because the context looks at the previous token.

Commits (one concern each; please review them one by one)

# Commit What it changes
1 refactor(server): share the fields a document assigns on global tables globalPath/withoutG/fieldWrites move from hover/resolve.ts to syntax/globalPaths.ts; hover unchanged; own tests
2 feat(server): record where each local is visible Declaration.visible + visibleAt in scope.ts (manual §3.5 rules; a scope ends at its closing keyword, or with the enclosing scope when the keyword is missing); name resolution unchanged
3 feat(server): find the completion context at the cursor syntax/completionContext.ts. Uses the lexer, not the tree, because the parser abandons the statement being typed.
4 refactor(server): render hover signatures and documentation separately renderSignature / renderDocumentation; renderHover output unchanged, and a test pins hover = the two joined
5 feat(server): collect completion candidates completion/candidates.ts (pure), with the rules above
6 feat(server): add completion Handler, capability, the shared markupFormat (was hoverFormat), fixtures, integration tests, both changelogs
7 docs: document completion README, editor guide, CONTRIBUTING, CLAUDE.md
8 perf(server): find the globals a document assigns in one pass Found while measuring (below): the first request took 90 ms on UI.MENU
9 perf(server): lex from the statement being typed, not the chunk start Project /review finding (below)

Project /review findings and what was done

Finding Severity Outcome
The context lexed the whole chunk up to the cursor on every request. On a 296 KB Lua file: 6 ms mid-file, 20 ms at the end, heading for the 50 ms budget around 1 MB. Should fix Fixed (commit 9). Lexing starts at the last top-level statement before the cursor and no later than the first syntax error. An equivalence test (each offset gives the same site as lexing the whole chunk) caught a parser-recovery case first (t. then z on the next line is t.z), hence the error bound.
After t:, fields assigned non-function values (t.x = 1) are offered as methods, because fieldWrites doesn't record the value kind Nit Kept; listed under Limitations
One-digit sortText only works for up to 10 groups Nit Kept: there are 5 groups

Also checked by the review:

  • no vscode import in src/server or src/shared;
  • no client, command, setting, manifest, dependency, build or engines.vscode change;
  • every advertised capability has a handler;
  • both handlers check cancellation and never throw.

How it was tested

Locally (Linux/WSL2):

  • Passed: check-types; lint (0 warnings); test:unit (394 passing, up from 342); check-l10n (no new strings: detail reuses the localized hover.kind.* labels).
  • npm test: 40 passing on both VS Code 1.91.0 and stable (5 new completion tests). They cover:
    • locals in scope, with out-of-scope names and io/os/require/coroutine checked absent;
    • document globals, library modules, game functions and keywords;
    • the manual text on resolve;
    • C: methods;
    • nothing in comments and strings;
    • .menu globals across chunks, keywords limited to expression ones, and nothing outside the Lua.
  • Packaging: package, notices -- --check, then vsce package + vsce ls (file list unchanged, 42 files).
  • commitlint passes on all 9 commits and on the title.

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:

  • 348,726 offsets of UI.MENU;
  • 42,290 offsets of a 296 KB Lua file built from its scripts.

Performance (UI.MENU: 3,436 Lua chunks, 727 assigned globals; 288 requests spread over the file; 5 runs):

Measure Before commit 8 Final
First request in a document (builds the globals list) 87–95 ms 8–11 ms
Later requests 0.06 ms median, 4 ms worst 0.06 ms median, 4 ms worst
296 KB Lua file, cursor mid-file (before commit 9 → final) 6 ms 0.26 ms
Loading the completion modules and data, once 18–25 ms same

The largest list (variable position) has 1,251 items; the budget is 50 ms per request.

Size (both built locally from the same tree; main in a temporary worktree):

Measure main This branch
dist/server/server.js 663,142 B 671,510 B (+8,368)
dist/client/extension.js 437,728 B unchanged
VSIX 309.02 KB 311.57 KB
Installed size 1,217,729 B (1.16 MB) 1,226,948 B (1.17 MB)

CI: results and timings against the baseline will be added in a comment once the run finishes.

Limitations

  • No type inference: fields of locals and of tables built in Lua are not offered, nor are methods on string variables (s:). String literals work: ("x"):.
  • Single file: globals defined in other workspace files are not offered (hover and navigation are single-file too).
  • EEex-Docs heads: a head that is not itself an entry (C, EEex_Options_Option) is not offered as a global; its members are offered after it.
  • C. vs C:: game object methods are offered after : only, the operator they are documented with.
  • t:: lists every field the document assigns on t, including ones that are not functions (review nit above).

Checklist

  • npm run check-types, npm run lint and npm run test:unit pass
  • npm test (integration) passes, if activation, commands, settings or the LSP wiring changed
  • New logic has unit tests; new commands/settings have an integration test (no new commands or settings; completion has integration tests)
  • User-facing strings use vscode.l10n.t() / package.nls.json, and npm run check-l10n passes (no new strings)
  • CHANGELOG.md and docs/CHANGELOG.vsix.md have entries under [Unreleased] (if users notice the change)
  • No setting key or command ID was renamed (breaking change), or it is called out above

🤖 Generated with Claude Code

4Luke4 and others added 9 commits October 29, 2026 18:44
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>
@4Luke4

4Luke4 commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

CI results for 83914dc

Every check is green. Times are compared with #6's final run (40921287788).

Job This PR Baseline (#6)
Detect changes 4 s 6 s
Type-check, lint, unit tests, package 26 s 22 s
Integration tests (minimum / stable) 29 s / 35 s 22 s / 25 s
Lua 5.2 WASM is reproducible skipped (no third_party/lua52/ change) 19 s
Lua 5.2 manual data is current skipped (no manual data change) 11 s
Commit messages 12 s 13–22 s
CodeQL actions / JS-TS 42 s / 45 s 38 s / 63 s
CodeQL results pass, no alerts pass

Why the integration jobs are slower. The difference is not in the tests. Step times for minimum / stable:

Step This PR Baseline (#6)
Run integration tests 9 s / 11 s 11 s / 10 s
actions/setup-node 3 s / 3 s under 2 s
Cache VS Code restore, stable 7 s 4 s

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

@4Luke4
4Luke4 merged commit a2289a7 into main Oct 29, 2026
11 checks passed
@4Luke4
4Luke4 deleted the feat/completion branch October 29, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant