Skip to content

Commit 73662f4

Browse files
committed
Get current component based on window's active editor
This PR fixes #2225. Signed-off-by: Denis Golovin dgolovin@redhat.com
1 parent 1fd7ee1 commit 73662f4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/odo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ export interface Odo {
361361
deleteURL(url: OpenShiftObject): Promise<OpenShiftObject>;
362362
createComponentCustomUrl(component: OpenShiftObject, name: string, port: string, secure?: boolean): Promise<OpenShiftObject>;
363363
getOpenShiftObjectByContext(context: string): OpenShiftObject;
364+
getSettingsByContext(context: string): odo.Component;
364365
loadItems<I>(result: cliInstance.CliExitData, fetch: (data) => I[]): I[];
365366
getRegistries(): Promise<Registry[]>;
366367
readonly subject: Subject<OdoEvent>;
@@ -1029,6 +1030,10 @@ export class OdoImpl implements Odo {
10291030
return OdoImpl.data.getObjectByContext(Uri.file(context));
10301031
}
10311032

1033+
getSettingsByContext(context: string): odo.Component {
1034+
return OdoImpl.data.getSettingsByContext(context);
1035+
}
1036+
10321037
async loadWorkspaceComponents(event: WorkspaceFoldersChangeEvent): Promise<void> {
10331038
const clusters = (await this.getClusters());
10341039
if(!clusters) return;

src/openshift/openshiftItem.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*-----------------------------------------------------------------------------------------------*/
55
/* eslint-disable @typescript-eslint/ban-types */
66

7-
import { window, QuickPickItem, commands } from 'vscode';
7+
import { window, QuickPickItem, commands, workspace } from 'vscode';
88
import * as validator from 'validator';
99
import { Odo, getInstance, OpenShiftObject, ContextType, OpenShiftApplication, OpenShiftProject } from '../odo';
1010
import { OpenShiftExplorer } from '../explorer';
@@ -159,13 +159,35 @@ export default class OpenShiftItem {
159159
let context: OpenShiftObject | QuickPickCommand = treeItem;
160160
let project: OpenShiftObject;
161161
if (!context) {
162+
162163
const clusters = await this.odo.getClusters();
163164
if (clusters.length) { // connected to cluster because odo version printed out server url
164165
const projects = await this.odo.getProjects();
165166
context = projects.find((prj:OpenShiftProject)=>prj.active);
166167
if (!context) {
167168
throw new VsCommandError(errorMessage.Project)
168169
}
170+
// first try to get target component out of active editor
171+
const currentEditorFile = window?.activeTextEditor?.document?.uri;
172+
if (currentEditorFile) {
173+
const contextFolder = workspace.getWorkspaceFolder(currentEditorFile);
174+
if (contextFolder) {
175+
const oso = this.odo.getOpenShiftObjectByContext(contextFolder.uri.fsPath);
176+
if (!oso) {
177+
const applications = await this.odo.getApplications(context);
178+
const settings = this.odo.getSettingsByContext(contextFolder.uri.fsPath);
179+
if (settings) {
180+
const app = applications.find((a) => a.getName() === settings.spec.app);
181+
if(app) {
182+
await this.odo.getComponents(app);
183+
context = this.odo.getOpenShiftObjectByContext(contextFolder.uri.fsPath);
184+
}
185+
}
186+
} else if (context?.getName() === oso?.getParent()?.getParent()?.getName()) {
187+
context = oso;
188+
}
189+
}
190+
}
169191
} else { // cluster is not accessible or user not logged in
170192
const projectName = await OpenShiftItem.getName('Project Name', Promise.resolve([]))
171193
if (projectName) {

0 commit comments

Comments
 (0)