From 9ecd716c1a95d40baea1f1f4229810faa3c74f9b Mon Sep 17 00:00:00 2001 From: WyoTwT Date: Tue, 30 Apr 2024 23:23:20 +0200 Subject: [PATCH] Conditional 'Show In Memory Inspector' for Variable Add new conditional for context menu showing Memory Inspector for a variable because some DAPs may not support it. Signed-off-by: Thor Thayer --- package.json | 4 ++-- src/common/messaging.ts | 1 + src/plugin/adapter-registry/adapter-capabilities.ts | 3 +++ src/plugin/adapter-registry/amalgamator-gdb-tracker.ts | 4 ++++ src/plugin/memory-provider.ts | 6 ++++++ src/plugin/memory-webview-main.ts | 5 ++++- src/webview/memory-webview-view.tsx | 3 ++- 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8e4c893..aa0ec49 100644 --- a/package.json +++ b/package.json @@ -148,7 +148,7 @@ "debug/variables/context": [ { "command": "memory-inspector.show-variable", - "when": "canViewMemory && memory-inspector.canRead" + "when": "canViewMemory && memory-inspector.canReadVariable" }, { "command": "memory-inspector.go-to-value", @@ -162,7 +162,7 @@ "view/item/context": [ { "command": "memory-inspector.show-variable", - "when": "canViewMemory && memory-inspector.canRead" + "when": "canViewMemory && memory-inspector.canReadVariable" } ], "explorer/context": [ diff --git a/src/common/messaging.ts b/src/common/messaging.ts index 48fdcde..0b6534d 100644 --- a/src/common/messaging.ts +++ b/src/common/messaging.ts @@ -48,6 +48,7 @@ export interface SessionContext { canRead: boolean; canWrite: boolean; stopped?: boolean; + canReadVariable: boolean; } // Notifications diff --git a/src/plugin/adapter-registry/adapter-capabilities.ts b/src/plugin/adapter-registry/adapter-capabilities.ts index bc51355..a6ca4c4 100644 --- a/src/plugin/adapter-registry/adapter-capabilities.ts +++ b/src/plugin/adapter-registry/adapter-capabilities.ts @@ -36,6 +36,7 @@ export interface AdapterCapabilities { writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: Context): Promise; getContexts?(session: vscode.DebugSession): Promise; getCurrentContext?(session: vscode.DebugSession): Promise; + supportShowVariables?(session: vscode.DebugSession): boolean; } export type WithChildren = Original & { children?: Array> }; @@ -143,6 +144,8 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker { getContexts?(session: vscode.DebugSession): Promise; getCurrentContext?(session: vscode.DebugSession): Promise; + + supportShowVariables?(session: vscode.DebugSession): boolean; } export class VariableTracker implements AdapterCapabilities { diff --git a/src/plugin/adapter-registry/amalgamator-gdb-tracker.ts b/src/plugin/adapter-registry/amalgamator-gdb-tracker.ts index 9176ed1..46c5b0b 100644 --- a/src/plugin/adapter-registry/amalgamator-gdb-tracker.ts +++ b/src/plugin/adapter-registry/amalgamator-gdb-tracker.ts @@ -57,6 +57,10 @@ export class AmalgamatorSessionManager extends VariableTracker implements Adapte async getCurrentContext(session: vscode.DebugSession): Promise { return this.sessions.get(session.id)?.getCurrentContext?.(session); } + + supportShowVariables(_session: vscode.DebugSession): boolean { + return false; + } } export class AmalgamatorGdbVariableTransformer extends AdapterVariableTracker { diff --git a/src/plugin/memory-provider.ts b/src/plugin/memory-provider.ts index 24a3c3f..2c1649c 100644 --- a/src/plugin/memory-provider.ts +++ b/src/plugin/memory-provider.ts @@ -111,4 +111,10 @@ export class MemoryProvider { const handler = this.adapterRegistry?.getHandlerForSession(session.type); return handler?.getCurrentContext?.(session); } + + public supportShowVariables(): boolean { + const session = this.sessionTracker.assertActiveSession('supports show variables'); + const handler = this.adapterRegistry?.getHandlerForSession(session.type); + return handler?.supportShowVariables?.(session) ?? true; + } } diff --git a/src/plugin/memory-webview-main.ts b/src/plugin/memory-webview-main.ts index f39e1c3..fd8cd73 100644 --- a/src/plugin/memory-webview-main.ts +++ b/src/plugin/memory-webview-main.ts @@ -284,11 +284,14 @@ export class MemoryWebview implements vscode.CustomReadonlyEditorProvider { protected createContext(session = this.sessionTracker.activeSession): SessionContext { const sessionId = session?.id; + const canReadVariables = !!this.sessionTracker.hasDebugCapability(session, 'supportsReadMemoryRequest') && + !!this.memoryProvider.supportShowVariables(); return { sessionId, canRead: !!this.sessionTracker.hasDebugCapability(session, 'supportsReadMemoryRequest'), canWrite: !!this.sessionTracker.hasDebugCapability(session, 'supportsWriteMemoryRequest'), - stopped: this.sessionTracker.isStopped(session) + stopped: this.sessionTracker.isStopped(session), + canReadVariable: !!this.sessionTracker.hasDebugCapability(session, 'supportsReadMemoryRequest') && !!canReadVariables }; } diff --git a/src/webview/memory-webview-view.tsx b/src/webview/memory-webview-view.tsx index 8661dd6..d130e8e 100644 --- a/src/webview/memory-webview-view.tsx +++ b/src/webview/memory-webview-view.tsx @@ -74,7 +74,8 @@ export interface MemoryAppState extends MemoryState, MemoryDisplayConfiguration export const DEFAULT_SESSION_CONTEXT: SessionContext = { canRead: false, - canWrite: false + canWrite: false, + canReadVariable: false }; export const DEFAULT_MEMORY_DISPLAY_CONFIGURATION: MemoryDisplayConfiguration = {