-
Notifications
You must be signed in to change notification settings - Fork 829
extension: add debug menu for var show in doc #3818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+164
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9c639cb
extension: add debug menu for var show in doc
MistaTwista 84e4e7b
better parsing and use TextDocumentContentProvider
MistaTwista 0207ea7
run gopls generator
MistaTwista a8264cd
run tools generate
MistaTwista 9ba130c
use memory references to read big strings from delve
MistaTwista b395ddd
add description to new command
MistaTwista af4625d
changelog updated
MistaTwista 63492e3
set prefix for the new command
MistaTwista 0548c6d
copyright added
MistaTwista e4da8dc
add type annotations
MistaTwista 0adf20d
better code separation and Disposable usage
MistaTwista 67c1ff5
change let to const
MistaTwista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
extension/src/goDebugCommands.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
/** | ||
* Registers commands to improve the debugging experience for Go. | ||
* | ||
* Currently, it adds a command to open a variable in a new text document. | ||
*/ | ||
export function registerGoDebugCommands(ctx: vscode.ExtensionContext) { | ||
// Track sessions since vscode doesn't provide a list of them. | ||
const sessions = new Map<string, vscode.DebugSession>(); | ||
|
||
ctx.subscriptions.push( | ||
vscode.debug.onDidStartDebugSession((s) => sessions.set(s.id, s)), | ||
vscode.debug.onDidTerminateDebugSession((s) => sessions.delete(s.id)), | ||
vscode.workspace.registerTextDocumentContentProvider('go-debug-variable', new VariableContentProvider(sessions)), | ||
vscode.commands.registerCommand('go.debug.openVariableAsDoc', async (ref: VariableRef) => { | ||
const uri = VariableContentProvider.uriForRef(ref); | ||
const doc = await vscode.workspace.openTextDocument(uri); | ||
await vscode.window.showTextDocument(doc); | ||
}) | ||
); | ||
} | ||
|
||
class VariableContentProvider implements vscode.TextDocumentContentProvider { | ||
sessions: Map<string, vscode.DebugSession> | ||
|
||
constructor(sessionsSet: Map<string, vscode.DebugSession>) { | ||
this.sessions = sessionsSet; | ||
} | ||
|
||
static uriForRef(ref: VariableRef) { | ||
return vscode.Uri.from({ | ||
scheme: 'go-debug-variable', | ||
authority: `${ref.container.variablesReference}@${ref.sessionId}`, | ||
path: `/${ref.variable.name}` | ||
}); | ||
} | ||
|
||
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> { | ||
const name = uri.path.replace(/^\//, ''); | ||
const [container, sessionId] = uri.authority.split('@', 2); | ||
if (!container || !sessionId) { | ||
throw new Error('Invalid URI'); | ||
} | ||
|
||
const session = this.sessions.get(sessionId); | ||
if (!session) return 'Debug session has been terminated'; | ||
|
||
const { variables } = await session.customRequest('variables', { | ||
variablesReference: parseInt(container, 10) | ||
}) as { variables: Variable[] }; | ||
|
||
const v = variables.find(v => v.name === name); | ||
if (!v) return `Cannot resolve variable ${name}`; | ||
|
||
if (!v.memoryReference) { | ||
const { result } = await session.customRequest('evaluate', { | ||
expression: v.evaluateName, | ||
context: 'clipboard' | ||
}) as { result: string }; | ||
|
||
v.value = result ?? v.value; | ||
|
||
return parseVariable(v); | ||
} | ||
|
||
const chunk = 1 << 14; | ||
let offset = 0; | ||
const full: Uint8Array[] = []; | ||
|
||
while (true) { | ||
const resp = await session.customRequest('readMemory', { | ||
memoryReference: v.memoryReference, | ||
offset, | ||
count: chunk | ||
}) as { address: string; data: string; unreadableBytes: number }; | ||
|
||
if (!resp.data) break; | ||
full.push(Buffer.from(resp.data, 'base64')); | ||
|
||
if (resp.unreadableBytes === 0) break; | ||
offset += chunk; | ||
} | ||
|
||
return Buffer.concat(full).toString('utf-8'); | ||
} | ||
} | ||
|
||
/** | ||
* A reference to a variable, used to pass data between commands. | ||
*/ | ||
interface VariableRef { | ||
sessionId: string; | ||
container: Container; | ||
variable: Variable; | ||
} | ||
|
||
/** | ||
* A container for variables, used to pass data between commands. | ||
*/ | ||
interface Container { | ||
name: string; | ||
variablesReference: number; | ||
expensive: boolean; | ||
} | ||
|
||
/** | ||
* A variable, used to pass data between commands. | ||
*/ | ||
interface Variable { | ||
name: string; | ||
value: string; | ||
evaluateName: string; | ||
variablesReference: number; | ||
memoryReference?: string; | ||
} | ||
|
||
const escapeCodes: Record<string, string> = { | ||
r: '\r', | ||
n: '\n', | ||
t: '\t' | ||
}; | ||
|
||
/** | ||
* Parses a variable value, unescaping special characters. | ||
*/ | ||
function parseVariable(variable: Variable) { | ||
const raw = variable.value.trim(); | ||
|
||
try { | ||
return JSON.parse(raw); | ||
} catch (_) { | ||
return raw.replace(/\\[nrt\\"'`]/, (_, s) => (s in escapeCodes ? escapeCodes[s] : s)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.