Repository: llvm/llvm-project

Issue:
Enhancement Request: Hide or collapse the modules window until we get a modules event in the LLDB-DAP extension
Not sure if this is even possible, but it would be nice if we could hide the modules window or keep it collapsed until we see a modules event. Older versions of lldb-dap don't emit module events which results in a empty window taking up screen real estate in the debug pane.


Discussion:

@llvm/issue-subscribers-lldb-dap

Author: Jonas Devlieghere (JDevlieghere)

<details>
Not sure if this is even possible, but it would be nice if we could hide the modules window or keep it collapsed until we see a modules event. Older versions of lldb-dap don't emit module events which results in a empty window taking up screen real estate in the debug pane.
</details>

Yeah was bugging me also, working on the fix.

Patch:
diff --git a/lldb/tools/lldb-dap/package.json b/lldb/tools/lldb-dap/package.json
index d5ca604798799..7f90d5535b54b 100644
--- a/lldb/tools/lldb-dap/package.json
+++ b/lldb/tools/lldb-dap/package.json
@@ -787,7 +787,7 @@
         {
           "id": "lldb-dap.modules",
           "name": "Modules",
-          "when": "inDebugMode && debugType == 'lldb-dap'",
+          "when": "inDebugMode && debugType == 'lldb-dap' && lldb-dap.showModules",
           "icon": "$(symbol-module)"
         }
       ]
diff --git a/lldb/tools/lldb-dap/src-ts/debug-session-tracker.ts b/lldb/tools/lldb-dap/src-ts/debug-session-tracker.ts
index 1ce190938d9c7..50db1e1c3a7b0 100644
--- a/lldb/tools/lldb-dap/src-ts/debug-session-tracker.ts
+++ b/lldb/tools/lldb-dap/src-ts/debug-session-tracker.ts
@@ -35,14 +35,24 @@ export class DebugSessionTracker
    * The modules are kept in an array to maintain the load order of the modules.
    */
   private modules = new Map<vscode.DebugSession, DebugProtocol.Module[]>();
-  private modulesChanged = new vscode.EventEmitter<void>();
+  private modulesChanged = new vscode.EventEmitter<
+    vscode.DebugSession | undefined
+  >();
 
   /**
    * Fired when modules are changed for any active debug session.
    *
    * Use `debugSessionModules` to retieve the active modules for a given debug session.
    */
-  onDidChangeModules: vscode.Event<void> = this.modulesChanged.event;
+  onDidChangeModules: vscode.Event<vscode.DebugSession | undefined> =
+    this.modulesChanged.event;
+
+  constructor() {
+    this.onDidChangeModules(this.moduleChangedListener, this);
+    vscode.debug.onDidChangeActiveDebugSession((session) =>
+      this.modulesChanged.fire(session),
+    );
+  }
 
   dispose() {
     this.modules.clear();
@@ -70,7 +80,27 @@ export class DebugSessionTracker
   /** Clear information from the active session. */
   private onExit(session: vscode.DebugSession) {
     this.modules.delete(session);
-    this.modulesChanged.fire();
+    this.modulesChanged.fire(undefined);
+  }
+
+  private showModulesTreeView(showModules: boolean) {
+    vscode.commands.executeCommand(
+      "setContext",
+      "lldb-dap.showModules",
+      showModules,
+    );
+  }
+
+  private moduleChangedListener(session: vscode.DebugSession | undefined) {
+    if (!session) {
+      this.showModulesTreeView(false);
+      return;
+    }
+
+    if (session == vscode.debug.activeDebugSession) {
+      const sessionHasModules = this.modules.get(session) != undefined;
+      this.showModulesTreeView(sessionHasModules);
+    }
   }
 
   private onDidSendMessage(
@@ -103,7 +133,7 @@ export class DebugSessionTracker
           break;
       }
       this.modules.set(session, modules);
-      this.modulesChanged.fire();
+      this.modulesChanged.fire(session);
     }
   }
 }
diff --git a/lldb/tools/lldb-dap/src-ts/ui/modules-data-provider.ts b/lldb/tools/lldb-dap/src-ts/ui/modules-data-provider.ts
index 091c1d69ac647..d0fb9270c734f 100644
--- a/lldb/tools/lldb-dap/src-ts/ui/modules-data-provider.ts
+++ b/lldb/tools/lldb-dap/src-ts/ui/modules-data-provider.ts
@@ -52,9 +52,6 @@ export class ModulesDataProvider implements vscode.TreeDataProvider<TreeData> {
 
   constructor(private readonly tracker: DebugSessionTracker) {
     tracker.onDidChangeModules(() => this.changeTreeData.fire());
-    vscode.debug.onDidChangeActiveDebugSession(() =>
-      this.changeTreeData.fire(),
-    );
   }
 
   getTreeItem(module: TreeData): vscode.TreeItem {