Skip to content

Anchor the #endregion folding marker - #261

Open
Ngo Quoc Viet (NgoQuocViet2001) wants to merge 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-endregion-marker-anchor
Open

Ngo Quoc Viet (NgoQuocViet2001) wants to merge 1 commit into
microsoft:mainfrom
NgoQuocViet2001:fix-endregion-marker-anchor

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown

Problem

htmlFolding.ts detects region markers with:

const m = text.match(/^\s*#(region\b)|(endregion\b)/);

Alternation binds looser than the anchor, so this reads as (^\s*#region\b) or (endregion\b) — the second branch is unanchored and matches the word anywhere in a comment.

Effect

Measured with getFoldingRanges on the built library:

input ranges
#region … a harmless multi-line comment … #endregion {3-5 comment}, {0-7 region} ✓
the same, with the comment containing the word endregion {0-3 region} ✗
a standalone multi-line comment {0-4 comment} ✓
the same comment containing the word endregion [] ✗

Two things go wrong at once. The open #region is closed at the prose comment instead of at its real marker, so the fold covers the wrong span; and because that comment was consumed as a marker, it also loses its own folding range.

An HTML comment that documents region markers — or any prose that happens to contain the word — is enough to trigger it.

Fix

Group the alternation so the anchor covers both branches:

const m = text.match(/^\s*#(?:(region\b)|(endregion\b))/);

Capture-group numbering is unchanged, so the m[1] / m[2] branches below need no edit.

Test plan

  • Added two cases to src/test/folding.test.ts beside the existing Fold regions: a region whose body contains a comment mentioning endregion, and a standalone comment containing the word.
  • Ran: node --test ./lib/esm/test/*.test.js → 178 passing (176 before).
  • Checked: reverting only htmlFolding.ts fails both new tests.
  • Ran: npm run lint → clean.

The marker test reads /^\s*#(region\b)|(endregion\b)/, and alternation binds
looser than the anchor, so `^\s*#` applies only to the first branch. Any
comment containing the word "endregion" anywhere matched the second one.

An open #region was therefore closed at that comment instead of at its real
marker, and the comment lost its own folding range in the process.
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