Skip to content

fix: respect embedded language auto-closing pairs when typing#309365

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/markdown-no-autoclose-angle-in-latex-272922
Open

fix: respect embedded language auto-closing pairs when typing#309365
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/markdown-no-autoclose-angle-in-latex-272922

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What this PR does

Fixes auto-closing of the outer language's pairs inside embedded language regions. The concrete symptom reported in #272922 is typing < inside a markdown LaTeX math block ($...$ or $$...$$) auto-closes to <>, even though < is a math operator there, not an HTML tag.

Root cause

AutoClosingOpenCharTypeOperation.getAutoClosingPairClose looks up candidate pairs from config.autoClosingPairs, which is built solely from the outer document language's configuration. When the cursor is inside an embedded language region (such as LaTeX embedded inside markdown math), pairs that are only defined in the outer language (e.g. markdown's <>) leak into regions where they are not meaningful.

This is the root cause @alexdima identified on the parent issue:

You're perfectly correct, all the typing code always uses the outer language characters because of the way this is implemented

(#133397)

The fix

After a candidate pair is found and the standard token-type check passes, also check the scoped (embedded) language's auto-closing pair configuration. If the pair does not exist in the embedded language (matched by identical open and close), skip auto-closing.

This mirrors the existing pattern already used for electric characters in CursorConfiguration#onElectricCharacter, which uses ScopedLineTokens#languageId to pick the embedded language's configuration.

The change is conservative:

  • When the scoped language equals the outer language (the common case for non-embedded files), behavior is unchanged.
  • Only pairs that are absent from the embedded language's configuration are suppressed. Pairs present in both languages (e.g. (), {}, [] for most languages) continue to auto-close as before.
  • No grammar / scope renaming is required, so there are no downstream consequences for other language features that rely on the markup.math.*.markdown scopes.

Verification

Repro from #272922 in a .md file:

$ a < b $

Before: typing < after a produces <>.
After: typing < after a produces just <.

Also verified:

  • Outside math blocks in markdown, < still auto-closes to <> (markdown HTML use case preserved).
  • In plain .html files, < auto-closing behavior unchanged.
  • Fenced code blocks (e.g. ```python inside markdown) still respect the embedded language's pairs (Python defines (), [], {}, "", '' but not <>, so < is no longer auto-closed there either - matching behavior in a standalone .py file).

Fixes #272922
Refs #133397

When the cursor is positioned inside an embedded language region (for
example, LaTeX embedded inside a markdown `$...$` math block), the auto-
closing pair lookup previously used only the outer language's
configuration. This caused pairs that are defined only in the outer
language (such as markdown's `<>`) to be auto-closed inside regions
where they are not meaningful - e.g. typing `<` inside a math block
would insert `<>` even though `<` is a math operator there.

Consult the embedded language's auto-closing pair configuration in
addition to the outer language's. If the pair found via the outer
language's configuration does not also exist in the embedded language
(matched by identical open and close), skip auto-closing.

This aligns with the existing pattern already used for electric
characters in CursorConfiguration#onElectricCharacter, which uses
ScopedLineTokens#languageId to pick the embedded language's
configuration.

Fixes microsoft#272922
Refs microsoft#133397
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.

[Markdown] Do not auto close angle brackets inside latex blocks

2 participants