From 46d2418a90f25c18773bcacfcd63c4764f485996 Mon Sep 17 00:00:00 2001 From: yogeshwaran-c Date: Mon, 13 Apr 2026 11:18:47 +0530 Subject: [PATCH] fix: respect embedded language auto-closing pairs when typing 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 #272922 Refs #133397 --- .../common/cursor/cursorTypeEditOperations.ts | 17 +++++++++++++++++ src/vs/editor/common/cursorCommon.ts | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/vs/editor/common/cursor/cursorTypeEditOperations.ts b/src/vs/editor/common/cursor/cursorTypeEditOperations.ts index d1290a3e651db..f63abde46a772 100644 --- a/src/vs/editor/common/cursor/cursorTypeEditOperations.ts +++ b/src/vs/editor/common/cursor/cursorTypeEditOperations.ts @@ -262,6 +262,23 @@ export class AutoClosingOpenCharTypeOperation { if (!pair.shouldAutoClose(scopedLineTokens, beforeColumn - scopedLineTokens.firstCharOffset)) { return null; } + // If the cursor is inside an embedded language region (e.g. LaTeX + // embedded inside a markdown `$...$` math block), honor the + // embedded language's auto-closing pair configuration. Without + // this, the outer language's pairs (e.g. markdown's `<>`) would + // leak into regions where they are not meaningful. + // See https://github.com/microsoft/vscode/issues/272922 and the + // parent issue https://github.com/microsoft/vscode/issues/133397. + if (scopedLineTokens.languageId !== config.languageId) { + const embeddedConfig = config.languageConfigurationService.getLanguageConfiguration(scopedLineTokens.languageId); + const embeddedCandidates = embeddedConfig.getAutoClosingPairs().autoClosingPairsOpenByEnd.get(ch); + const hasMatchingEmbeddedPair = embeddedCandidates + ? embeddedCandidates.some(c => c.open === pair.open && c.close === pair.close) + : false; + if (!hasMatchingEmbeddedPair) { + return null; + } + } // Typing for example a quote could either start a new string, in which case auto-closing is desirable // or it could end a previously started string, in which case auto-closing is not desirable // diff --git a/src/vs/editor/common/cursorCommon.ts b/src/vs/editor/common/cursorCommon.ts index 4b03cf57d26fc..782b96456fae3 100644 --- a/src/vs/editor/common/cursorCommon.ts +++ b/src/vs/editor/common/cursorCommon.ts @@ -84,6 +84,10 @@ export class CursorConfiguration { private readonly _languageId: string; private _electricChars: { [key: string]: boolean } | null; + public get languageId(): string { + return this._languageId; + } + public static shouldRecreate(e: ConfigurationChangedEvent): boolean { return ( e.hasChanged(EditorOption.layoutInfo)