fix(debugger): show and copy the whole value of a long string

The value popup (…Show) showed the adapter's cut-short variable value with
its escapes, in a modal dialog that only the close button dismissed. It now
evaluates the variable again for the clipboard, which adapters such as Delve
answer in full, shows a string as its text with real line breaks, and closes
on a click outside. Copy Value of a string gets the full value the same way.
Цей коміт міститься в:
l0ng-ai committed 2026-11-20 19:06:58 +08:00
1 джерело 24e5687e95
коміт b85b93be6f
3 files changed
+75 -6

No files matched your search

+13 -1
Переглянути файл
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { Variable } from "@/platform/ipc";
import { valueText } from "../valueText";
import { stringText, valueText } from "../valueText";
const v = (name: string, value: string, variablesReference = 0): Variable =>
({ name, value, variablesReference, typeName: null, evaluateName: null }) as Variable;
@@ -39,3 +39,15 @@ describe("valueText", () => {
expect(text).toBe("r\n a: x\n b: y\n …");
});
});
describe("stringText (#4747)", () => {
it("shows a quoted string as its text, escapes resolved", () => {
expect(stringText('"Name: app\\nPort: \\"3306\\"\\tok\\\\"')).toBe('Name: app\nPort: "3306"\tok\\');
expect(stringText("'it\\'s'")).toBe("it's");
expect(stringText('"caf\\u00e9 \\x41 \\u{1F600}"')).toBe("café A 😀");
});
it("leaves anything that isn't one quoted literal alone", () => {
for (const value of ["42", "[]string len: 2", '"a" + "b', "Foo@12"]) expect(stringText(value)).toBe(value);
});
});
+43 -5
Переглянути файл
@@ -12,15 +12,17 @@ import { methodAt } from "../breakpoints/symbols";
import {
activeSession,
addWatch,
evaluate,
removeAllWatches,
removeWatch,
revealFrame,
sessionSupports,
setVariableValue,
useDebug,
} from "../state";
import { blurDebugView, focusDebugView } from "./focusedView";
import { classifyValue, oneLine, shownValue, sortVariables } from "../values";
import { valueText } from "../valueText";
import { stringText, valueText } from "../valueText";
import { workflowSettings } from "@/platform/workflowSettings";
import styles from "./VariablesView.module.css";
@@ -282,7 +284,14 @@ export function VariablesView({
const copyValue = async (node: VarNode) => {
let text = node.value;
if (session != null && node.reference > 0) {
const paused = session != null ? useDebug.getState().sessions[session] : undefined;
if (paused?.status === "paused" && node.reference <= 0 && node.evaluateName) {
// A long string arrives cut short; evaluating it for the clipboard returns all of it.
text = await evaluate(node.evaluateName, "clipboard", paused).then(
(v) => v.value || text,
() => text,
);
} else if (session != null && node.reference > 0) {
try {
text = await valueText(node, (ref) => call(commands.debugVariables(session, ref)));
} catch (e) {
@@ -383,15 +392,44 @@ export function VariablesView({
)
}
/>
{valuePopup && <ValueDialog node={valuePopup} onClose={() => setValuePopup(null)} />}
{valuePopup && <ValueDialog node={valuePopup} session={session} onClose={() => setValuePopup(null)} />}
</div>
);
}
function ValueDialog({ node, onClose }: { node: VarNode; onClose: () => void }) {
/**
* View: the whole value in a popup that a click outside closes. Adapters cut long strings
* short in variables; the value is evaluated again as for copying (`clipboard`), which
* returns it in full, and a string shows as its text, line breaks included.
*/
function ValueDialog({
node,
session,
onClose,
}: {
node: VarNode;
session: DebugSessionId | null;
onClose: () => void;
}) {
const [value, setValue] = useState(node.value);
useEffect(() => {
const s = session != null ? useDebug.getState().sessions[session] : undefined;
if (!s || s.status !== "paused" || !node.evaluateName || node.reference > 0) return;
let live = true;
evaluate(node.evaluateName, "clipboard", s).then(
(v) => live && v.value && setValue(v.value),
() => {},
);
return () => {
live = false;
};
}, [node, session]);
const text = classifyValue(value, node.typeName) === "string" ? stringText(value) : value;
return (
<Dialog
open
popup
dismissOnBackdrop
onClose={onClose}
title={`${node.name}${node.typeName ? ` {${node.typeName}}` : ""}`}
width={720}
@@ -401,7 +439,7 @@ function ValueDialog({ node, onClose }: { node: VarNode; onClose: () => void })
aria-label={formatMessage(tx("Value of {label}"), { label: node.name })}
>
<div className={styles.valuePopupContent}>
<pre className={styles.valuePopupValue}>{node.value}</pre>
<pre className={styles.valuePopupValue}>{text}</pre>
</div>
</Dialog>
);
+19
Переглянути файл
@@ -51,3 +51,22 @@ export async function valueText(
await walk(root.reference, 1);
return lines.join("\n");
}
const ESCAPES: Record<string, string> = { n: "\n", r: "\r", t: "\t", b: "\b", f: "\f", v: "\v", "0": "\0" };
/**
* A string value as its text (the View popup): a quoted literal (`"a\nb"`, Python's `'a'`)
* loses its quotes and has its escapes resolved, so line breaks show as lines. Anything
* else, or a literal the adapter cut short, is returned as it came.
*/
export function stringText(value: string): string {
const m = /^(["'])([\s\S]*)\1$/.exec(value);
if (!m) return value;
return m[2]!.replace(/\\(u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.)/g, (all, e: string) => {
if (e[0] === "u" || e[0] === "x") {
const code = parseInt(e.replace(/^[ux]\{?|\}$/g, ""), 16);
return Number.isNaN(code) ? all : String.fromCodePoint(code);
}
return ESCAPES[e] ?? e;
});
}