mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(editor): pair backticks, Python prefixed strings and triple quotes (#5051)
`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.
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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<string, unknown> = {}) =>
|
||||
|
||||
Reference in New Issue
Block a user