From ab214b4ce0d679df2af384fcead26048befc5b2f Mon Sep 17 00:00:00 2001 From: Kartik Gupta <88345179+kartikgupta-db@users.noreply.github.com> Date: Wed, 28 Feb 2024 15:38:47 +0100 Subject: [PATCH] Allow right click -> copy for all tree items (#1101) ## Changes ## Tests --- packages/databricks-vscode/package.json | 12 ++++++- .../src/configuration/ui/ClusterComponent.ts | 2 +- .../src/configuration/ui/types.ts | 1 + packages/databricks-vscode/src/extension.ts | 5 +++ .../src/utils/UtilsCommands.ts | 36 ++++++++++++++++++- 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/databricks-vscode/package.json b/packages/databricks-vscode/package.json index 06cac40ad..12c5eaa28 100644 --- a/packages/databricks-vscode/package.json +++ b/packages/databricks-vscode/package.json @@ -269,6 +269,12 @@ "title": "Show bundle logs", "enablement": "databricks.context.activated", "category": "Databricks" + }, + { + "command": "databricks.utils.copy", + "title": "Copy", + "enablement": "databricks.context.activated", + "category": "Databricks" } ], "viewsContainers": { @@ -434,6 +440,10 @@ "command": "databricks.bundle.cancelRun", "when": "view == dabsResourceExplorerView && viewItem =~ /^databricks.bundle.*.cancellable.*$/ && databricks.context.bundle.deploymentState == idle", "group": "inline@0" + }, + { + "command": "databricks.utils.copy", + "when": "view == dabsResourceExplorerView || view == configurationView" } ], "editor/title": [ @@ -509,7 +519,7 @@ "label": "Run on Databricks", "icon": { "dark": "resources/dark/databricks-run-icon.svg", - "light": "resources/light/logo.svg" + "light": "resources/light/databricks-run-icon.svg" } } ], diff --git a/packages/databricks-vscode/src/configuration/ui/ClusterComponent.ts b/packages/databricks-vscode/src/configuration/ui/ClusterComponent.ts index 28191734d..4e4708276 100644 --- a/packages/databricks-vscode/src/configuration/ui/ClusterComponent.ts +++ b/packages/databricks-vscode/src/configuration/ui/ClusterComponent.ts @@ -162,7 +162,7 @@ export class ClusterComponent extends BaseComponent { return []; } const cluster = this.connectionManager.cluster; - const children: TreeItem[] = [ + const children: ConfigurationTreeItem[] = [ { label: "Cluster ID", description: cluster.id, diff --git a/packages/databricks-vscode/src/configuration/ui/types.ts b/packages/databricks-vscode/src/configuration/ui/types.ts index 024dffe9c..0c3fde402 100644 --- a/packages/databricks-vscode/src/configuration/ui/types.ts +++ b/packages/databricks-vscode/src/configuration/ui/types.ts @@ -2,4 +2,5 @@ import {TreeItem} from "vscode"; export interface ConfigurationTreeItem extends TreeItem { url?: string; + copyText?: string; } diff --git a/packages/databricks-vscode/src/extension.ts b/packages/databricks-vscode/src/extension.ts index 154c70627..c099a7809 100644 --- a/packages/databricks-vscode/src/extension.ts +++ b/packages/databricks-vscode/src/extension.ts @@ -653,6 +653,11 @@ export async function activate( utilCommands.openExternalCommand(), utilCommands ), + telemetry.registerCommand( + "databricks.utils.copy", + utilCommands.copyToClipboardCommand(), + utilCommands + ), telemetry.registerCommand("databricks.call", (fn) => { if (fn) { fn(); diff --git a/packages/databricks-vscode/src/utils/UtilsCommands.ts b/packages/databricks-vscode/src/utils/UtilsCommands.ts index a9e14982d..51739a05c 100644 --- a/packages/databricks-vscode/src/utils/UtilsCommands.ts +++ b/packages/databricks-vscode/src/utils/UtilsCommands.ts @@ -1,4 +1,4 @@ -import {Disposable, window} from "vscode"; +import {Disposable, window, env} from "vscode"; import {openExternal} from "./urlUtils"; export class UtilsCommands implements Disposable { @@ -24,6 +24,40 @@ export class UtilsCommands implements Disposable { }; } + copyToClipboardCommand() { + return async (value: any | undefined) => { + let text: string | undefined; + + if (value?.copyText instanceof Promise) { + text = await value.copyText; + } else if (value.copyText !== undefined) { + text = value.copyText; + } + + if (text === undefined && value?.getTreeItem !== undefined) { + const treeItem = value.getTreeItem(); + if (treeItem instanceof Promise) { + value = await treeItem; + } else { + value = treeItem; + } + } + + if (text === undefined) { + text = value?.copyText ?? value?.description ?? value?.label; + } + + if (text === undefined) { + window.showErrorMessage( + "Databricks: Can't copy to clipboard. No text found." + ); + return; + } + window.showInformationMessage("Copied to clipboard"); + await env.clipboard.writeText(text); + }; + } + dispose() { this.disposables.forEach((d) => d.dispose()); }