microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debugConfigurationProvider.ts
100lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as vscode from "vscode"; |
| 5 | import { TelemetryHelper } from "../common/telemetryHelper"; |
| 6 | import { Telemetry } from "../common/telemetry"; |
| 7 | import * as nls from "vscode-nls"; |
| 8 | const localize = nls.loadMessageBundle(); |
| 9 | |
| 10 | export class ReactNativeDebugConfigProvider implements vscode.DebugConfigurationProvider { |
| 11 | private debugConfigurations = { |
| 12 | "Debug Android": { |
| 13 | "name": "Debug Android", |
| 14 | "cwd": "${workspaceFolder}", |
| 15 | "type": "reactnative", |
| 16 | "request": "launch", |
| 17 | "platform": "android", |
| 18 | }, |
| 19 | "Debug iOS": { |
| 20 | "name": "Debug iOS", |
| 21 | "cwd": "${workspaceFolder}", |
| 22 | "type": "reactnative", |
| 23 | "request": "launch", |
| 24 | "platform": "ios", |
| 25 | }, |
| 26 | "Attach to packager": { |
| 27 | "name": "Attach to packager", |
| 28 | "cwd": "${workspaceFolder}", |
| 29 | "type": "reactnative", |
| 30 | "request": "attach", |
| 31 | }, |
| 32 | "Debug in Exponent": { |
| 33 | "name": "Debug in Exponent", |
| 34 | "cwd": "${workspaceFolder}", |
| 35 | "type": "reactnative", |
| 36 | "request": "launch", |
| 37 | "platform": "exponent", |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | private pickConfig: ReadonlyArray<vscode.QuickPickItem> = [ |
| 42 | { |
| 43 | label: "Debug Android", |
| 44 | description: localize("DebugAndroidConfigDesc", "Run and debug Android application"), |
| 45 | }, |
| 46 | { |
| 47 | label: "Debug iOS", |
| 48 | description: localize("DebugiOSConfigDesc", "Run and debug iOS application"), |
| 49 | }, |
| 50 | { |
| 51 | label: "Attach to packager", |
| 52 | description: localize("AttachToPackagerConfigDesc", "Attach to already working application packager"), |
| 53 | }, |
| 54 | { |
| 55 | label: "Debug in Exponent", |
| 56 | description: localize("DebugExpoConfigDesc", "Debug Expo application or React Native application in Expo"), |
| 57 | }, |
| 58 | ]; |
| 59 | |
| 60 | public async provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration[]> { |
| 61 | return new Promise<vscode.DebugConfiguration[]>((resolve) => { |
| 62 | const configPicker = this.prepareDebugConfigPicker(); |
| 63 | const disposables: vscode.Disposable[] = []; |
| 64 | const pickHandler = () => { |
| 65 | let chosenConfigsEvent = TelemetryHelper.createTelemetryEvent("chosenDebugConfigurations"); |
| 66 | let selected: string[] = configPicker.selectedItems.map(element => element.label); |
| 67 | chosenConfigsEvent.properties["selectedItems"] = selected; |
| 68 | Telemetry.send(chosenConfigsEvent); |
| 69 | const launchConfig = this.gatherDebugScenarios(selected); |
| 70 | disposables.forEach(d => d.dispose()); |
| 71 | resolve(launchConfig); |
| 72 | }; |
| 73 | |
| 74 | disposables.push( |
| 75 | configPicker.onDidAccept(pickHandler), |
| 76 | configPicker.onDidHide(pickHandler), |
| 77 | configPicker |
| 78 | ); |
| 79 | |
| 80 | configPicker.show(); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | private gatherDebugScenarios(selectedItems: string[]): vscode.DebugConfiguration[] { |
| 85 | let launchConfig: vscode.DebugConfiguration[] = selectedItems.map(element => this.debugConfigurations[element]); |
| 86 | return launchConfig; |
| 87 | } |
| 88 | |
| 89 | private prepareDebugConfigPicker(): vscode.QuickPick<vscode.QuickPickItem> { |
| 90 | const debugConfigPicker = vscode.window.createQuickPick(); |
| 91 | debugConfigPicker.canSelectMany = true; |
| 92 | debugConfigPicker.ignoreFocusOut = true; |
| 93 | debugConfigPicker.title = localize("DebugConfigQuickPickLabel", "Pick debug configurations"); |
| 94 | debugConfigPicker.items = this.pickConfig; |
| 95 | // QuickPickItem property `picked` doesn't work, so this line will check first item in the list |
| 96 | // which is supposed to be Debug Android |
| 97 | debugConfigPicker.selectedItems = [this.pickConfig[0]]; |
| 98 | return debugConfigPicker; |
| 99 | } |
| 100 | } |