|
| 1 | +/*----------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE file in the project root for license information. |
| 4 | + *-----------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { |
| 7 | + TreeDataProvider, |
| 8 | + TreeItem, |
| 9 | + Event, |
| 10 | + ProviderResult, |
| 11 | + EventEmitter, |
| 12 | + Disposable, |
| 13 | + TreeView, |
| 14 | + window, |
| 15 | + TreeItemCollapsibleState, |
| 16 | + debug, |
| 17 | + DebugSession, |
| 18 | +} from 'vscode'; |
| 19 | + |
| 20 | +import { Odo, OdoImpl } from './odo'; |
| 21 | + |
| 22 | +class DebugSessionEntry { |
| 23 | + label: string; |
| 24 | + session: DebugSession; |
| 25 | +} |
| 26 | + |
| 27 | +export class DebugSessionsView implements TreeDataProvider<string>, Disposable { |
| 28 | + private static instance: DebugSessionsView; |
| 29 | + private static sessions: Map<string, DebugSessionEntry> = new Map(); |
| 30 | + |
| 31 | + private static odoctl: Odo = OdoImpl.Instance; |
| 32 | + |
| 33 | + private treeView: TreeView<string>; |
| 34 | + |
| 35 | + private onDidChangeTreeDataEmitter: EventEmitter<string> = |
| 36 | + new EventEmitter<string | undefined>(); |
| 37 | + |
| 38 | + readonly onDidChangeTreeData: Event<string | undefined> = this |
| 39 | + .onDidChangeTreeDataEmitter.event; |
| 40 | + |
| 41 | + private constructor() { |
| 42 | + this.treeView = window.createTreeView('openshiftDebugView', { |
| 43 | + treeDataProvider: this, |
| 44 | + }); |
| 45 | + debug.onDidStartDebugSession((session) => { |
| 46 | + if (session.configuration.contextPath) { |
| 47 | + const osObj = DebugSessionsView.odoctl.getOpenShiftObjectByContext(session.configuration.contextPath.fsPath); |
| 48 | + DebugSessionsView.sessions.set(session.configuration.contextPath.fsPath, { |
| 49 | + label: `${osObj.getParent().getParent().getName()}/${osObj.getParent().getName()}/${osObj.getName()}`, |
| 50 | + session |
| 51 | + }); |
| 52 | + this.refresh(); |
| 53 | + } |
| 54 | + }), |
| 55 | + debug.onDidTerminateDebugSession((session) => { |
| 56 | + if (session.configuration?.contextPath) { |
| 57 | + DebugSessionsView.sessions.delete(session.configuration.contextPath.fsPath); |
| 58 | + this.refresh(); |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + static getInstance(): DebugSessionsView { |
| 64 | + if (!DebugSessionsView.instance) { |
| 65 | + DebugSessionsView.instance = new DebugSessionsView(); |
| 66 | + } |
| 67 | + return DebugSessionsView.instance; |
| 68 | + } |
| 69 | + |
| 70 | + // eslint-disable-next-line class-methods-use-this |
| 71 | + getTreeItem(element: string): TreeItem | Thenable<TreeItem> { |
| 72 | + return { |
| 73 | + label: DebugSessionsView.sessions.get(element).label, |
| 74 | + collapsibleState: TreeItemCollapsibleState.None, |
| 75 | + contextValue: 'openshift.debug.session', |
| 76 | + iconPath: DebugSessionsView.odoctl.getOpenShiftObjectByContext(element).iconPath, |
| 77 | + command: { |
| 78 | + command: 'workbench.view.debug', |
| 79 | + title: 'Show Debug and Run' |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + // eslint-disable-next-line class-methods-use-this |
| 85 | + getChildren(): ProviderResult<string[]> { |
| 86 | + return [...DebugSessionsView.sessions.keys()]; |
| 87 | + } |
| 88 | + |
| 89 | + // eslint-disable-next-line class-methods-use-this |
| 90 | + getParent?(): string { |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + |
| 94 | + refresh(): void { |
| 95 | + this.onDidChangeTreeDataEmitter.fire(); |
| 96 | + } |
| 97 | + |
| 98 | + dispose(): void { |
| 99 | + this.treeView.dispose(); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments