Skip to content

Commit afdb804

Browse files
authored
fix: copy multiline values as template literals (#2197)
Fixes microsoft/vscode#241008
1 parent 638347c commit afdb804

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This changelog records changes to stable releases since 1.50.2. "TBA" changes he
77
- fix: explicitly specify completion ranges ([vscode#243409](https://github.com/microsoft/vscode/issues/243409))
88
- fix: memory leak between debug sessions ([#2173](https://github.com/microsoft/vscode-js-debug/issues/2173))
99
- fix: support `npm.scriptRunner: node`
10+
- fix: copy multiline values as template literals ([vscode#241008](https://github.com/microsoft/vscode/issues/241008))
1011
- fix: support "attach to node process" without wmic.exe and on arm64 ([vscode#244139](https://github.com/microsoft/vscode/issues/244139))
1112
- fix: support webview2 debugging on arm64
1213
- fix: race condition when opening attached windows ([vscode#239769](https://github.com/microsoft/vscode/issues/239769))

src/adapter/templates/serializeForClipboard.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ export const serializeForClipboardTmpl = templateFunction(function(
121121
),
122122
indent.repeat(level) + '}',
123123
].join(eol);
124-
case 'string':
125-
return JSON.stringify(value);
124+
case 'string': {
125+
if (value.includes('\n')) {
126+
return '`' + value.replace(/[`\\\\]|\${/g, '\\$&') + '`';
127+
} else {
128+
return JSON.stringify(value);
129+
}
130+
}
126131
case 'symbol':
127132
return value.toString();
128133
case 'undefined':

src/test/evaluate/evaluate.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,18 @@ describe('evaluate', () => {
252252
'new Date(1665007127286)': '"2022-10-05T21:58:47.286Z"',
253253
'(() => { const node = document.createElement("div"); node.innerText = "hi"; return node })()':
254254
`<div>hi</div>`,
255+
'"hello\\nmu`lti${line}"': '`hello\nmu\\`lti\\${line}`',
256+
};
257+
258+
const fnCopyExpressions = {
259+
'"hello"': 'hello',
260+
'"hello\\nmu`lti${line}"': 'hello\nmu`lti${line}',
255261
};
256262

257263
itIntegrates('copy via function', async ({ r }) => {
258264
const p = await r.launchAndLoad('blank');
259-
p.dap.evaluate({ expression: 'var x = "hello"; copy(x)' });
260-
expect((await p.dap.once('copyRequested')).text).to.equal('hello');
261-
for (const [expression, expected] of Object.entries(copyExpressions)) {
265+
const mapping = { ...copyExpressions, ...fnCopyExpressions };
266+
for (const [expression, expected] of Object.entries(mapping)) {
262267
p.dap.evaluate({ expression: `copy(${expression})` });
263268
const actual = await p.dap.once('copyRequested');
264269
expect(actual.text).to.equal(expected, expression);

0 commit comments

Comments
 (0)