Skip to content

feat(server): add Lua 5.2 parser, document symbols and folding - #3

Merged
4Luke4 merged 10 commits into
mainfrom
feat/lua-parser-symbols-folding
Oct 28, 2026
Merged

4Luke4 merged 10 commits into
mainfrom
feat/lua-parser-symbols-folding

Conversation

@4Luke4

@4Luke4 4Luke4 commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

What and why

The next step after syntax diagnostics (#1). Every remaining feature planned (completion, hover, go to definition, references, rename, semantic tokens, …) needs a syntax tree with source positions, and the embedded official compiler only says whether a chunk compiles. This PR adds that foundation, plus the first two features built on it:

  • Error-tolerant Lua 5.2 parser and scope analysis: original code in src/server/syntax/, no new dependency, checked against the official Lua 5.2.4 compiler.
  • Document symbols: Outline view, breadcrumbs, Go to Symbol in Editor, for .lua and .menu.
  • Folding ranges: Lua structure plus every .menu block and script.

It also sets versioning to start at 1.0.0, with 1.0.0 as the first stable release, and fixes the Release workflow, which ran (and failed) on any push that touched VERSION.

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

# Commit What it changes
1 ci(release): publish only when a release pull request is merged publish now also requires the pushed commit to start with chore(release): . Before, any push touching VERSION started it. #1 caused failed run 40704747499 (CHANGELOG.md has no non-empty "## [0.0.0]" section; nothing was published), and this PR would have caused another. The guard holds with the repository's COMMIT_OR_PR_TITLE squash setting: release PRs have exactly one commit, and its message matches the PR title.
2 build(release): start versioning at 1.0.0 … VERSION/package.json/lock → 1.0.0 (via npm version). New pure releaseVersion() for the case with no tag yet: stable → the committed VERSION as is (1.0.0); pre-release → 1.1.0; once a tag exists, the old logic applies unchanged. An odd committed minor is refused with an actionable message.
3 feat(server): add an error-tolerant Lua 5.2 lexer and parser lexer.ts follows llex.c; parser.ts mirrors lparser.c (priorities, .../break checks, LUAI_MAXCCALLS). It never throws, recovers at statement boundaries, and publishes no diagnostics: the official compiler stays their only source.
4 feat(server): add Lua scope analysis Locals, parameters, implicit self, for variables and repeat … until scoping (manual §3.5); globals by name.
5 feat(server): report the block structure of .menu files The existing menu scanner also returns the nested { … } blocks and their name; chunks gain an origin (file/block/property). Diagnostics are unchanged.
6 feat(server): add document symbols Parse cache keyed by URI and version, dropped on close. Hierarchical or flat symbols depending on the client capability. Capability documentSymbolProvider.
7 feat(server): add folding ranges Closing lines stay visible; honors rangeLimit and kinds. Capability foldingRangeProvider.
8 docs: document symbols and folding README, editor guide feature table, CLAUDE.md/CONTRIBUTING layout.
9 refactor(server): extract and unit-test the LSP conversions … Fixes a finding from the project /review: the flat-symbol path (used by other editors) and the folding capability handling now have unit tests.
10 ci: build the integration tests before running them, and fail empty runs Pre-existing CI bug found while checking this PR's job timings: every CI integration job since #1 logged 0 passing (0ms) and passed. npx vscode-test --label … skips npm's pretest build, so no test files existed. CI now runs npm run pretest first, and failZero/fail-zero make any run that finds no tests fail (both verified locally: exit 1 instead of 0).

Changelog entries (CHANGELOG.md and docs/CHANGELOG.vsix.md) were added in the commits that make each change, as CLAUDE.md requires.

How it was tested

Locally (Linux/WSL2, Node 24): check-types, lint (0 warnings), test:unit (141 passing), check-l10n, package, notices -- --check, vsce package + vsce ls (allow-list unchanged), actionlint 1.7.12, commitlint on all commits and on the title, and npm test (11 passing on VS Code 1.91.0 and 1.143.1).

Parser vs. the official Lua 5.2.4 compiler (the embedded WebAssembly module is the oracle):

  • In CI, test/unit/server/syntax/differential.test.ts runs a fixed corpus of 81 inputs, 4 seeds covering every statement and expression form, and 1,600 deterministic token mutations (the compiler rejects 1,401 of them, so both outcomes are exercised). Acceptance must match exactly.
  • A local sweep of 99,338 first- and second-order mutations found 0 disagreements.
  • Nesting limits match the compiler exactly: 198 nested parentheses, 200 do blocks, 199 tables, 99 nested functions.

On the game's own UI.MENU (local only; samples/ is gitignored and nothing from it was copied). It has 26,101 lines and 3,109 Lua chunks:

Result
Parse errors 0, with the same verdict as the compiler for every chunk
Menu structure 144 menu blocks, all named; 2,146 blocks, none unclosed, every nested keyword an element type
Parse + scopes 25.7 ms median (cached and shared by both features)
Symbols / folding 1.7 ms / 5.0 ms → 785 symbols, 4,510 ranges

Everything stays under the ~50 ms event-loop budget from .claude/rules/language-server.md, so no yielding was needed.

Release dry run (scratch clone with a feat: commit, nothing pushed): prepare --channel stable gives 1.0.0 with both changelogs promoted, [1.0.0]: …/releases/tag/v1.0.0, and verify passing. prepare --channel pre-release gives 1.1.0.

Bundle size: server 375,361 → 401,293 bytes (+25,932, +6.9%); client unchanged (435,815 bytes); still 10 npm packages in the notices.

CI timing review (first push, against PR #1's per-job baseline): checks 25 s (19 s), CodeQL JS/TS 72 s (61 s), actions 33 s (46 s), commitlint 15 s (17 s), integration stable 43 s (45 s), integration minimum 50 s (23 s, 2.2×). The step timings showed both caches hit (npm 47 MB, VS Code 129 MB), so the extra setup seconds were network variance. The log, however, showed that no test was running at all, which commit 10 fixes. The second push's timings are in a comment below.

Known limitations (by design, documented in the code)

  • The parser does not check goto/label rules (duplicate or invisible labels, jumps into a local's scope) or code-generation limits (200 locals, 255 upvalues, registers, constants). This affects nothing visible: those errors still come from the official compiler as diagnostics.
  • The outline lists definitions, not every name. Left out: locals inside blocks or functions, assignments to existing locals, globals assigned inside functions, and assignments in "…" menu scripts (runtime state).
  • No region folding: VS Code's built-in Lua language-configuration.json defines no region markers (checked at the 1.91.0 tag), so recognizing a marker syntax would be an invention. This corrects an assumption in my plan.
  • Once this server provides folding, VS Code uses it instead of the {/} markers in language-configuration/ie-menu.json. The ranges cover every such block.

Out of scope, found while investigating

Three Dependabot update jobs on main are failing and need a separate fix:

  • npm version update: No matching version found for @typescript-eslint/eslint-plugin@8.72.1 with a date before 10/25/2026. This looks like the cooldown window in dependabot.yml clashing with the pinned typescript-eslint.
  • @azure/msal-node security update: dependency_still_vulnerable. It's a transitive dependency of @vscode/vsce, a dev dependency that is not bundled.

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; the two new LSP features have 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 10 commits October 28, 2026 15:42
The publish job ran on every push to main that touched VERSION. The
scaffold PR (#1) reset VERSION to 0.0.0 and so started a release run on
main that failed in "Verify version" (run 40704747499: CHANGELOG.md has
no non-empty "## [0.0.0] - YYYY-MM-DD" section). Nothing was published,
but the failure was noise, and the next version change would repeat it.

publish now also requires the pushed commit to start with
"chore(release): ". The prepare job opens release pull requests with a
single commit and a title that are both "chore(release): vX.Y.Z", and the
repository squash-merges with COMMIT_OR_PR_TITLE, so the merge commit
always carries that prefix. Other VERSION changes skip the job, and a
skipped job is not a failure.

actionlint 1.7.12 passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… stable version

VERSION, package.json and package-lock.json move from 0.0.0 to 1.0.0
(set with `npm version 1.0.0 --no-git-tag-version`).

Until now, with no tag, `prepare` used VERSION as a baseline and always
went strictly above it, so 1.0.0 itself could never be released (the
next releases would have been 1.1.0 or 1.2.0). The new pure function
releaseVersion() in scripts/release/version.mts handles the first release:

- no tag, stable: the committed VERSION as is (it must have an even
  minor, otherwise prepare fails with an actionable message);
- no tag, pre-release: the next odd line, 1.0.0 -> 1.1.0;
- a tag exists: nextVersion() from it, unchanged.

prepare still requires at least one releasable (feat/fix/perf/revert or
breaking) commit. `npm version --allow-same-version` was already passed,
so keeping 1.0.0 unchanged works.

Docs: version examples in README, CONTRIBUTING, both changelog headers
and the bug report placeholder now use the 1.x line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every planned language feature (symbols, folding, completion, hover,
navigation, rename, semantic tokens) needs a syntax tree with source
positions; the embedded official compiler only reports whether a chunk
compiles. This adds an original parser in src/server/syntax/ (pure: no
LSP or vscode imports), with no new dependency.

- lexer.ts follows lua-5.2.4/src/llex.c: ASCII names, the 22 reserved
  words, read_numeral (hex fractions and p exponents, malformed numbers),
  every 5.2 escape (\z, \x, \ddd, \ + line break), long brackets of any
  level, short and long comments kept apart from tokens. Offsets are
  document offsets, so chunks embedded in .menu files need no mapping.
- ast.ts: typed node unions for the whole Lua 5.2 grammar (manual §9),
  each with [start, end) offsets and the offsets of closing keywords.
- parser.ts: recursive descent mirroring lparser.c, with the official
  operator priorities, the `...`-outside-vararg and break-outside-loop
  checks, and the LUAI_MAXCCALLS nesting limit counted the same way as
  `enterlevel`. Error tolerant: it never throws, skips to the next
  statement after an error and keeps constructs whose closing keyword
  is missing, so features keep working while the user types. It
  publishes no diagnostics: the official compiler stays their only
  source. Not checked, by design: goto/label rules and code-generation
  limits (locals, upvalues, registers, constants).

Verification against the official Lua 5.2.4 compiler (the embedded
WebAssembly module):
- test/unit/server/syntax/differential.test.ts: a fixed corpus of 81
  accepted/rejected inputs, 4 seeds covering every statement and
  expression form, and 1,600 deterministic token mutations (1,401 of
  them rejected by the compiler); acceptance must match exactly.
- Locally, 99,338 first- and second-order mutations: 0 disagreements.
- Nesting limits match the compiler exactly (e.g. 198 parentheses, 200
  `do` blocks, 99 nested functions).
- The game's own UI.MENU (local only, never committed): 3,109 chunks,
  0 parse errors, same verdict as the compiler for each, 16 ms median
  to extract and parse all of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
src/server/syntax/scope.ts resolves every identifier of a chunk to its
local declaration, or records it as a global, following the visibility
rules of the Lua 5.2 manual (§3.5):

- `local x = x`: the new local is visible only after the statement;
- `local function f`: visible in its own body (unlike `local f = function`);
- parameters and the implicit `self` of `function a:m()` in the body,
  `for` variables in the loop body (loop expressions are outside it);
- `repeat … until cond`: cond sees the body's locals;
- block scopes end at their closing keyword, shadowing resolves to the
  innermost declaration; only the first name of `function a.b.c()` and
  no table-constructor field name is a variable.

Document symbols use it to tell a global assignment from an assignment
to an existing local; go to definition, references and rename will use
the same declarations and references later. Pure module, unit-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The existing .menu scanner (extractMenuLua) now also returns `blocks`:
the nested `{ … }` tree, each with its keyword (`menu`, `button`, …),
the offsets of both braces, and its `name '…'` / `name "…"` property.
Extending the one scanner keeps a single definition of the menu syntax
(strings, `--` comments and backtick blocks hide their braces), so
symbols and folding see exactly what validation sees.

LuaChunk gains `origin` (`file`, `block` or `property`) so features can
treat `` `…` `` blocks differently from `action "…"` scripts. Chunks,
problems and diagnostics are unchanged; all existing tests pass as is.

Checked on the game's own UI.MENU (local only): 144 top-level blocks,
all `menu`, all named, 2,146 blocks in total, none unclosed, and every
nested keyword is an element type (button, label, text, list > column,
slot, edit, slider, template, handle, map, movie).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
textDocument/documentSymbol for .lua and .menu files: the Outline view,
breadcrumbs and "Go to Symbol in Editor".

- src/server/syntax/document.ts: parseDocument() parses every Lua chunk
  of a document (reusing documentKind and extractMenuLua) and runs the
  scope analysis.
- src/server/documentCache.ts: parse results keyed by URI + version,
  shared by the features and dropped when the document closes
  (.claude/rules/language-server.md › Performance).
- src/server/syntax/symbols.ts (pure): functions and methods at any
  depth, nested under their enclosing function; locals declared by
  top-level statements; the first assignment to each global outside any
  function; named fields of table constructors. Assignments to existing
  locals, and locals inside blocks or functions, are left out so the
  outline shows definitions rather than every name. In .menu files each
  `menu` block is a Namespace named by its `name` property (the keyword
  when unnamed) holding the Lua symbols inside it; "…" scripts such as
  onOpen contribute functions only.
- src/server/features/documentSymbols.ts: hierarchical DocumentSymbol[]
  when the client announces hierarchicalDocumentSymbolSupport, otherwise
  flat SymbolInformation[] with container names (other editors). Honors
  the cancellation token, never throws (logs and returns null).
- Capability: documentSymbolProvider (label "IE Lua").

Tests: unit tests for symbols (including selection ⊆ range for every
symbol and symbols around syntax errors) and the cache; integration
tests with new original fixtures (outline.lua, OUTLINE.MENU) through
vscode.executeDocumentSymbolProvider, passing on VS Code 1.91.0 and
1.143.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
textDocument/foldingRange for .lua and .menu files.

Once a server provides folding ranges, VS Code uses them instead of
indentation and of the `{`/`}` folding markers of
language-configuration/ie-menu.json, so the ranges cover everything those
did, plus Lua structure:

- Lua: function bodies, do/while/for/repeat, each if/elseif/else clause,
  multi-line table constructors, parenthesized call arguments and long
  strings, long comments and runs of two or more `--` lines (kind
  comment);
- .menu: every `{ … }` block, `` `…` `` blocks, multi-line "…" scripts,
  and the Lua constructs inside them.

A construct folds to the line before its closing keyword, which stays
visible (`end`, `until`, `}`, `)`, the next `elseif`/`else`, a closing
`]]`, backtick or quote); comments fold entirely; a construct with a
missing closer folds to its last line. One range per start line (the
outermost). No region markers: VS Code's built-in Lua
language-configuration.json defines none (checked at the 1.91.0 tag),
so recognizing one would be an invention.

src/server/features/foldingRanges.ts honors `rangeLimit` (keeps the
first ranges), omits the comment kind when the client's valueSet lacks
it, returns whole-line ranges only (fine for `lineFoldingOnly`), checks
the cancellation token and never throws. Capability:
foldingRangeProvider.

The pure logic (src/server/syntax/folding.ts) is unit-tested with LSP
line numbering; integration tests use vscode.executeFoldingRangeProvider
(present in 1.91.0, checked in extHostApiCommands.ts at that tag) on the outline fixtures and pass on VS Code
1.91.0 and 1.143.1. On the game's own UI.MENU (26,101 lines, local only):
4,510 ranges in 5 ms, after a 26 ms parse that symbols reuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- README: the two new features, and the "early version" note.
- docs/editors/README.md: a "What you get" table with the LSP requests the
  server answers (diagnostics, document symbols, folding) for users of
  other editors, and that symbols and folding do not depend on
  validate.mode.
- CLAUDE.md (§1 purpose, §2 layout) and CONTRIBUTING (layout): the new
  src/server/syntax/ folder and its differential test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s and folding

Found by the project /review: the code that adapts results to the
client was only exercised through VS Code, which always takes the
hierarchical symbol path and accepts every folding kind.

- toLspSymbols() (features/documentSymbols.ts): hierarchical
  DocumentSymbol[] or flat SymbolInformation[] with container names, the
  path that editors without hierarchicalDocumentSymbolSupport use.
- toLspFoldingRanges() (features/foldingRanges.ts): rangeLimit
  truncation (a negative limit counts as 0) and dropping the comment kind
  when the client's valueSet lacks it.

Both are pure and unit-tested in test/unit/server/features/; the
handlers keep only document lookup, cancellation and error logging.

The cache's `size` getter is now documented, and the folding integration
test sorts ranges numerically before formatting them.

No behavior change: 141 unit tests and 11 integration tests (VS Code
1.91.0 and 1.143.1) pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found while checking the CI timings of this pull request: every
"Integration tests" job, on this branch and on main since #1, logged
`0 passing (0ms)` and succeeded. ci.yml calls `npx vscode-test --label`
directly to pick one VS Code version, which skips npm's `pretest` script
(`node esbuild.mjs --tests`), so out/test/integration/ never existed and
the test glob matched nothing. The suites passed only locally, where
`npm test` runs pretest.

- ci.yml: a "Build extension and integration tests" step runs
  `npm run pretest` before vscode-test (same command as `npm test`).
- .vscode-test.mjs: mocha `failZero: true` (mocha 11.8.0, used by
  @vscode/test-cli, applies it from mochaOpts), so a run without tests
  fails. Verified locally: without the build, `vscode-test --label
  minimum` now ends with "1 test failed." and exit code 1 (before: 0
  passing, exit 0); with it, 11 passing.
- .mocharc.json: `fail-zero` for the unit tests as well (mocha 12
  already fails when no file matches; this covers matching files with
  no tests: exit 1 instead of 0, verified with an empty test file).

actionlint 1.7.12 passes. The integration jobs will take a few seconds
longer for the build and for actually running the 11 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@4Luke4

4Luke4 commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

CI after the fix (921132a): all green, integration tests run for real

Job PR #1 baseline 1st push (0181a18) This push Notes
Type-check, lint, unit tests, package 19 s 25 s 25 s 141 unit tests, including the 1,600-mutation differential test
Integration (VS Code minimum, 1.91.0) 23 s 50 s 29 s 11 passing (before: 0 passing)
Integration (VS Code stable, 1.143.1) 45 s 43 s 28 s 11 passing (before: 0 passing)
Lua 5.2 WASM is reproducible 44 s skipped 16 s runs because ci.yml changed (by design)
CodeQL JS/TS / actions 61 s / 46 s 72 s / 33 s 57 s / 42 s
Conventional Commits 17 s 15 s 16 s
  • The new "Build extension and integration tests" step takes 0 s (esbuild). Both caches hit on every job.
  • The 50 s on the first push was setup-step variance (checkout 7 s, setup-node 9 s, against 1 s / 3 s now), not a bottleneck. The log check it prompted is what exposed the 0-test runs.
  • One "Commit messages" run was cancelled because my description edit (edited event) superseded it through the workflow's cancel-in-progress concurrency. The replacement run passed.

🤖 Generated with Claude Code

@4Luke4
4Luke4 merged commit 12d3fe7 into main Oct 28, 2026
10 of 11 checks passed
@4Luke4
4Luke4 deleted the feat/lua-parser-symbols-folding branch October 28, 2026 15: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