Path: blob/main/src/vs/workbench/contrib/debug/browser/debugSessionPicker.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import * as nls from '../../../../nls.js';5import { matchesFuzzy } from '../../../../base/common/filters.js';6import { DisposableStore } from '../../../../base/common/lifecycle.js';7import { IDebugService, IDebugSession, REPL_VIEW_ID } from '../common/debug.js';8import { IQuickInputService, IQuickPickSeparator } from '../../../../platform/quickinput/common/quickInput.js';910import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';11import { IPickerDebugItem } from '../common/loadedScriptsPicker.js';12import { IViewsService } from '../../../services/views/common/viewsService.js';13import { ICommandService } from '../../../../platform/commands/common/commands.js';141516export async function showDebugSessionMenu(accessor: ServicesAccessor, selectAndStartID: string) {17const quickInputService = accessor.get(IQuickInputService);18const debugService = accessor.get(IDebugService);19const viewsService = accessor.get(IViewsService);20const commandService = accessor.get(ICommandService);2122const localDisposableStore = new DisposableStore();23const quickPick = quickInputService.createQuickPick<IPickerDebugItem>({ useSeparators: true });24localDisposableStore.add(quickPick);25quickPick.matchOnLabel = quickPick.matchOnDescription = quickPick.matchOnDetail = quickPick.sortByLabel = false;26quickPick.placeholder = nls.localize('moveFocusedView.selectView', 'Search debug sessions by name');2728const pickItems = _getPicksAndActiveItem(quickPick.value, selectAndStartID, debugService, viewsService, commandService);29quickPick.items = pickItems.picks;30quickPick.activeItems = pickItems.activeItems;3132localDisposableStore.add(quickPick.onDidChangeValue(async () => {33quickPick.items = _getPicksAndActiveItem(quickPick.value, selectAndStartID, debugService, viewsService, commandService).picks;34}));35localDisposableStore.add(quickPick.onDidAccept(() => {36const selectedItem = quickPick.selectedItems[0];37selectedItem.accept();38quickPick.hide();39localDisposableStore.dispose();40}));41quickPick.show();42}4344function _getPicksAndActiveItem(filter: string, selectAndStartID: string, debugService: IDebugService, viewsService: IViewsService, commandService: ICommandService): { picks: Array<IPickerDebugItem | IQuickPickSeparator>; activeItems: Array<IPickerDebugItem> } {45const debugConsolePicks: Array<IPickerDebugItem | IQuickPickSeparator> = [];46const headerSessions: IDebugSession[] = [];4748const currSession = debugService.getViewModel().focusedSession;49const sessions = debugService.getModel().getSessions(false);50const activeItems: Array<IPickerDebugItem> = [];5152sessions.forEach((session) => {53if (session.compact && session.parentSession) {54headerSessions.push(session.parentSession);55}56});5758sessions.forEach((session) => {59const isHeader = headerSessions.includes(session);60if (!session.parentSession) {61debugConsolePicks.push({ type: 'separator', label: isHeader ? session.name : undefined });62}6364if (!isHeader) {65const pick = _createPick(session, filter, debugService, viewsService, commandService);66if (pick) {67debugConsolePicks.push(pick);68if (session.getId() === currSession?.getId()) {69activeItems.push(pick);70}71}72}73});7475if (debugConsolePicks.length) {76debugConsolePicks.push({ type: 'separator' });77}7879const createDebugSessionLabel = nls.localize('workbench.action.debug.startDebug', 'Start a New Debug Session');80debugConsolePicks.push({81label: `$(plus) ${createDebugSessionLabel}`,82ariaLabel: createDebugSessionLabel,83accept: () => commandService.executeCommand(selectAndStartID)84});8586return { picks: debugConsolePicks, activeItems };87}888990function _getSessionInfo(session: IDebugSession): { label: string; description: string; ariaLabel: string } {91const label = (!session.configuration.name.length) ? session.name : session.configuration.name;92const parentName = session.compact ? undefined : session.parentSession?.configuration.name;93let description = '';94let ariaLabel = '';95if (parentName) {96ariaLabel = nls.localize('workbench.action.debug.spawnFrom', 'Session {0} spawned from {1}', label, parentName);97description = parentName;98}99100return { label, description, ariaLabel };101}102103function _createPick(session: IDebugSession, filter: string, debugService: IDebugService, viewsService: IViewsService, commandService: ICommandService): IPickerDebugItem | undefined {104const pickInfo = _getSessionInfo(session);105const highlights = matchesFuzzy(filter, pickInfo.label, true);106if (highlights) {107return {108label: pickInfo.label,109description: pickInfo.description,110ariaLabel: pickInfo.ariaLabel,111highlights: { label: highlights },112accept: () => {113debugService.focusStackFrame(undefined, undefined, session, { explicit: true });114if (!viewsService.isViewVisible(REPL_VIEW_ID)) {115viewsService.openView(REPL_VIEW_ID, true);116}117}118};119}120return undefined;121}122123124125126