|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { inject, injectable } from 'inversify'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import * as nls from 'vscode-nls'; |
| 8 | +import { Commands, DebugType, registerCommand } from '../common/contributionUtils'; |
| 9 | +import { IExtensionContribution } from '../ioc-extras'; |
| 10 | +import { BrowserTargetType } from '../targets/browser/browserTargets'; |
| 11 | +import { DebugSessionTracker } from './debugSessionTracker'; |
| 12 | + |
| 13 | +const localize = nls.loadMessageBundle(); |
| 14 | + |
| 15 | +const qualifies = (session: vscode.DebugSession) => { |
| 16 | + if (session?.type !== DebugType.Edge) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + const type: BrowserTargetType = session.configuration.__browserTargetType; |
| 21 | + return type === BrowserTargetType.IFrame || type === BrowserTargetType.Page; |
| 22 | +}; |
| 23 | + |
| 24 | +const toolExtensionId = 'ms-edgedevtools.vscode-edge-devtools'; |
| 25 | +const commandId = 'vscode-edge-devtools.attachToCurrentDebugTarget'; |
| 26 | + |
| 27 | +@injectable() |
| 28 | +export class EdgeDevToolOpener implements IExtensionContribution { |
| 29 | + constructor(@inject(DebugSessionTracker) private readonly tracker: DebugSessionTracker) {} |
| 30 | + |
| 31 | + /** @inheritdoc */ |
| 32 | + public register(context: vscode.ExtensionContext) { |
| 33 | + context.subscriptions.push( |
| 34 | + registerCommand(vscode.commands, Commands.OpenEdgeDevTools, async () => { |
| 35 | + if (!vscode.extensions.all.some(e => e.id === toolExtensionId)) { |
| 36 | + return vscode.commands.executeCommand( |
| 37 | + 'workbench.extensions.action.showExtensionsWithIds', |
| 38 | + [toolExtensionId], |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + const session = |
| 43 | + vscode.debug.activeDebugSession && qualifies(vscode.debug.activeDebugSession) |
| 44 | + ? vscode.debug.activeDebugSession |
| 45 | + : await DebugSessionTracker.pickSession( |
| 46 | + this.tracker.getConcreteSessions().filter(qualifies), |
| 47 | + localize( |
| 48 | + 'selectEdgeToolSession', |
| 49 | + 'Select the page where you want to open the devtools', |
| 50 | + ), |
| 51 | + ); |
| 52 | + |
| 53 | + if (!session) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + return vscode.commands.executeCommand(commandId, session.id); |
| 58 | + }), |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
0 commit comments