From ff6187ebf9e7250b93e79bd8672be4ff68af7609 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 01:48:28 +0800 Subject: [PATCH] fix(editor): pair backticks, Python prefixed strings and triple quotes (#5051) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `prefsExtension` gives `closeBrackets()` its own pairing list (to honour the Insert pair bracket / quote options), and that list replaced the language's: only `( [ { ' "`. So: - TypeScript / JavaScript: typing `\`` left the template literal open; - Python: `f"`, `r'`, `b"`… left the string open (the language's `stringPrefixes` were dropped), and `"""` did not close. The list now adds the backtick for JavaScript, TypeScript, Vue, Svelte, Astro and Go, and Python's `'''` / `"""` with its string prefixes; the two options still switch brackets and quotes off. --- src/editor/core/cm/prefs.ts | 27 ++++++++++++++++-- src/editor/core/cm/smartKeys.test.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/editor/core/cm/prefs.ts b/src/editor/core/cm/prefs.ts index 8eb3dc00b..fced47673 100644 --- a/src/editor/core/cm/prefs.ts +++ b/src/editor/core/cm/prefs.ts @@ -618,6 +618,27 @@ export function caretExtension(prefs: EditorExtras): Extension { ]; } +/** Template literals and raw strings open with a backtick. */ +const BACKTICK_LANGS = new Set([ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", + "vue", + "svelte", + "astro", + "go", +]); +const PYTHON_PREFIXES = ["f", "r", "u", "b", "fr", "rf", "br", "rb"].flatMap((p) => + p.length === 1 ? [p, p.toUpperCase()] : [p, p.toUpperCase(), p[0]!.toUpperCase() + p[1], p[0] + p[1]!.toUpperCase()], +); + +/** The quotes that pair in `language`, and the string prefixes that may come before them. */ +function languageQuotes(language: string | undefined): { quotes: string[]; prefixes?: string[] } { + if (language === "python") return { quotes: ["'", '"', "'''", '"""'], prefixes: PYTHON_PREFIXES }; + return { quotes: BACKTICK_LANGS.has(language ?? "") ? ["'", '"', "`"] : ["'", '"'] }; +} + export function prefsExtension(o: PrefsOptions): Extension { const { prefs } = o; const g = prefs.general; @@ -659,8 +680,10 @@ export function prefsExtension(o: PrefsOptions): Extension { out.push(pasteReindentFilter(k.reformatOnPaste)); // Pairing: closeBrackets() reads the brackets list from language data; ours comes first. - const brackets = [...(k.pairBrackets ? ["(", "[", "{"] : []), ...(k.pairQuotes ? ["'", '"'] : [])]; - out.push(Prec.highest(EditorState.languageData.of(() => [{ closeBrackets: { brackets } }]))); + const quotes = k.pairQuotes ? languageQuotes(o.language) : null; + const brackets = [...(k.pairBrackets ? ["(", "[", "{"] : []), ...(quotes?.quotes ?? [])]; + const pairing = { brackets, ...(quotes?.prefixes ? { stringPrefixes: quotes.prefixes } : {}) }; + out.push(Prec.highest(EditorState.languageData.of(() => [{ closeBrackets: pairing }]))); // `<` pairs where the syntax says a generic type list starts, as in IDEA — no separate option. if (k.pairBrackets && !o.readOnly) out.push(smartAngleBrackets(o.language)); if (!k.surroundSelection) out.push(Prec.highest(noSurround)); diff --git a/src/editor/core/cm/smartKeys.test.ts b/src/editor/core/cm/smartKeys.test.ts index 1a011eb0e..f215c4bd6 100644 --- a/src/editor/core/cm/smartKeys.test.ts +++ b/src/editor/core/cm/smartKeys.test.ts @@ -8,6 +8,7 @@ import { java } from "@codemirror/lang-java"; import { javascript } from "@codemirror/lang-javascript"; import { languages as cmLanguages } from "@codemirror/language-data"; import { kotlin, csharp } from "@codemirror/legacy-modes/mode/clike"; +import { python } from "@codemirror/legacy-modes/mode/python"; import { validateEditorExtras } from "@/platform/editorSettings"; import { autoIndentBackspace, @@ -211,6 +212,46 @@ describe("tab out of pairs", () => { }); }); +describe("quote pairs per language", () => { + const typed = (language: string, support: Extension, text: string, chars: string) => { + const v = make(text, [ + support, + closeBrackets(), + prefsExtension({ + prefs: validateEditorExtras({}), + path: "", + softWrap: false, + showWhitespace: false, + hardWrapAt: 120, + readOnly: false, + language, + }), + ]); + for (const ch of chars) { + const tr = insertBracket(v.state, ch); + if (tr) v.dispatch(tr); + else v.dispatch(v.state.update(v.state.replaceSelection(ch), { userEvent: "input.type" })); + } + return show(v); + }; + + it("pairs template literal backticks in TypeScript", () => { + expect(typed("typescript", javascript({ typescript: true }), "const g = |", "`")).toBe("const g = `|`"); + }); + + it("pairs Python's prefixed and triple-quoted strings", () => { + const py = StreamLanguage.define(python); + expect(typed("python", py, "label = f|", '"')).toBe('label = f"|"'); + expect(typed("python", py, "p = rb|", "'")).toBe("p = rb'|'"); + expect(typed("python", py, " |", '"""')).toBe(' """|"""'); + }); + + it("keeps plain quotes for other languages and respects the options", () => { + expect(typed("java", java(), "String s = |", '"')).toBe('String s = "|"'); + expect(typed("java", java(), "String s = |", "`")).toBe("String s = `|"); + }); +}); + describe("smart angle brackets", () => { /** The real wiring: prefsExtension attaches the `<` handler for the document's language. */ const prefs = (language: string | undefined, smartKeys: Record = {}) =>