diff --git a/src/explorer.ts b/src/explorer.ts index c0d7386a9..879407207 100644 --- a/src/explorer.ts +++ b/src/explorer.ts @@ -415,11 +415,24 @@ export class OpenShiftExplorer implements TreeDataProvider, Dispos loadKubernetesCore(namespace: string | null, value: string) { const outputFormat = this.getOutputFormat(); const uri = this.kubefsUri(namespace, value, outputFormat); - workspace.openTextDocument(uri).then((doc) => { - if (doc) { - void window.showTextDocument(doc); - } - }, + + const query = this.getComparableQuery(uri); + const openUri = workspace.textDocuments.map((doc) => doc.uri) + .find((docUri) => { + return (docUri.scheme === uri.scheme && + docUri.authority === uri.authority && + docUri.fragment === uri.fragment && + docUri.path === uri.path && + this.getComparableQuery(docUri) === query); + }); + + // If open document is found for the URI provided, we use its URI to bring its editor to the front + // instead of openning a new editor + workspace.openTextDocument(openUri ? openUri : uri).then((doc) => { + if (doc) { + void window.showTextDocument(doc); + } + }, (err) => window.showErrorMessage(`Error loading document: ${err}`)); } @@ -450,6 +463,19 @@ export class OpenShiftExplorer implements TreeDataProvider, Dispos return Uri.parse(uri); } + /* + * Returns the query string of the specified Uri without "nonce" param, + * so the query strings can be compared. + * The "nonce" param is generated as current time value for every KubefsUri created, + * f.i., "_=1709642987392", and are always added to the end of the query string (so + * they always have the preceeding query parameters sepacator character ("&") added), + * so the query strings, if they aren't cleared from "nonce" param, can be compared for + * Uri objects even when they point to the same document. + */ + getComparableQuery(uri: Uri): string { + return uri.query.replace(/&_=[0-9]+/g, ''); + } + @vsCommand('openshift.resource.delete') public static async deleteResource(component: KubernetesObject) { await Oc.Instance.deleteKubernetesObject(component.kind, component.metadata.name);