|
4 | 4 | *-----------------------------------------------------------------------------------------------*/ |
5 | 5 | /* eslint-disable @typescript-eslint/ban-types */ |
6 | 6 |
|
7 | | -import { window, QuickPickItem, commands } from 'vscode'; |
| 7 | +import { window, QuickPickItem, commands, workspace } from 'vscode'; |
8 | 8 | import * as validator from 'validator'; |
9 | 9 | import { Odo, getInstance, OpenShiftObject, ContextType, OpenShiftApplication, OpenShiftProject } from '../odo'; |
10 | 10 | import { OpenShiftExplorer } from '../explorer'; |
@@ -159,13 +159,35 @@ export default class OpenShiftItem { |
159 | 159 | let context: OpenShiftObject | QuickPickCommand = treeItem; |
160 | 160 | let project: OpenShiftObject; |
161 | 161 | if (!context) { |
| 162 | + |
162 | 163 | const clusters = await this.odo.getClusters(); |
163 | 164 | if (clusters.length) { // connected to cluster because odo version printed out server url |
164 | 165 | const projects = await this.odo.getProjects(); |
165 | 166 | context = projects.find((prj:OpenShiftProject)=>prj.active); |
166 | 167 | if (!context) { |
167 | 168 | throw new VsCommandError(errorMessage.Project) |
168 | 169 | } |
| 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 | + } |
169 | 191 | } else { // cluster is not accessible or user not logged in |
170 | 192 | const projectName = await OpenShiftItem.getName('Project Name', Promise.resolve([])) |
171 | 193 | if (projectName) { |
|
0 commit comments