diff --git a/src/features/debugger/__tests__/valueText.test.ts b/src/features/debugger/__tests__/valueText.test.ts index e27584c42e..0d1f07c685 100644 --- a/src/features/debugger/__tests__/valueText.test.ts +++ b/src/features/debugger/__tests__/valueText.test.ts @@ -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); + }); +}); diff --git a/src/features/debugger/ui/VariablesView.tsx b/src/features/debugger/ui/VariablesView.tsx index 82ae6a1fde..352e07d880 100644 --- a/src/features/debugger/ui/VariablesView.tsx +++ b/src/features/debugger/ui/VariablesView.tsx @@ -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 && setValuePopup(null)} />} + {valuePopup && setValuePopup(null)} />} ); } -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 ( void }) aria-label={formatMessage(tx("Value of {label}"), { label: node.name })} >
-
{node.value}
+
{text}
); diff --git a/src/features/debugger/valueText.ts b/src/features/debugger/valueText.ts index 43bfc9672c..1167111dcb 100644 --- a/src/features/debugger/valueText.ts +++ b/src/features/debugger/valueText.ts @@ -51,3 +51,22 @@ export async function valueText( await walk(root.reference, 1); return lines.join("\n"); } + +const ESCAPES: Record = { 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; + }); +}