Skip to content

Commit b04ab94

Browse files
committed
extension: add debug menu for var show in doc
Adds a context-menu entry in the Variables views that opens the selected string value in a new editor tab, rendering real newlines instead of escape sequences. Fixes #3817
1 parent 7301453 commit b04ab94

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

extension/package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@
212212
}
213213
},
214214
"commands": [
215+
{
216+
"command": "go.debug.openVariableAsDoc",
217+
"title": "Go: Open in new Document"
218+
},
215219
{
216220
"command": "go.gopath",
217221
"title": "Go: Current GOPATH",
@@ -3490,6 +3494,13 @@
34903494
"when": "debugType == 'go' && callStackItemType == 'stackFrame' || (callStackItemType == 'thread' && callStackItemStopped)"
34913495
}
34923496
],
3497+
"debug/variables/context": [
3498+
{
3499+
"command": "go.debug.openVariableAsDoc",
3500+
"when": "debugType=='go'",
3501+
"group": "navigation"
3502+
}
3503+
],
34933504
"editor/context": [
34943505
{
34953506
"when": "editorTextFocus && config.go.editorContextMenuCommands.toggleTestFile && resourceLangId == go",
@@ -3686,4 +3697,4 @@
36863697
}
36873698
]
36883699
}
3689-
}
3700+
}

extension/src/goDebugCommands.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as vscode from 'vscode';
2+
3+
export function registerGoDebugCommands(ctx: vscode.ExtensionContext) {
4+
ctx.subscriptions.push(
5+
vscode.commands.registerCommand(
6+
'go.debug.openVariableAsDoc',
7+
async (args: any) => {
8+
const variable = args.variable;
9+
10+
let raw = variable.value.trim();
11+
if (
12+
(raw.startsWith('"') && raw.endsWith('"')) ||
13+
(raw.startsWith('`') && raw.endsWith('`'))
14+
) {
15+
raw = raw.slice(1, -1);
16+
}
17+
18+
let text: string;
19+
try {
20+
text = JSON.parse(`"${raw.replace(/"/g, '\\"')}"`);
21+
} catch {
22+
text = raw
23+
.replace(/\\r/g, '\r')
24+
.replace(/\\n/g, '\n')
25+
.replace(/\\t/g, '\t')
26+
.replace(/\\"/g, '"')
27+
.replace(/\\\\/g, '\\');
28+
}
29+
30+
const doc = await vscode.workspace.openTextDocument({
31+
language: 'plaintext',
32+
content: text
33+
});
34+
35+
const editor = await vscode.window.showTextDocument(doc);
36+
37+
await vscode.commands.executeCommand('workbench.action.editor.changeLanguageMode');
38+
}
39+
)
40+
)
41+
}

extension/src/goDebugConfiguration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { resolveHomeDir } from './utils/pathUtils';
3434
import { createRegisterCommand } from './commands';
3535
import { GoExtensionContext } from './context';
3636
import { spawn } from 'child_process';
37+
import { registerGoDebugCommands } from './goDebugCommands';
3738

3839
let dlvDAPVersionChecked = false;
3940

@@ -45,6 +46,7 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
4546
const registerCommand = createRegisterCommand(ctx, goCtx);
4647
registerCommand('go.debug.pickProcess', () => pickProcess);
4748
registerCommand('go.debug.pickGoProcess', () => pickGoProcess);
49+
registerGoDebugCommands(ctx);
4850
}
4951

5052
constructor(private defaultDebugAdapterType: string = 'go') {}

0 commit comments

Comments
 (0)