Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve "Open Kuberbetes YAML file" to avoid accidental opens and dup… #3961

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,24 @@
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)

Check warning on line 420 in src/explorer.ts

View check run for this annotation

Codecov / codecov/patch

src/explorer.ts#L419-L420

Added lines #L419 - L420 were not covered by tests
.find((docUri) => {
return (docUri.scheme === uri.scheme &&
docUri.authority === uri.authority &&
docUri.fragment === uri.fragment &&
docUri.path === uri.path &&
this.getComparableQuery(docUri) === query);

Check warning on line 426 in src/explorer.ts

View check run for this annotation

Codecov / codecov/patch

src/explorer.ts#L423-L426

Added lines #L423 - L426 were not covered by tests
});

// 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);

Check warning on line 433 in src/explorer.ts

View check run for this annotation

Codecov / codecov/patch

src/explorer.ts#L433

Added line #L433 was not covered by tests
}
},
(err) => window.showErrorMessage(`Error loading document: ${err}`));
}

Expand Down Expand Up @@ -450,6 +463,19 @@
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, '');

Check warning on line 476 in src/explorer.ts

View check run for this annotation

Codecov / codecov/patch

src/explorer.ts#L475-L476

Added lines #L475 - L476 were not covered by tests
}

@vsCommand('openshift.resource.delete')
public static async deleteResource(component: KubernetesObject) {
await Oc.Instance.deleteKubernetesObject(component.kind, component.metadata.name);
Expand Down
Loading