diff --git a/package.json b/package.json index fe60fec1a..78dbe26d0 100644 --- a/package.json +++ b/package.json @@ -728,6 +728,10 @@ { "id": "openshiftWatchView", "name": "Watch Sessions" + }, + { + "id": "openshiftDebugView", + "name": "Debug Sessions" } ] }, diff --git a/src/debug.ts b/src/debug.ts new file mode 100644 index 000000000..e5caee460 --- /dev/null +++ b/src/debug.ts @@ -0,0 +1,102 @@ +/*----------------------------------------------------------------------------------------------- + * Copyright (c) Red Hat, Inc. All rights reserved. + * Licensed under the MIT License. See LICENSE file in the project root for license information. + *-----------------------------------------------------------------------------------------------*/ + +import { + TreeDataProvider, + TreeItem, + Event, + ProviderResult, + EventEmitter, + Disposable, + TreeView, + window, + TreeItemCollapsibleState, + debug, + DebugSession, +} from 'vscode'; + +import { Odo, OdoImpl } from './odo'; + +class DebugSessionEntry { + label: string; + session: DebugSession; +} + +export class DebugSessionsView implements TreeDataProvider, Disposable { + private static instance: DebugSessionsView; + private static sessions: Map = new Map(); + + private static odoctl: Odo = OdoImpl.Instance; + + private treeView: TreeView; + + private onDidChangeTreeDataEmitter: EventEmitter = + new EventEmitter(); + + readonly onDidChangeTreeData: Event = this + .onDidChangeTreeDataEmitter.event; + + private constructor() { + this.treeView = window.createTreeView('openshiftDebugView', { + treeDataProvider: this, + }); + debug.onDidStartDebugSession((session) => { + if (session.configuration.contextPath) { + const osObj = DebugSessionsView.odoctl.getOpenShiftObjectByContext(session.configuration.contextPath.fsPath); + DebugSessionsView.sessions.set(session.configuration.contextPath.fsPath, { + label: `${osObj.getParent().getParent().getName()}/${osObj.getParent().getName()}/${osObj.getName()}`, + session + }); + this.refresh(); + } + }), + debug.onDidTerminateDebugSession((session) => { + if (session.configuration?.contextPath) { + DebugSessionsView.sessions.delete(session.configuration.contextPath.fsPath); + this.refresh(); + } + }) + } + + static getInstance(): DebugSessionsView { + if (!DebugSessionsView.instance) { + DebugSessionsView.instance = new DebugSessionsView(); + } + return DebugSessionsView.instance; + } + + // eslint-disable-next-line class-methods-use-this + getTreeItem(element: string): TreeItem | Thenable { + return { + label: DebugSessionsView.sessions.get(element).label, + collapsibleState: TreeItemCollapsibleState.None, + contextValue: 'openshift.debug.session', + iconPath: DebugSessionsView.odoctl.getOpenShiftObjectByContext(element).iconPath, + command: { + command: 'workbench.view.debug', + title: 'Show Debug and Run' + } + }; + } + + // eslint-disable-next-line class-methods-use-this + getChildren(): ProviderResult { + return [...DebugSessionsView.sessions.keys()]; + } + + // eslint-disable-next-line class-methods-use-this + getParent?(): string { + return undefined; + } + + refresh(): void { + this.onDidChangeTreeDataEmitter.fire(); + } + + dispose(): void { + this.treeView.dispose(); + } + +} diff --git a/src/extension.ts b/src/extension.ts index f72835b38..8b16e9ae9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -21,11 +21,13 @@ import { registerCommands } from './vscommand'; import { ToolsConfig } from './tools'; import { extendClusterExplorer } from './k8s/clusterExplorer'; import { WatchSessionsView } from './watch'; +import { DebugSessionsView } from './debug'; import path = require('path'); import fsx = require('fs-extra'); import treeKill = require('tree-kill'); + // eslint-disable-next-line @typescript-eslint/no-empty-function // this method is called when your extension is deactivated export function deactivate(): void { @@ -70,6 +72,7 @@ export async function activate(extensionContext: ExtensionContext): Promise ), OpenShiftExplorer.getInstance(), WatchSessionsView.getInstance(), + DebugSessionsView.getInstance(), ...Component.init(extensionContext) ]; disposable.forEach((value) => extensionContext.subscriptions.push(value));