дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(editor): unescape \/ when copying from a JavaScript string (#7218)
Copying from inside a JavaScript/TypeScript string literal unescapes its escapes, as IDEA does, but left \/ as is. It is now unescaped in JavaScript strings, and kept in strings of languages embedded in template literals. Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
190e5639f1
коміт
9ba3150998
2 files changed
+36
-6
No files matched your search
@@ -98,6 +98,26 @@ describe("string clipboard", () => {
|
||||
expect(view.state.doc.toString()).toBe('value = ""\\\n"');
|
||||
});
|
||||
|
||||
it.each([
|
||||
["JavaScript", "javascript", "https://example.test"],
|
||||
["TypeScript", "typescript", "https://example.test"],
|
||||
["Python", "python", "https:\\/\\/example.test"],
|
||||
])("%s unescapes an escaped slash only where the language does", async (mode, language, expected) => {
|
||||
const view = await make(mode, language, 'value = "«https:\\/\\/example.test»"');
|
||||
expect(copiedText(view.state)).toBe(expected);
|
||||
});
|
||||
|
||||
it("keeps an escaped slash in a string of a language embedded in JavaScript", async () => {
|
||||
// The embedded mode loads lazily; load it first so the first parse mounts it.
|
||||
await loadSupport(findLanguageDescription("SQL", "")!);
|
||||
const views = await Promise.all(
|
||||
["", "// filler\n".repeat(400)].map((before) =>
|
||||
make("JavaScript", "javascript", before + "const q = sql`SELECT '«a\\/b»'`;"),
|
||||
),
|
||||
);
|
||||
for (const view of views) expect(copiedText(view.state)).toBe("a\\/b");
|
||||
});
|
||||
|
||||
it("disables both escaping and unescaping with the setting", async () => {
|
||||
const settings = { smartKeys: { escapeOnPaste: false, reformatOnPaste: "none" } };
|
||||
const view = await make("Java", "java", 'value = "«a\\nb»"', settings);
|
||||
|
||||
@@ -8,9 +8,11 @@ const JS = new Set(["javascript", "typescript"]);
|
||||
interface PlainString {
|
||||
quote: string;
|
||||
language: string;
|
||||
/** `\/` is an escape: a JavaScript string, not one of a language embedded in a template literal. */
|
||||
slash: boolean;
|
||||
}
|
||||
|
||||
function readEscape(text: string, at: number, language: string): { end: number; value: string } {
|
||||
function readEscape(text: string, at: number, language: string, slash = false): { end: number; value: string } {
|
||||
const rest = text.slice(at + 1);
|
||||
const ch = rest[0];
|
||||
const basic: Record<string, string> = { n: "\n", r: "\r", t: "\t", "\\": "\\", '"': '"', "'": "'" };
|
||||
@@ -18,6 +20,7 @@ function readEscape(text: string, at: number, language: string): { end: number;
|
||||
if (!["kotlin", "rust"].includes(language)) basic.f = "\f";
|
||||
if (["python", "go", "c", "cpp"].includes(language)) basic.a = "\x07";
|
||||
if (!["java", "kotlin", "rust"].includes(language)) basic.v = "\v";
|
||||
if (slash) basic["/"] = "/";
|
||||
if (language === "kotlin") basic.$ = "$";
|
||||
if (language === "java") basic.s = " ";
|
||||
if (language === "c" || language === "cpp") basic["?"] = "?";
|
||||
@@ -85,7 +88,9 @@ function plainStringAt(state: EditorState, from: number, to: number, language: s
|
||||
if ([from, to].some((pos) => pos > node.from + i && pos < node.from + end)) return null;
|
||||
i = end - 1;
|
||||
}
|
||||
return { quote, language };
|
||||
let embedded = false;
|
||||
for (let n = node.parent; n; n = n.parent) if (n.type.isTop && n.parent) embedded = true;
|
||||
return { quote, language, slash: JS.has(language) && !embedded };
|
||||
}
|
||||
if (!node.parent) return null;
|
||||
node = node.parent;
|
||||
@@ -109,12 +114,12 @@ function escapeText(text: string, literal: PlainString): string {
|
||||
});
|
||||
}
|
||||
|
||||
function unescapeText(text: string, language: string): string {
|
||||
function unescapeText(text: string, literal: PlainString): string {
|
||||
let out = "";
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
if (text[i] !== "\\") out += text[i];
|
||||
else {
|
||||
const escape = readEscape(text, i, language);
|
||||
const escape = readEscape(text, i, literal.language, literal.slash);
|
||||
out += escape.value;
|
||||
i = escape.end - 1;
|
||||
}
|
||||
@@ -151,10 +156,15 @@ export function stringClipboard(language: string | undefined): Extension {
|
||||
),
|
||||
EditorView.clipboardOutputFilter.of((text, state) => {
|
||||
const ranges = state.selection.ranges;
|
||||
if (ranges.some((r) => r.empty || !plainStringAt(state, r.from, r.to, language))) return text;
|
||||
const literals: PlainString[] = [];
|
||||
for (const r of ranges) {
|
||||
const literal = r.empty ? null : plainStringAt(state, r.from, r.to, language);
|
||||
if (!literal) return text;
|
||||
literals.push(literal);
|
||||
}
|
||||
const selected = ranges.map((r) => state.sliceDoc(r.from, r.to));
|
||||
if (text !== selected.join(state.lineBreak)) return text;
|
||||
return selected.map((part) => unescapeText(part, language)).join(state.lineBreak);
|
||||
return selected.map((part, i) => unescapeText(part, literals[i]!)).join(state.lineBreak);
|
||||
}),
|
||||
];
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача