Skip to content

fix(structure): An omitted OF/ELSE is not a branch of the CASE or IF around it - #873

Merged
msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/case-branches-skip-omit
Nov 25, 2026
Merged

msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/case-branches-skip-omit

Conversation

@geircodes

Copy link
Copy Markdown
Contributor

An omitted OF/ELSE is not a branch of the CASE or IF around it

What happened

A CASE with an unconditional OMIT block inside one of its branches. The block is dead code to the compiler, and it holds a stray ELSE and one END more than it opens:

      CASE UPPER(Kind)
      OF 'A'
         ...
  OMIT('xxxxx')
         ...
            ELSE  ! left over, no structure
               ...
            END
         ...
  !xxxxx
      ELSE
         ...
      END

Hovering the CASE showed the omitted ELSE as one of its branches:

CASE — 3 branches · END on line 3106

Line Branch
2967 OF 'A'
3062 ELSE ! left over, no structure
3103 ELSE

The END was right: process() already reads an OMIT body as non-code (omittedLines(), omitFloor), so the extra END inside the block closes nothing. Only the branch list was wrong.

Root cause

populateBranches() collects every OF/OROF/ELSIF/ELSE token and gives each to the innermost CASE/IF open around it, with no regard for OMIT bodies:

const allBranchTokens = this.tokens.filter(t =>
    t.type === TokenType.ConditionalContinuation && isBranchKeyword(t.value)
);
...
for (const container of open) {
    const inNested = open.some(n => ...);

The same branches feed the CASE, OF and END hover cards, the "branch N of M" text, and the OROF check in StructureDiagnostics.validateCaseStructures.

The fix

process() passes the OMIT body flags it already computed, and a branch keyword in an OMIT body goes only to a CASE/IF opened in that body, as the body's ENDs do:

const inOmit = (line: number): boolean => omitted !== undefined && omitted[line] === 1;
...
// Only a structure opened in the OMIT body can own an omitted keyword. One opened in a
// body closes with it, so "opened in an OMIT body" here means this body.
const keywordOmitted = inOmit(t.line);
for (const container of open) {
    if (keywordOmitted && !inOmit(container.line)) continue;

Dropping omitted keywords altogether would be simpler but wrong: a CASE or IF written in the body keeps its own branches.

What this changes for the other readers of branches:

  • The CASE card above now lists OF 'A' and the real ELSE: 2 branches.
  • The real ELSE says "branch 3 of 3", not "4 of 4", in the test fixture.
  • CASE a / OMIT('***') / OF 9 / !*** / OROF 2 / END: the omitted OF used to hide "OROF must be preceded by an OF clause in CASE structure". The error now shows on the live OROF, as the compiler sees it.

Testing

  • OmitBodyStructures.test.ts, new suite "A branch keyword in an OMIT body is not a branch of the structure around it" (4 tests): a CASE around a body with a stray OF and ELSE, a CASE and an IF written in the body keeping their own branches, an IF around a body with a stray ELSIF and ELSE, and the OROF error.
  • HoverProvider.CaseBranchList.test.ts (3 tests): the CASE card and the END card list only the live branches, and the real ELSE card counts "3 of 3".
  • 6 of the 7 fail with the fix reverted, showing the omitted keyword. The seventh (structures in the body keep their own branches) passes both ways: it guards against the fix dropping too much.
  • Full server suite: 4626 passing, 3 pending, 0 failing.

An independent review also probed two OMIT blocks in one CASE, a body that leaves a CASE open, a CASE → IF → OMIT nesting, a conditional OMIT (unchanged, still read as code), an unterminated OMIT (unchanged), and a second process() on the same tokens (identical result).

Scope

  • Only unconditional OMIT, like the END pairing it follows. A conditional OMIT/COMPILE is not evaluated in DocumentStructure and stays code.
  • A branch keyword on the last line of an OMIT body that leaves its own CASE open goes to no structure, because leaveOmitBody ends that CASE on the same line. Nothing shows branches of omitted structures (hover is off in OMIT bodies, diagnostics there are filtered), so there is nothing to see.
  • UnreachableCodeProvider has the same blindness to OMIT bodies in its own branch-keyword scan: an omitted ELSE or RETURN changes which live lines are marked unreachable. That is a separate provider and is left for its own issue.

…around it

populateBranches() gave a CASE/IF every OF, OROF, ELSIF and ELSE inside
it, including those in the body of an unconditional OMIT block. END
pairing already treats an OMIT body as non-code, so the CASE card named
the right END but listed a stray omitted ELSE among its branches, the
"branch N of M" count was off, and an omitted OF hid the "OROF must be
preceded by an OF" error. A branch keyword in an OMIT body now goes only
to a CASE/IF opened in that body.

Tests: 4 in OmitBodyStructures.test.ts, 3 in
HoverProvider.CaseBranchList.test.ts; 6 fail without the fix. Full
suite 4626 passing, 3 pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@msarson
msarson merged commit 19e7da6 into msarson:version-1.0.11 Nov 25, 2026
1 check passed
msarson added a commit that referenced this pull request Nov 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geircodes
geircodes deleted the fix/case-branches-skip-omit branch November 25, 2026 13:46
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.

2 participants