Skip to content

Fix auto indentation after a key with an anchor - #1289

Merged
datho7561 merged 2 commits into
redhat-developer:mainfrom
kwy404:fix-yaml-anchor-indent
Oct 2, 2026
Merged

datho7561 merged 2 commits into
redhat-developer:mainfrom
kwy404:fix-yaml-anchor-indent

Conversation

@kwy404

@kwy404 kwy404 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Root cause: increaseIndentPattern in language-configuration.json contains the HTML entity & where it should have a plain &. Because of that, the optional anchor group (&\w+)? never matches a real anchor, so a line like base: &base or - &item is not seen as opening a block.

The extension defaults editor.autoIndent to keep for YAML, so this shows up for users who set "[yaml]": { "editor.autoIndent": "full" }. With that setting, pressing Enter after base: indents the next line, but pressing Enter after base: &base does not.

Fix: replace & with & in the pattern. Nothing else changes.

What issues does this PR fix or reference?

No existing issue. The same pattern has been in the file since 2018.

Is it tested? How?

Added test/languageConfiguration.test.ts. It loads language-configuration.json and checks that increaseIndentPattern matches base: &base and - &item. It fails on main (assert.ok(increaseIndentPattern.test('base: &base')) is false) and passes with the fix. The full npm test suite passes.

To check it by hand: set "[yaml]": { "editor.autoIndent": "full" }, type base: &base in a YAML file and press Enter. The new line is now indented by two spaces, as it is after base:.

The increaseIndentPattern in language-configuration.json contained the HTML entity & instead of &, so lines such as 'base: &base' never matched.

Signed-off-by: kwy404 <thekaway404@gmail.com>
@kwy404
kwy404 requested a review from datho7561 as a code owner October 1, 2026 22:40

@h55n h55n left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this against master. The pattern in language-configuration.json still has the HTML-escaped &, so "base: &base" and "- &item" never matched. I ran the old and new regex in node: both of those now match, and "key: value" and "- item" still don't, so nothing else changes. The test reads the real config file, which is good. One thing for later, not for this PR: "key: &anc # comment" still doesn't indent, since the anchor is followed by a comment.

@kwy404

kwy404 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking it so thoroughly @h55n. Good point about key: &anc # comment, I'll keep that one for a separate change so this PR stays focused on the escaping bug.

@datho7561

Copy link
Copy Markdown
Contributor

If I have something like this:

root:
  second: &ref

And press enter after &ref, it indents properly after this change. But if you have this:

root:
  second: &ref-erance

and you hit enter after &ref-erance, it still doesn't indent properly.

@kwy404

kwy404 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @datho7561, good catch. The pattern only allowed word characters after &, but YAML anchor names can contain anything except whitespace and the flow indicators , [ ] { }. In bd23559 the anchor part is now &[^\s,\[\]{}]+, so second: &ref-erance indents after Enter, and I added it (plus - &item.v2) to the test, along with key: value and - item as cases that must not indent.

@datho7561 datho7561 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked the spec, and yes, that seems correct. I can confirm the indentation is happening as expected now.

Thanks, Alexandre!

@datho7561 datho7561 added the bug label Oct 2, 2026
@datho7561 datho7561 added this to the 1.25.0 milestone Oct 2, 2026
@datho7561
datho7561 merged commit cdfb5a6 into redhat-developer:main Oct 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants