Skip to content

Commit

Permalink
Conditional 'Show In Memory Inspector' for Variable
Browse files Browse the repository at this point in the history
Add new conditional for context menu showing Memory Inspector for a
variable because some DAPs may not support it.

Signed-off-by: Thor Thayer <[email protected]>
  • Loading branch information
WyoTwT committed May 8, 2024
1 parent b531f77 commit d2fef45
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"debug/variables/context": [
{
"command": "memory-inspector.show-variable",
"when": "canViewMemory && memory-inspector.canRead"
"when": "canViewMemory && memory-inspector.canReadVariable"
},
{
"command": "memory-inspector.store-file",
Expand All @@ -152,7 +152,7 @@
"view/item/context": [
{
"command": "memory-inspector.show-variable",
"when": "canViewMemory && memory-inspector.canRead"
"when": "canViewMemory && memory-inspector.canReadVariable"
}
],
"explorer/context": [
Expand Down
1 change: 1 addition & 0 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface SessionContext {
sessionId?: string;
canRead: boolean;
canWrite: boolean;
canReadVariable: boolean;
}

// Notifications
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/adapter-registry/adapter-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface AdapterCapabilities {
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: Context): Promise<WriteMemoryResult>;
getContexts?(session: vscode.DebugSession): Promise<Context[]>;
getCurrentContext?(session: vscode.DebugSession): Promise<Context | undefined>;
supportShowVariables?(session: vscode.DebugSession): boolean;
}

export type WithChildren<Original> = Original & { children?: Array<WithChildren<DebugProtocol.Variable>> };
Expand Down Expand Up @@ -143,6 +144,8 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {

getContexts?(session: vscode.DebugSession): Promise<Context[]>;
getCurrentContext?(session: vscode.DebugSession): Promise<Context | undefined>;

supportShowVariables?(session: vscode.DebugSession): boolean;
}

export class VariableTracker implements AdapterCapabilities {
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/adapter-registry/amalgamator-gdb-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class AmalgamatorSessionManager extends VariableTracker implements Adapte
async getCurrentContext(session: vscode.DebugSession): Promise<Context | undefined> {
return this.sessions.get(session.id)?.getCurrentContext?.(session);
}

supportShowVariables(_session: vscode.DebugSession): boolean {
return false;
}
}

export class AmalgamatorGdbVariableTransformer extends AdapterVariableTracker {
Expand Down
12 changes: 11 additions & 1 deletion src/plugin/memory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface LabeledUint8Array extends Uint8Array {
export class MemoryProvider {
public static ReadKey = `${manifest.PACKAGE_NAME}.canRead`;
public static WriteKey = `${manifest.PACKAGE_NAME}.canWrite`;
public static ReadVariableKey = `${manifest.PACKAGE_NAME}.canReadVariable`;

private _onDidStopDebug = new vscode.EventEmitter<vscode.DebugSession>();
public readonly onDidStopDebug = this._onDidStopDebug.event;
Expand Down Expand Up @@ -105,17 +106,20 @@ export class MemoryProvider {
createContext(session = vscode.debug.activeDebugSession): SessionContext {
const sessionId = session?.id;
const capabilities = sessionId ? this.sessionDebugCapabilities.get(sessionId) : undefined;
const canReadVariable = this.supportShowVariables();
return {
sessionId,
canRead: !!capabilities?.supportsReadMemoryRequest,
canWrite: !!capabilities?.supportsWriteMemoryRequest
canWrite: !!capabilities?.supportsWriteMemoryRequest,
canReadVariable: !!capabilities?.supportsReadMemoryRequest && !!canReadVariable
};
}

protected setContext(session?: vscode.DebugSession): void {
const newContext = this.createContext(session);
vscode.commands.executeCommand('setContext', MemoryProvider.ReadKey, newContext.canRead);
vscode.commands.executeCommand('setContext', MemoryProvider.WriteKey, newContext.canWrite);
vscode.commands.executeCommand('setContext', MemoryProvider.ReadVariableKey, newContext.canReadVariable);
this._onDidChangeSessionContext.fire(newContext);
}

Expand Down Expand Up @@ -206,4 +210,10 @@ export class MemoryProvider {
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.getCurrentContext?.(session);
}

public supportShowVariables(): boolean {
const session = this.assertActiveSession('supports show variables');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.supportShowVariables?.(session) ?? true;
}
}
3 changes: 2 additions & 1 deletion src/webview/memory-webview-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export interface MemoryAppState extends MemoryState, MemoryDisplayConfiguration

const DEFAULT_SESSION_CONTEXT: SessionContext = {
canRead: false,
canWrite: false
canWrite: false,
canReadVariable: false
};

const MEMORY_DISPLAY_CONFIGURATION_DEFAULTS: MemoryDisplayConfiguration = {
Expand Down

0 comments on commit d2fef45

Please sign in to comment.