Repository: microsoft/vscode-edge-devtools
Pull request #219 (2020-10-13 19:03:25)

Re-disable quick open menu and filter command menu options
Quick open and command menu should be disabled via this commit: https://github.com/microsoft/vscode-edge-devtools/pull/131

This PR re-disables the Quick open menu, but leaves the command menu available with filtered commands.  The commands will contain the commands from the available tabs (Only Elements commands when Network is disabled and Elements and Network commands when Network is enabled.)

![command-menu](https://user-images.githubusercontent.com/14304598/97610989-d3aba480-19d2-11eb-9cf1-f39ecb10c20d.gif)



Patch:
diff --git a/postBuildStep.ts b/postBuildStep.ts
index 55bb860..c0ba47a 100644
--- a/postBuildStep.ts
+++ b/postBuildStep.ts
@@ -9,8 +9,9 @@ import applyRuntimeImportScriptPathPrefixPatch from "./src/host/polyfills/runtim
 import {
     applyAppendTabPatch,
     applyCommonRevealerPatch,
+    applyCommandMenuPatch,
     applyDrawerTabLocationPatch,
-    applyHandleActionPatch,
+    applyQuickOpenPatch,
     applyInspectorCommonContextMenuPatch,
     applyInspectorCommonCssPatch,
     applyInspectorCommonCssRightToolbarPatch,
@@ -121,11 +122,9 @@ async function patchFilesForWebView(toolsOutDir: string) {
         applyRuntimeImportScriptPathPrefixPatch,
     ]);
 
-    await patchFileForWebViewWrapper("ui/ui.js", toolsOutDir, [
-        applyHandleActionPatch,
-    ]);
-    await patchFileForWebViewWrapper("ui/ui.js", toolsOutDir, [
-        applyHandleActionPatch,
+    await patchFileForWebViewWrapper("quick_open/quick_open.js", toolsOutDir, [
+        applyCommandMenuPatch,
+        applyQuickOpenPatch,
     ]);
     await patchFileForWebViewWrapper("browser_debugger/browser_debugger.js", toolsOutDir, [
         applyRemoveBreakOnContextMenuItem,
diff --git a/src/host/polyfills/simpleView.ts b/src/host/polyfills/simpleView.ts
index 72ccf90..a8fb0d9 100644
--- a/src/host/polyfills/simpleView.ts
+++ b/src/host/polyfills/simpleView.ts
@@ -43,14 +43,58 @@ export function applyCommonRevealerPatch(content: string) {
     }
 }
 
-export function applyHandleActionPatch(content: string) {
-    // This patch removes the ability to use the
-    // quick open menu (CTRL + P) and command menu (CTRL + SHIFT + P)
-    const pattern = /handleAction\(context,\s*actionId\)\s*{/g;
+export function applyQuickOpenPatch(content: string) {
+    // This patch removes the ability to use the quick open menu (CTRL + P)
+    const pattern = /handleAction\(context,actionId\){switch\(actionId\)/;
 
     if (content.match(pattern)) {
         return content
-        .replace(pattern, "handleAction(context, actionId) { return false;");
+        .replace(pattern, "handleAction(context, actionId) { actionId = null; switch(actionId)");
+    } else {
+        return null;
+    }
+}
+
+export function applyCommandMenuPatch(content: string) {
+    // This patch modifies the available options in the command menu.
+    const functionPattern = /attach\(\){const allCommands/;
+    if (content.match(functionPattern)) {
+        content = content.replace(functionPattern, `${getApprovedTabs.toString().slice(9)} attach(){const allCommands`);
+    } else {
+        return null;
+    }
+
+    // pattern intended to match logic of CommandMenu.attach()
+    const pattern = /for\(const action of actions\){const category=action[\s\S]+this\._commands\.sort\(commandComparator\);/;
+    if(content.match(pattern)) {
+        return content.replace(pattern, `this.getApprovedTabs((networkSettings) => {
+            const networkEnabled = networkSettings.enableNetwork;
+            for (const action of actions) {
+              const category = action.category();
+              if (!category) {
+                continue;
+              }
+              let condition = (category !== 'Elements' || action.title() === 'Show DOM Breakpoints');
+              if (networkEnabled) {
+                condition = condition && category !== 'Network';
+              }
+              if (!condition) {
+                const options = {action, userActionCode: undefined};
+                this._commands.push(CommandMenu.createActionCommand(options));
+              }
+            }
+            for (const command of allCommands) {
+              let condition = (command.category() !== 'Elements' || command.title() === 'Show DOM Breakpoints');
+              if (networkEnabled) {
+                condition = condition && command.category() !== 'Network';
+              }
+              if (!condition && command.available()) {
+                this._commands.push(command);
+              }
+            }
+            this._commands = this._commands.sort(commandComparator);
+            this._refreshCallback();
+          });`);
     } else {
         return null;
     }


Test patch:
diff --git a/src/host/polyfills/simpleView.test.ts b/src/host/polyfills/simpleView.test.ts
index c077443..c6076bc 100644
--- a/src/host/polyfills/simpleView.test.ts
+++ b/src/host/polyfills/simpleView.test.ts
@@ -37,32 +37,32 @@ describe("simpleView", () => {
             expect.stringContaining("let reveal = function revealInVSCode(revealable, omitFocus) {"));
     });
 
-    it("applyHandleActionPatch correctly changes handleAction text for Quick Open", async () => {
-        const filePath = "ui/ui.js";
+    it("applyQuickOpenPatch correctly changes handleAction text for Quick Open", async () => {
+        const filePath = "quick_open/quick_open.js";
         const fileContents = getTextFromFile(filePath);
         if (!fileContents) {
             throw new Error(`Could not find file: ${filePath}`);
         }
 
         const apply = await import("./simpleView");
-        const result = apply.applyHandleActionPatch(fileContents);
+        const result = apply.applyQuickOpenPatch(fileContents);
         expect(result).not.toEqual(null);
         expect(result).toEqual(
-            expect.stringContaining("handleAction(context, actionId) { return false;"));
+            expect.stringContaining("handleAction(context, actionId) { actionId = null; switch(actionId)"));
     });
 
-    it("applyHandleActionPatch correctly changes handleAction text for Command Menu", async () => {
-        const filePath = "ui/ui.js";
+    it("applyCommandMenuPatch correctly changes attach text for command menu", async () => {
+        const filePath = "quick_open/quick_open.js";
         const fileContents = getTextFromFile(filePath);
         if (!fileContents) {
             throw new Error(`Could not find file: ${filePath}`);
         }
 
         const apply = await import("./simpleView");
-        const result = apply.applyHandleActionPatch(fileContents);
+        const result = apply.applyCommandMenuPatch(fileContents);
         expect(result).not.toEqual(null);
         expect(result).toEqual(
-            expect.stringContaining("handleAction(context, actionId) { return false;"));
+            expect.stringContaining("this.getApprovedTabs((networkSettings)"));
     });
 
     it("applyInspectorViewPatch correctly changes _showDrawer text", async () => {