|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import * as nls from 'vs/nls'; |
| 6 | +import { matchesFuzzy } from 'vs/base/common/filters'; |
| 7 | +import { Source } from 'vs/workbench/contrib/debug/common/debugSource'; |
| 8 | +import { IQuickInputService, IQuickPickItem, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput'; |
| 9 | +import { IDebugService, IDebugSession } from 'vs/workbench/contrib/debug/common/debug'; |
| 10 | +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; |
| 11 | +import { getIconClasses } from 'vs/editor/common/services/getIconClasses'; |
| 12 | +import { IModelService } from 'vs/editor/common/services/model'; |
| 13 | +import { ILanguageService } from 'vs/editor/common/languages/language'; |
| 14 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 15 | + |
| 16 | +import { dirname } from 'vs/base/common/resources'; |
| 17 | +import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; |
| 18 | +import { ILabelService } from 'vs/platform/label/common/label'; |
| 19 | + |
| 20 | + |
| 21 | +interface IPickerLoadedScriptItem extends IQuickPickItem { |
| 22 | + accept(): void; |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * This function takes a regular quickpick and makes one for loaded scripts that has persistent headers |
| 28 | + * e.g. when some picks are filtered out, the ones that are visible still have its header. |
| 29 | + */ |
| 30 | +export async function showLoadedScriptMenu(accessor: ServicesAccessor) { |
| 31 | + const quickInputService = accessor.get(IQuickInputService); |
| 32 | + const debugService = accessor.get(IDebugService); |
| 33 | + const editorService = accessor.get(IEditorService); |
| 34 | + const sessions = debugService.getModel().getSessions(false); |
| 35 | + const modelService = accessor.get(IModelService); |
| 36 | + const languageService = accessor.get(ILanguageService); |
| 37 | + const labelService = accessor.get(ILabelService); |
| 38 | + |
| 39 | + const localDisposableStore = new DisposableStore(); |
| 40 | + const quickPick = quickInputService.createQuickPick<IPickerLoadedScriptItem>(); |
| 41 | + localDisposableStore.add(quickPick); |
| 42 | + quickPick.matchOnLabel = quickPick.matchOnDescription = quickPick.matchOnDetail = quickPick.sortByLabel = false; |
| 43 | + quickPick.placeholder = nls.localize('moveFocusedView.selectView', "Search loaded scripts by name"); |
| 44 | + quickPick.items = await _getPicks(quickPick.value, sessions, editorService, modelService, languageService, labelService); |
| 45 | + |
| 46 | + localDisposableStore.add(quickPick.onDidChangeValue(async () => { |
| 47 | + quickPick.items = await _getPicks(quickPick.value, sessions, editorService, modelService, languageService, labelService); |
| 48 | + })); |
| 49 | + localDisposableStore.add(quickPick.onDidAccept(() => { |
| 50 | + const selectedItem = quickPick.selectedItems[0]; |
| 51 | + selectedItem.accept(); |
| 52 | + quickPick.hide(); |
| 53 | + localDisposableStore.dispose(); |
| 54 | + })); |
| 55 | + quickPick.show(); |
| 56 | +} |
| 57 | + |
| 58 | +async function _getPicksFromSession(session: IDebugSession, filter: string, editorService: IEditorService, modelService: IModelService, languageService: ILanguageService, labelService: ILabelService): Promise<Array<IPickerLoadedScriptItem | IQuickPickSeparator>> { |
| 59 | + const items: Array<IPickerLoadedScriptItem | IQuickPickSeparator> = []; |
| 60 | + items.push({ type: 'separator', label: session.name }); |
| 61 | + const sources = await session.getLoadedSources(); |
| 62 | + |
| 63 | + sources.forEach((element: Source) => { |
| 64 | + const pick = _createPick(element, filter, editorService, modelService, languageService, labelService); |
| 65 | + if (pick) { |
| 66 | + items.push(pick); |
| 67 | + } |
| 68 | + |
| 69 | + }); |
| 70 | + return items; |
| 71 | +} |
| 72 | +async function _getPicks(filter: string, sessions: IDebugSession[], editorService: IEditorService, modelService: IModelService, languageService: ILanguageService, labelService: ILabelService): Promise<Array<IPickerLoadedScriptItem | IQuickPickSeparator>> { |
| 73 | + const loadedScriptPicks: Array<IPickerLoadedScriptItem | IQuickPickSeparator> = []; |
| 74 | + |
| 75 | + |
| 76 | + const picks = await Promise.all( |
| 77 | + sessions.map((session) => _getPicksFromSession(session, filter, editorService, modelService, languageService, labelService)) |
| 78 | + ); |
| 79 | + |
| 80 | + for (const row of picks) { |
| 81 | + for (const elem of row) { |
| 82 | + loadedScriptPicks.push(elem); |
| 83 | + } |
| 84 | + } |
| 85 | + return loadedScriptPicks; |
| 86 | +} |
| 87 | + |
| 88 | +function _createPick(source: Source, filter: string, editorService: IEditorService, modelService: IModelService, languageService: ILanguageService, labelService: ILabelService): IPickerLoadedScriptItem | undefined { |
| 89 | + |
| 90 | + const label = labelService.getUriBasenameLabel(source.uri); |
| 91 | + const desc = labelService.getUriLabel(dirname(source.uri)); |
| 92 | + |
| 93 | + // manually filter so that headers don't get filtered out |
| 94 | + const labelHighlights = matchesFuzzy(filter, label, true); |
| 95 | + const descHighlights = matchesFuzzy(filter, desc, true); |
| 96 | + if (labelHighlights || descHighlights) { |
| 97 | + return { |
| 98 | + label, |
| 99 | + description: desc === '.' ? undefined : desc, |
| 100 | + highlights: { label: labelHighlights ?? undefined, description: descHighlights ?? undefined }, |
| 101 | + iconClasses: getIconClasses(modelService, languageService, source.uri), |
| 102 | + accept: () => { |
| 103 | + if (source.available) { |
| 104 | + source.openInEditor(editorService, { startLineNumber: 0, startColumn: 0, endLineNumber: 0, endColumn: 0 }); |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | + return undefined; |
| 110 | +} |
0 commit comments