From 8d67832620f453539a8a4c7a6d9707314cdd9458 Mon Sep 17 00:00:00 2001 From: Denis Golovin Date: Fri, 27 Aug 2021 13:13:50 -0700 Subject: [PATCH] Get current component based on window's active editor This PR fixes #2225. Signed-off-by: Denis Golovin dgolovin@redhat.com --- src/odo.ts | 5 +++++ src/openshift/openshiftItem.ts | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/odo.ts b/src/odo.ts index f83b2a2ec..911722c16 100644 --- a/src/odo.ts +++ b/src/odo.ts @@ -361,6 +361,7 @@ export interface Odo { deleteURL(url: OpenShiftObject): Promise; createComponentCustomUrl(component: OpenShiftObject, name: string, port: string, secure?: boolean): Promise; getOpenShiftObjectByContext(context: string): OpenShiftObject; + getSettingsByContext(context: string): odo.Component; loadItems(result: cliInstance.CliExitData, fetch: (data) => I[]): I[]; getRegistries(): Promise; readonly subject: Subject; @@ -1029,6 +1030,10 @@ export class OdoImpl implements Odo { return OdoImpl.data.getObjectByContext(Uri.file(context)); } + getSettingsByContext(context: string): odo.Component { + return OdoImpl.data.getSettingsByContext(context); + } + async loadWorkspaceComponents(event: WorkspaceFoldersChangeEvent): Promise { const clusters = (await this.getClusters()); if(!clusters) return; diff --git a/src/openshift/openshiftItem.ts b/src/openshift/openshiftItem.ts index f3f5939b6..c74233a92 100644 --- a/src/openshift/openshiftItem.ts +++ b/src/openshift/openshiftItem.ts @@ -4,7 +4,7 @@ *-----------------------------------------------------------------------------------------------*/ /* eslint-disable @typescript-eslint/ban-types */ -import { window, QuickPickItem, commands } from 'vscode'; +import { window, QuickPickItem, commands, workspace } from 'vscode'; import * as validator from 'validator'; import { Odo, getInstance, OpenShiftObject, ContextType, OpenShiftApplication, OpenShiftProject } from '../odo'; import { OpenShiftExplorer } from '../explorer'; @@ -159,6 +159,7 @@ export default class OpenShiftItem { let context: OpenShiftObject | QuickPickCommand = treeItem; let project: OpenShiftObject; if (!context) { + const clusters = await this.odo.getClusters(); if (clusters.length) { // connected to cluster because odo version printed out server url const projects = await this.odo.getProjects(); @@ -166,6 +167,27 @@ export default class OpenShiftItem { if (!context) { throw new VsCommandError(errorMessage.Project) } + // first try to get target component out of active editor + const currentEditorFile = window?.activeTextEditor?.document?.uri; + if (currentEditorFile) { + const contextFolder = workspace.getWorkspaceFolder(currentEditorFile); + if (contextFolder) { + const oso = this.odo.getOpenShiftObjectByContext(contextFolder.uri.fsPath); + if (!oso) { + const applications = await this.odo.getApplications(context); + const settings = this.odo.getSettingsByContext(contextFolder.uri.fsPath); + if (settings) { + const app = applications.find((a) => a.getName() === settings.spec.app); + if(app) { + await this.odo.getComponents(app); + context = this.odo.getOpenShiftObjectByContext(contextFolder.uri.fsPath); + } + } + } else if (context?.getName() === oso?.getParent()?.getParent()?.getName()) { + context = oso; + } + } + } } else { // cluster is not accessible or user not logged in const projectName = await OpenShiftItem.getName('Project Name', Promise.resolve([])) if (projectName) {