From dae36be198a6ceb28a764df224787b9ecda0bbab Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Tue, 30 Jul 2024 10:10:59 +0100 Subject: [PATCH 01/12] start the labels --- package.json | 12 +++++++++++ src/extension.ts | 6 ++++++ src/models/labels.ts | 26 ++++++++++++++++++++++++ src/utils/connLabel.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/models/labels.ts create mode 100644 src/utils/connLabel.ts diff --git a/package.json b/package.json index 61cd91da..3dea07fb 100644 --- a/package.json +++ b/package.json @@ -179,6 +179,18 @@ "description": "Connection map for workspace files", "default": {}, "scope": "resource" + }, + "kdb.connectionLabels": { + "type": "object", + "description": "List of label names and colorset", + "default": {}, + "scope": "resource" + }, + "kdb.connectionLabelsMap": { + "type": "object", + "description": "Label connection map", + "default": {}, + "scope": "resource" } } }, diff --git a/src/extension.ts b/src/extension.ts index 837505f3..bf1d2d86 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -475,6 +475,12 @@ export async function activate(context: ExtensionContext) { ext.dataSourceTreeProvider.reload(); ext.scratchpadTreeProvider.reload(); } + if (event.affectsConfiguration("kdb.connectionLabelsMap")) { + ext.serverProvider.reload(); + } + if (event.affectsConfiguration("kdb.connectionLabels")) { + ext.serverProvider.reload(); + } }), ); diff --git a/src/models/labels.ts b/src/models/labels.ts new file mode 100644 index 00000000..2410b81f --- /dev/null +++ b/src/models/labels.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 1998-2023 Kx Systems Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +export type LabelColors = { + name: string; + colorHex: string; +}; + +export type Labels = { + name: string; + color: LabelColors; +}; + +export type ConnectionLabels = { + [labelName: string]: string[]; // Array of connection names +}; diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts new file mode 100644 index 00000000..f1440ed1 --- /dev/null +++ b/src/utils/connLabel.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 1998-2023 Kx Systems Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +import { LabelColors } from "../models/labels"; + +export const labelColors: LabelColors[] = [ + { + name: "White", + colorHex: "#FFFFFF", + }, + { + name: "Red", + colorHex: "#CD3131", + }, + { + name: "Green", + colorHex: "#10BC7A", + }, + { + name: "Yellow", + colorHex: "#E5E50E", + }, + { + name: "Blue", + colorHex: "#2371C8", + }, + { + name: "Magenta", + colorHex: "#BC3FBC", + }, + { + name: "Cyan", + colorHex: "#15A7CD", + }, +]; From 1a5eedf99d4fd1ea7f085878592221a0b5e71a5c Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Wed, 31 Jul 2024 09:51:35 +0100 Subject: [PATCH 02/12] add more labels stuffs --- src/models/insights.ts | 1 + src/models/server.ts | 1 + src/utils/connLabel.ts | 15 +++++++++ .../components/kdbNewConnectionView.ts | 33 ++++++++++++------- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/models/insights.ts b/src/models/insights.ts index b9446e4d..2f97284b 100644 --- a/src/models/insights.ts +++ b/src/models/insights.ts @@ -15,6 +15,7 @@ export interface InsightDetails { alias: string; server: string; auth: boolean; + labels?: string[]; realm?: string; insecure?: boolean; } diff --git a/src/models/server.ts b/src/models/server.ts index af400b55..e54ad532 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -24,6 +24,7 @@ export interface ServerDetails { serverAlias: string; managed: boolean; tls: boolean; + labels?: string[]; username?: string; password?: string; } diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index f1440ed1..7d030f5f 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -11,6 +11,7 @@ * specific language governing permissions and limitations under the License. */ +import { html } from "lit"; import { LabelColors } from "../models/labels"; export const labelColors: LabelColors[] = [ @@ -43,3 +44,17 @@ export const labelColors: LabelColors[] = [ colorHex: "#15A7CD", }, ]; + +export function getDropdownOptions() { + let colorsOptions = ` + No Label Selected + `; + + labelColors.forEach((color) => { + colorsOptions += ` + ${color.name} + `; + }); + + return colorsOptions; +} diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index 93d79117..494c728d 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -31,6 +31,7 @@ export class KdbNewConnectionView extends LitElement { tls: false, username: "", password: "", + labels: [], }; bundledServer: ServerDetails = { serverName: "127.0.0.1", @@ -39,6 +40,7 @@ export class KdbNewConnectionView extends LitElement { serverAlias: "local", managed: false, tls: false, + labels: [], }; insightsServer: InsightDetails = { alias: "", @@ -46,6 +48,7 @@ export class KdbNewConnectionView extends LitElement { auth: true, realm: "", insecure: false, + labels: [], }; serverType: ServerType = ServerType.KDB; isBundledQ: boolean = true; @@ -279,6 +282,21 @@ export class KdbNewConnectionView extends LitElement { this.serverType = config.serverType; } + renderConnectionLabelsSection(type: string) { + return html`
+
+
Connection label (optional)
+
+ +
+
+
`; + } + renderNewConnectionForm() { return html`
@@ -345,6 +363,7 @@ export class KdbNewConnectionView extends LitElement {
${this.renderPortNumber()}
+ ${this.renderConnectionLabelsSection("bundleQ")}
@@ -415,6 +434,7 @@ export class KdbNewConnectionView extends LitElement { + ${this.renderConnectionLabelsSection("myQ")} @@ -448,6 +468,7 @@ export class KdbNewConnectionView extends LitElement { + ${this.renderConnectionLabelsSection("insights")} @@ -633,7 +654,6 @@ export class KdbNewConnectionView extends LitElement { this.insightsServer.alias = this.connectionData.serverName; this.insightsServer.server = this.connectionData.serverAddress; this.insightsServer.realm = this.connectionData.realm ?? ""; - this.insightsServer.insecure = this.connectionData.insecure ?? false; return html`
@@ -651,17 +671,6 @@ export class KdbNewConnectionView extends LitElement {
Advanced ${this.renderRealm()} -
- Accept insecure SSL certifcates -
From c030c06a1d1af117718c25b068c837852e31672d Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Thu, 1 Aug 2024 14:16:00 +0100 Subject: [PATCH 03/12] small changes --- package.json | 8 ++++---- src/extensionVariables.ts | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3dea07fb..12ab17d6 100644 --- a/package.json +++ b/package.json @@ -181,15 +181,15 @@ "scope": "resource" }, "kdb.connectionLabels": { - "type": "object", + "type": "array", "description": "List of label names and colorset", - "default": {}, + "default": [], "scope": "resource" }, "kdb.connectionLabelsMap": { - "type": "object", + "type": "array", "description": "Label connection map", - "default": {}, + "default": [], "scope": "resource" } } diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 735c5ab2..5250c7a2 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -37,6 +37,7 @@ import { ScratchpadFile } from "./models/scratchpad"; import { LocalConnection } from "./classes/localConnection"; import { InsightsConnection } from "./classes/insightsConnection"; import { DataSourceFiles } from "./models/dataSource"; +import { Labels } from "./models/labels"; // eslint-disable-next-line @typescript-eslint/no-namespace export namespace ext { @@ -84,6 +85,7 @@ export namespace ext { export const kdbNodesWithoutAuth: string[] = []; export const kdbNodesWithoutTls: string[] = []; export const kdbConnectionAliasList: string[] = []; + export const connLabelList: Labels[] = []; export const maxRetryCount = 5; export let secretSettings: AuthSettings; From 1f7a71298c9ee3e18f00eb8c8b5cab4537e6cbe2 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Thu, 1 Aug 2024 14:16:13 +0100 Subject: [PATCH 04/12] remove labels from server obj --- src/models/insights.ts | 1 - src/models/server.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/models/insights.ts b/src/models/insights.ts index 2f97284b..b9446e4d 100644 --- a/src/models/insights.ts +++ b/src/models/insights.ts @@ -15,7 +15,6 @@ export interface InsightDetails { alias: string; server: string; auth: boolean; - labels?: string[]; realm?: string; insecure?: boolean; } diff --git a/src/models/server.ts b/src/models/server.ts index e54ad532..af400b55 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -24,7 +24,6 @@ export interface ServerDetails { serverAlias: string; managed: boolean; tls: boolean; - labels?: string[]; username?: string; password?: string; } From 0d06622f2344518e85e1883d84b6bb5bc5095899 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Thu, 1 Aug 2024 14:43:08 +0100 Subject: [PATCH 05/12] connlabel utils --- src/utils/connLabel.ts | 94 ++++++++++++++++--- .../components/kdbNewConnectionView.ts | 78 ++++++++++++++- 2 files changed, 158 insertions(+), 14 deletions(-) diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index 7d030f5f..d4c97363 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -12,7 +12,11 @@ */ import { html } from "lit"; -import { LabelColors } from "../models/labels"; +import { repeat } from "lit/directives/repeat.js"; +import { LabelColors, Labels } from "../models/labels"; +import { workspace } from "vscode"; +import { ext } from "../extensionVariables"; +import { kdbOutputLog } from "./core"; export const labelColors: LabelColors[] = [ { @@ -45,16 +49,84 @@ export const labelColors: LabelColors[] = [ }, ]; -export function getDropdownOptions() { - let colorsOptions = ` - No Label Selected - `; +export function getDropdownColorOptions() { + return html` + No Color Selected + ${repeat( + labelColors, + (color) => color, + (color) => + html` +
+ ${color.name}
+
`, + )} + `; +} + +export function getWorkspaceLabels() { + const existingConnLbls = workspace + .getConfiguration() + .get("kdb.connectionLabels"); + ext.connLabelList.length = 0; + if (existingConnLbls && existingConnLbls.length > 0) { + existingConnLbls.forEach((label: Labels) => { + ext.connLabelList.push(label); + }); + } +} - labelColors.forEach((color) => { - colorsOptions += ` - ${color.name} - `; - }); +export function createNewLabel(name: string, colorName: string) { + const color = labelColors.find((color) => color.name === colorName); + if (name === "") { + kdbOutputLog("Label name can't be empty", "ERROR"); + } + if (color && name !== "") { + const newLbl: Labels = { + name: name, + color: color, + }; + ext.connLabelList.push(newLbl); + workspace + .getConfiguration() + .update("kdb.connectionLabels", ext.connLabelList, true); + } else { + kdbOutputLog("No Color selected for the label", "ERROR"); + } +} + +export function getDropdownLblOptions() { + getWorkspaceLabels(); + return html` + No Label Selected + + ${repeat( + ext.connLabelList, + (lbl) => lbl, + (lbl) => + html` +
+ ${lbl.name}
+
`, + )} + `; +} - return colorsOptions; +export function generateLabels(): Labels { + return { + name: "", + color: { + name: "", + colorHex: "", + }, + }; } diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index 494c728d..f823dd75 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -22,6 +22,8 @@ import { EditConnectionMessage } from "../../models/messages"; @customElement("kdb-new-connection-view") export class KdbNewConnectionView extends LitElement { static styles = [vscodeStyles, kdbStyles, newConnectionStyles]; + newLblName = ""; + newLblColorName = ""; kdbServer: ServerDetails = { serverName: "", serverPort: "", @@ -31,7 +33,6 @@ export class KdbNewConnectionView extends LitElement { tls: false, username: "", password: "", - labels: [], }; bundledServer: ServerDetails = { serverName: "127.0.0.1", @@ -40,7 +41,6 @@ export class KdbNewConnectionView extends LitElement { serverAlias: "local", managed: false, tls: false, - labels: [], }; insightsServer: InsightDetails = { alias: "", @@ -48,12 +48,13 @@ export class KdbNewConnectionView extends LitElement { auth: true, realm: "", insecure: false, - labels: [], }; + labels = []; serverType: ServerType = ServerType.KDB; isBundledQ: boolean = true; oldAlias: string = ""; editAuth: boolean = false; + private isModalOpen = false; private _connectionData: EditConnectionMessage | undefined = undefined; private readonly vscode = acquireVsCodeApi(); private tabConfig = { @@ -73,6 +74,16 @@ export class KdbNewConnectionView extends LitElement { this.requestUpdate("connectionData", oldValue); } + openModal() { + this.isModalOpen = true; + this.requestUpdate(); + } + + closeModal() { + this.isModalOpen = false; + this.requestUpdate(); + } + connectedCallback() { super.connectedCallback(); window.addEventListener("message", this.handleMessage.bind(this)); @@ -282,6 +293,67 @@ export class KdbNewConnectionView extends LitElement { this.serverType = config.serverType; } + renderNewLabelModal() { + return html` + + + + `; + } + + renderNewLblBtn() { + return html` + + Create New Label + + ${this.isModalOpen ? this.renderNewLabelModal() : ""} + `; + } + renderConnectionLabelsSection(type: string) { return html`
From e5a4b5956f28ceda3609ac982831929a1eff1d38 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 08:17:19 +0100 Subject: [PATCH 06/12] create new label --- package.json | 1 + src/extension.ts | 10 ++ src/extensionVariables.ts | 33 ++++- src/panels/newConnection.ts | 18 +++ src/utils/connLabel.ts | 89 +------------- .../components/kdbNewConnectionView.ts | 114 ++++++++++++++++-- src/webview/components/styles.ts | 33 +++++ 7 files changed, 202 insertions(+), 96 deletions(-) diff --git a/package.json b/package.json index 12ab17d6..884bf410 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "onCommand:kdb.newConnection.editInsightsConnection", "onCommand:kdb.newConnection.editMyQConnection", "onCommand:kdb.newConnection.editBundledConnection", + "onCommand:kdb.labels.create", "onView:kdb-datasources-explorer", "onTerminalProfile:kdb.q-terminal", "onLanguage:python" diff --git a/src/extension.ts b/src/extension.ts index bf1d2d86..1649390e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -104,6 +104,7 @@ import { connectBuildTools, lintCommand } from "./commands/buildToolsCommand"; import { CompletionProvider } from "./services/completionProvider"; import { QuickFixProvider } from "./services/quickFixProvider"; import { connectClientCommands } from "./commands/clientCommands"; +import { createNewLabel, getWorkspaceLabels } from "./utils/connLabel"; let client: LanguageClient; @@ -112,6 +113,9 @@ export async function activate(context: ExtensionContext) { ext.outputChannel = window.createOutputChannel("kdb"); ext.openSslVersion = await checkOpenSslInstalled(); ext.isBundleQCreated = false; + + getWorkspaceLabels(); + // clear necessary contexts commands.executeCommand("setContext", "kdb.connected.active", false); commands.executeCommand("setContext", "kdb.insightsConnected", false); @@ -300,6 +304,12 @@ export async function activate(context: ExtensionContext) { await editKdbConnection(kdbData, oldAlias, true); }, ), + commands.registerCommand( + "kdb.labels.create", + async (name: string, colorName: string) => { + await createNewLabel(name, colorName); + }, + ), commands.registerCommand( "kdb.removeConnection", async (viewItem: KdbNode) => { diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 5250c7a2..119a66f8 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -37,7 +37,7 @@ import { ScratchpadFile } from "./models/scratchpad"; import { LocalConnection } from "./classes/localConnection"; import { InsightsConnection } from "./classes/insightsConnection"; import { DataSourceFiles } from "./models/dataSource"; -import { Labels } from "./models/labels"; +import { LabelColors, Labels } from "./models/labels"; // eslint-disable-next-line @typescript-eslint/no-namespace export namespace ext { @@ -308,4 +308,35 @@ export namespace ext { export const diagnosticCollection = languages.createDiagnosticCollection("kdb"); + + export const labelColors: LabelColors[] = [ + { + name: "White", + colorHex: "#FFFFFF", + }, + { + name: "Red", + colorHex: "#CD3131", + }, + { + name: "Green", + colorHex: "#10BC7A", + }, + { + name: "Yellow", + colorHex: "#E5E50E", + }, + { + name: "Blue", + colorHex: "#2371C8", + }, + { + name: "Magenta", + colorHex: "#BC3FBC", + }, + { + name: "Cyan", + colorHex: "#15A7CD", + }, + ]; } diff --git a/src/panels/newConnection.ts b/src/panels/newConnection.ts index 15528718..99591e9b 100644 --- a/src/panels/newConnection.ts +++ b/src/panels/newConnection.ts @@ -17,6 +17,7 @@ import { getNonce } from "../utils/getNonce"; import { ext } from "../extensionVariables"; import { InsightsNode, KdbNode } from "../services/kdbTreeProvider"; import { ConnectionType, EditConnectionMessage } from "../models/messages"; +import { getWorkspaceLabels } from "../utils/connLabel"; export class NewConnectionPannel { public static currentPanel: NewConnectionPannel | undefined; @@ -54,6 +55,10 @@ export class NewConnectionPannel { data: editConnData, }); } + panel.webview.postMessage({ + command: "refreshLabels", + data: ext.connLabelList, + }); } public static close() { @@ -119,6 +124,19 @@ export class NewConnectionPannel { message.oldAlias, ); } + if (message.command === "kdb.labels.create") { + vscode.commands.executeCommand( + "kdb.labels.create", + message.data.name, + message.data.colorName, + ); + setTimeout(() => { + this._panel.webview.postMessage({ + command: "refreshLabels", + data: ext.connLabelList, + }); + }, 500); + } }); } diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index d4c97363..e9239eaf 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -11,63 +11,11 @@ * specific language governing permissions and limitations under the License. */ -import { html } from "lit"; -import { repeat } from "lit/directives/repeat.js"; -import { LabelColors, Labels } from "../models/labels"; +import { Labels } from "../models/labels"; import { workspace } from "vscode"; import { ext } from "../extensionVariables"; import { kdbOutputLog } from "./core"; -export const labelColors: LabelColors[] = [ - { - name: "White", - colorHex: "#FFFFFF", - }, - { - name: "Red", - colorHex: "#CD3131", - }, - { - name: "Green", - colorHex: "#10BC7A", - }, - { - name: "Yellow", - colorHex: "#E5E50E", - }, - { - name: "Blue", - colorHex: "#2371C8", - }, - { - name: "Magenta", - colorHex: "#BC3FBC", - }, - { - name: "Cyan", - colorHex: "#15A7CD", - }, -]; - -export function getDropdownColorOptions() { - return html` - No Color Selected - ${repeat( - labelColors, - (color) => color, - (color) => - html` -
- ${color.name}
-
`, - )} - `; -} - export function getWorkspaceLabels() { const existingConnLbls = workspace .getConfiguration() @@ -81,7 +29,8 @@ export function getWorkspaceLabels() { } export function createNewLabel(name: string, colorName: string) { - const color = labelColors.find((color) => color.name === colorName); + getWorkspaceLabels(); + const color = ext.labelColors.find((color) => color.name === colorName); if (name === "") { kdbOutputLog("Label name can't be empty", "ERROR"); } @@ -98,35 +47,3 @@ export function createNewLabel(name: string, colorName: string) { kdbOutputLog("No Color selected for the label", "ERROR"); } } - -export function getDropdownLblOptions() { - getWorkspaceLabels(); - return html` - No Label Selected - - ${repeat( - ext.connLabelList, - (lbl) => lbl, - (lbl) => - html` -
- ${lbl.name}
-
`, - )} - `; -} - -export function generateLabels(): Labels { - return { - name: "", - color: { - name: "", - colorHex: "", - }, - }; -} diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index f823dd75..4cca8da1 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -18,10 +18,43 @@ import { InsightDetails } from "../../models/insights"; import { kdbStyles, newConnectionStyles, vscodeStyles } from "./styles"; import { EditConnectionMessage } from "../../models/messages"; +import { repeat } from "lit/directives/repeat.js"; +import { LabelColors, Labels } from "../../models/labels"; @customElement("kdb-new-connection-view") export class KdbNewConnectionView extends LitElement { static styles = [vscodeStyles, kdbStyles, newConnectionStyles]; + lblColorsList: LabelColors[] = [ + { + name: "White", + colorHex: "#FFFFFF", + }, + { + name: "Red", + colorHex: "#CD3131", + }, + { + name: "Green", + colorHex: "#10BC7A", + }, + { + name: "Yellow", + colorHex: "#E5E50E", + }, + { + name: "Blue", + colorHex: "#2371C8", + }, + { + name: "Magenta", + colorHex: "#BC3FBC", + }, + { + name: "Cyan", + colorHex: "#15A7CD", + }, + ]; + lblNamesList: Labels[] = []; newLblName = ""; newLblColorName = ""; kdbServer: ServerDetails = { @@ -80,6 +113,8 @@ export class KdbNewConnectionView extends LitElement { } closeModal() { + this.newLblColorName = ""; + this.newLblName = ""; this.isModalOpen = false; this.requestUpdate(); } @@ -112,6 +147,10 @@ export class KdbNewConnectionView extends LitElement { if (message.command === "editConnection") { this.connectionData = message.data; } + if (message.command === "refreshLabels") { + this.lblNamesList = message.data; + this.requestUpdate(); + } } changeTLS() { @@ -293,8 +332,48 @@ export class KdbNewConnectionView extends LitElement { this.serverType = config.serverType; } + renderLblDropdownColorOptions() { + return html` + No Color Selected + ${repeat( + this.lblColorsList, + (color) => color, + (color) => + html` +
+ ${color.name}
+
`, + )} + `; + } + + renderLblDropdownOptions() { + return html` + No Label Selected + ${repeat( + this.lblNamesList, + (lbl) => lbl, + (lbl) => + html` +
+ ${lbl.name}
+
`, + )} + `; + } + renderNewLabelModal() { return html` +
-
+
Cancel - + Create
@@ -345,16 +425,16 @@ export class KdbNewConnectionView extends LitElement { renderNewLblBtn() { return html` Create New Label - ${this.isModalOpen ? this.renderNewLabelModal() : ""} `; } - renderConnectionLabelsSection(type: string) { + renderConnectionLabelsSection() { return html`
Connection label (optional)
@@ -362,8 +442,10 @@ export class KdbNewConnectionView extends LitElement { + ${this.renderNewLblBtn()}
`; @@ -372,6 +454,7 @@ export class KdbNewConnectionView extends LitElement { renderNewConnectionForm() { return html`
+ ${this.isModalOpen ? this.renderNewLabelModal() : ""}
@@ -540,7 +623,7 @@ export class KdbNewConnectionView extends LitElement {
- ${this.renderConnectionLabelsSection("insights")} + ${this.renderConnectionLabelsSection()}
@@ -791,6 +874,19 @@ export class KdbNewConnectionView extends LitElement { } } + private createLabel() { + this.vscode.postMessage({ + command: "kdb.labels.create", + data: { + name: this.newLblName, + colorName: this.newLblColorName, + }, + }); + setTimeout(() => { + this.closeModal(); + }, 500); + } + private editConnection() { if (!this.connectionData) { return; diff --git a/src/webview/components/styles.ts b/src/webview/components/styles.ts index 5066127f..ee290445 100644 --- a/src/webview/components/styles.ts +++ b/src/webview/components/styles.ts @@ -165,4 +165,37 @@ export const newConnectionStyles = css` .text-field.larger { width: 20em; } + + .modal { + position: fixed; + top: 50%; + transform: translate(-50%, -50%); + background: var(--vscode-editor-background); + color: var(--vscode-editor-foreground); + padding: 1rem; + z-index: 1001; + border: 1px solid var(--vscode-editorWidget-border); + box-shadow: 0 2px 10px var(--vscode-widget-shadow); + } + + .modal-content h2 { + color: var(--vscode-editor-foreground); + } + vscode-text-field, + vscode-dropdown, + vscode-button { + --vscode-input-background: var(--vscode-editor-background); + --vscode-input-foreground: var(--vscode-editor-foreground); + --vscode-input-border: var(--vscode-editorWidget-border); + } + + .overlay { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: rgba(0, 0, 0, 0.5); + z-index: 1000; + } `; From cf1428548c00e758129199a24259141aa83a5197 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 10:36:20 +0100 Subject: [PATCH 07/12] add label to connection --- package.json | 4 +- src/commands/serverCommand.ts | 24 +++++++- src/extension.ts | 33 ++++++---- src/extensionVariables.ts | 3 +- src/models/labels.ts | 5 +- src/panels/newConnection.ts | 7 ++- src/utils/connLabel.ts | 60 ++++++++++++++++++- .../components/kdbNewConnectionView.ts | 22 +++++-- 8 files changed, 134 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 884bf410..98954665 100644 --- a/package.json +++ b/package.json @@ -187,9 +187,9 @@ "default": [], "scope": "resource" }, - "kdb.connectionLabelsMap": { + "kdb.labelsConnectionMap": { "type": "array", - "description": "Label connection map", + "description": "Labels connection map", "default": [], "scope": "resource" } diff --git a/src/commands/serverCommand.ts b/src/commands/serverCommand.ts index 68379855..2e9b6ddc 100644 --- a/src/commands/serverCommand.ts +++ b/src/commands/serverCommand.ts @@ -67,6 +67,7 @@ import { Telemetry } from "../utils/telemetryClient"; import { ConnectionManagementService } from "../services/connectionManagerService"; import { InsightsConnection } from "../classes/insightsConnection"; import { MetaContentProvider } from "../services/metaContentProvider"; +import { handleLabelsConnMap } from "../utils/connLabel"; export async function addNewConnection(): Promise { NewConnectionPannel.close(); @@ -78,7 +79,10 @@ export async function editConnection(viewItem: KdbNode | InsightsNode) { NewConnectionPannel.render(ext.context.extensionUri, viewItem); } -export async function addInsightsConnection(insightsData: InsightDetails) { +export async function addInsightsConnection( + insightsData: InsightDetails, + labels?: string[], +) { const aliasValidation = validateServerAlias(insightsData.alias, false); if (aliasValidation) { window.showErrorMessage(aliasValidation); @@ -127,12 +131,16 @@ export async function addInsightsConnection(insightsData: InsightDetails) { await updateInsights(insights); const newInsights = getInsights(); if (newInsights != undefined) { + if (labels && labels.length > 0) { + handleLabelsConnMap(labels, insightsData.alias); + } ext.serverProvider.refreshInsights(newInsights); Telemetry.sendEvent("Connection.Created.Insights"); } window.showInformationMessage( `Added Insights connection: ${insightsData.alias}`, ); + NewConnectionPannel.close(); } } @@ -141,6 +149,7 @@ export async function addInsightsConnection(insightsData: InsightDetails) { export async function editInsightsConnection( insightsData: InsightDetails, oldAlias: string, + labels?: string[], ) { const aliasValidation = oldAlias === insightsData.alias @@ -207,12 +216,16 @@ export async function editInsightsConnection( const newInsights = getInsights(); if (newInsights != undefined) { + if (labels && labels.length > 0) { + handleLabelsConnMap(labels, insightsData.alias); + } ext.serverProvider.refreshInsights(newInsights); Telemetry.sendEvent("Connection.Edited.Insights"); } window.showInformationMessage( `Edited Insights connection: ${insightsData.alias}`, ); + NewConnectionPannel.close(); } } @@ -301,6 +314,7 @@ export async function enableTLS(serverKey: string): Promise { export async function addKdbConnection( kdbData: ServerDetails, isLocal?: boolean, + labels?: string[], ): Promise { const aliasValidation = validateServerAlias(kdbData.serverAlias, isLocal!); const hostnameValidation = validateServerName(kdbData.serverName); @@ -359,6 +373,9 @@ export async function addKdbConnection( await updateServers(servers); const newServers = getServers(); if (newServers != undefined) { + if (labels && labels.length > 0) { + handleLabelsConnMap(labels, kdbData.serverAlias); + } Telemetry.sendEvent("Connection.Created.QProcess"); ext.serverProvider.refresh(newServers); } @@ -368,6 +385,7 @@ export async function addKdbConnection( window.showInformationMessage( `Added kdb connection: ${kdbData.serverAlias}`, ); + NewConnectionPannel.close(); } } @@ -378,6 +396,7 @@ export async function editKdbConnection( oldAlias: string, isLocal?: boolean, editAuth?: boolean, + labels?: string[], ) { const aliasValidation = oldAlias === kdbData.serverAlias @@ -455,6 +474,9 @@ export async function editKdbConnection( } const newServers = getServers(); if (newServers != undefined) { + if (labels && labels.length > 0) { + handleLabelsConnMap(labels, kdbData.serverAlias); + } ext.serverProvider.refresh(newServers); Telemetry.sendEvent("Connection.Edited.KDB"); } diff --git a/src/extension.ts b/src/extension.ts index 1649390e..c8ebe394 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -270,38 +270,47 @@ export async function activate(context: ExtensionContext) { ), commands.registerCommand( "kdb.newConnection.createNewInsightConnection", - async (insightsData: InsightDetails) => { - await addInsightsConnection(insightsData); + async (insightsData: InsightDetails, labels: string[]) => { + await addInsightsConnection(insightsData, labels); }, ), commands.registerCommand( "kdb.newConnection.createNewConnection", - async (kdbData: ServerDetails) => { - await addKdbConnection(kdbData, false); + async (kdbData: ServerDetails, labels: string[]) => { + await addKdbConnection(kdbData, false, labels); }, ), commands.registerCommand( "kdb.newConnection.createNewBundledConnection", - async (kdbData: ServerDetails) => { - await addKdbConnection(kdbData, true); + async (kdbData: ServerDetails, labels: string[]) => { + await addKdbConnection(kdbData, true, labels); }, ), commands.registerCommand( "kdb.newConnection.editInsightsConnection", - async (insightsData: InsightDetails, oldAlias: string) => { - await editInsightsConnection(insightsData, oldAlias); + async ( + insightsData: InsightDetails, + oldAlias: string, + labels: string[], + ) => { + await editInsightsConnection(insightsData, oldAlias, labels); }, ), commands.registerCommand( "kdb.newConnection.editMyQConnection", - async (kdbData: ServerDetails, oldAlias: string, editAuth: boolean) => { - await editKdbConnection(kdbData, oldAlias, false, editAuth); + async ( + kdbData: ServerDetails, + oldAlias: string, + editAuth: boolean, + labels: string[], + ) => { + await editKdbConnection(kdbData, oldAlias, false, editAuth, labels); }, ), commands.registerCommand( "kdb.newConnection.editBundledConnection", - async (kdbData: ServerDetails, oldAlias: string) => { - await editKdbConnection(kdbData, oldAlias, true); + async (kdbData: ServerDetails, oldAlias: string, labels: string[]) => { + await editKdbConnection(kdbData, oldAlias, true, false, labels); }, ), commands.registerCommand( diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 119a66f8..93984651 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -37,7 +37,7 @@ import { ScratchpadFile } from "./models/scratchpad"; import { LocalConnection } from "./classes/localConnection"; import { InsightsConnection } from "./classes/insightsConnection"; import { DataSourceFiles } from "./models/dataSource"; -import { LabelColors, Labels } from "./models/labels"; +import { ConnectionLabels, LabelColors, Labels } from "./models/labels"; // eslint-disable-next-line @typescript-eslint/no-namespace export namespace ext { @@ -86,6 +86,7 @@ export namespace ext { export const kdbNodesWithoutTls: string[] = []; export const kdbConnectionAliasList: string[] = []; export const connLabelList: Labels[] = []; + export const labelConnMapList: ConnectionLabels[] = []; export const maxRetryCount = 5; export let secretSettings: AuthSettings; diff --git a/src/models/labels.ts b/src/models/labels.ts index 2410b81f..dc9797a6 100644 --- a/src/models/labels.ts +++ b/src/models/labels.ts @@ -21,6 +21,7 @@ export type Labels = { color: LabelColors; }; -export type ConnectionLabels = { - [labelName: string]: string[]; // Array of connection names +export type ConnectionLabel = { + labelName: string; + connections: string[]; }; diff --git a/src/panels/newConnection.ts b/src/panels/newConnection.ts index 99591e9b..c62189f0 100644 --- a/src/panels/newConnection.ts +++ b/src/panels/newConnection.ts @@ -17,7 +17,6 @@ import { getNonce } from "../utils/getNonce"; import { ext } from "../extensionVariables"; import { InsightsNode, KdbNode } from "../services/kdbTreeProvider"; import { ConnectionType, EditConnectionMessage } from "../models/messages"; -import { getWorkspaceLabels } from "../utils/connLabel"; export class NewConnectionPannel { public static currentPanel: NewConnectionPannel | undefined; @@ -87,6 +86,7 @@ export class NewConnectionPannel { vscode.commands.executeCommand( "kdb.newConnection.createNewBundledConnection", message.data, + message.labels, ); } } @@ -94,12 +94,14 @@ export class NewConnectionPannel { vscode.commands.executeCommand( "kdb.newConnection.createNewInsightConnection", message.data, + message.labels, ); } if (message.command === "kdb.newConnection.createNewConnection") { vscode.commands.executeCommand( "kdb.newConnection.createNewConnection", message.data, + message.labels, ); } if (message.command === "kdb.newConnection.editInsightsConnection") { @@ -107,6 +109,7 @@ export class NewConnectionPannel { "kdb.newConnection.editInsightsConnection", message.data, message.oldAlias, + message.labels, ); } if (message.command === "kdb.newConnection.editMyQConnection") { @@ -115,6 +118,7 @@ export class NewConnectionPannel { message.data, message.oldAlias, message.editAuth, + message.labels, ); } if (message.command === "kdb.newConnection.editBundledConnection") { @@ -122,6 +126,7 @@ export class NewConnectionPannel { "kdb.newConnection.editBundledConnection", message.data, message.oldAlias, + message.labels, ); } if (message.command === "kdb.labels.create") { diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index e9239eaf..3147ccc6 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -11,7 +11,7 @@ * specific language governing permissions and limitations under the License. */ -import { Labels } from "../models/labels"; +import { ConnectionLabels, Labels } from "../models/labels"; import { workspace } from "vscode"; import { ext } from "../extensionVariables"; import { kdbOutputLog } from "./core"; @@ -47,3 +47,61 @@ export function createNewLabel(name: string, colorName: string) { kdbOutputLog("No Color selected for the label", "ERROR"); } } + +export function getWorkspaceLabelsConnMap() { + const existingLabelConnMaps = workspace + .getConfiguration() + .get("kdb.labelsConnectionMap"); + ext.labelConnMapList.length = 0; + if (existingLabelConnMaps && existingLabelConnMaps.length > 0) { + existingLabelConnMaps.forEach((labelConnMap: ConnectionLabels) => { + ext.labelConnMapList.push(labelConnMap); + }); + } +} + +export function addConnToLabel(labelName: string, connName: string) { + const label = ext.connLabelList.find((lbl) => lbl.name === labelName); + if (label) { + if (ext.labelConnMapList.length > 0) { + const labelConnMap = ext.labelConnMapList.find( + (lbl) => lbl.labelName === labelName, + ); + if (labelConnMap) { + if (!labelConnMap.connections.includes(connName)) { + labelConnMap.connections.push(connName); + } + } else { + ext.labelConnMapList.push({ + labelName: labelName, + connections: [connName], + }); + } + } else { + ext.labelConnMapList.push({ + labelName: labelName, + connections: [connName], + }); + } + } +} + +export function removeConnFromLabels(connName: string) { + ext.labelConnMapList.forEach((labelConnMap) => { + if (labelConnMap.connections.includes(connName)) { + labelConnMap.connections = labelConnMap.connections.filter( + (conn: string) => conn !== connName, + ); + } + }); +} + +export function handleLabelsConnMap(labels: string[], connName: string) { + removeConnFromLabels(connName); + labels.forEach((label) => { + addConnToLabel(label, connName); + }); + workspace + .getConfiguration() + .update("kdb.labelsConnectionMap", ext.labelConnMapList, true); +} diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index 4cca8da1..b174e909 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -82,7 +82,7 @@ export class KdbNewConnectionView extends LitElement { realm: "", insecure: false, }; - labels = []; + labels: string[] = []; serverType: ServerType = ServerType.KDB; isBundledQ: boolean = true; oldAlias: string = ""; @@ -441,7 +441,14 @@ export class KdbNewConnectionView extends LitElement {
@@ -518,7 +525,7 @@ export class KdbNewConnectionView extends LitElement {
${this.renderPortNumber()}
- ${this.renderConnectionLabelsSection("bundleQ")} + ${this.renderConnectionLabelsSection()}
@@ -589,7 +596,7 @@ export class KdbNewConnectionView extends LitElement {
- ${this.renderConnectionLabelsSection("myQ")} + ${this.renderConnectionLabelsSection()} @@ -658,6 +665,7 @@ export class KdbNewConnectionView extends LitElement {
${this.renderEditConnFields()}
+
${this.renderConnectionLabelsSection()}
@@ -860,16 +868,19 @@ export class KdbNewConnectionView extends LitElement { this.vscode.postMessage({ command: "kdb.newConnection.createNewBundledConnection", data: this.bundledServer, + labels: this.labels, }); } else if (this.serverType === ServerType.INSIGHTS) { this.vscode.postMessage({ command: "kdb.newConnection.createNewInsightConnection", data: this.data, + labels: this.labels, }); } else { this.vscode.postMessage({ command: "kdb.newConnection.createNewConnection", data: this.data, + labels: this.labels, }); } } @@ -896,6 +907,7 @@ export class KdbNewConnectionView extends LitElement { command: "kdb.newConnection.editBundledConnection", data: this.bundledServer, oldAlias: "local", + labels: this.labels, }); } else if (this.connectionData.connType === 1) { this.vscode.postMessage({ @@ -903,12 +915,14 @@ export class KdbNewConnectionView extends LitElement { data: this.data, oldAlias: this.oldAlias, editAuth: this.editAuth, + labels: this.labels, }); } else { this.vscode.postMessage({ command: "kdb.newConnection.editInsightsConnection", data: this.data, oldAlias: this.oldAlias, + labels: this.labels, }); } } From 5960ad38d2c622c2ff9ec821cdbcc3554bdc61bb Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 11:15:26 +0100 Subject: [PATCH 08/12] edit labels --- src/extension.ts | 7 +++++- src/panels/newConnection.ts | 12 ++++++---- src/utils/connLabel.ts | 21 +++++++++++++--- .../components/kdbNewConnectionView.ts | 24 +++++++++++-------- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index c8ebe394..13e4e964 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -104,7 +104,11 @@ import { connectBuildTools, lintCommand } from "./commands/buildToolsCommand"; import { CompletionProvider } from "./services/completionProvider"; import { QuickFixProvider } from "./services/quickFixProvider"; import { connectClientCommands } from "./commands/clientCommands"; -import { createNewLabel, getWorkspaceLabels } from "./utils/connLabel"; +import { + createNewLabel, + getWorkspaceLabels, + getWorkspaceLabelsConnMap, +} from "./utils/connLabel"; let client: LanguageClient; @@ -114,6 +118,7 @@ export async function activate(context: ExtensionContext) { ext.openSslVersion = await checkOpenSslInstalled(); ext.isBundleQCreated = false; + getWorkspaceLabelsConnMap(); getWorkspaceLabels(); // clear necessary contexts diff --git a/src/panels/newConnection.ts b/src/panels/newConnection.ts index c62189f0..b3d8597e 100644 --- a/src/panels/newConnection.ts +++ b/src/panels/newConnection.ts @@ -17,6 +17,7 @@ import { getNonce } from "../utils/getNonce"; import { ext } from "../extensionVariables"; import { InsightsNode, KdbNode } from "../services/kdbTreeProvider"; import { ConnectionType, EditConnectionMessage } from "../models/messages"; +import { retrieveConnLabelsNames } from "../utils/connLabel"; export class NewConnectionPannel { public static currentPanel: NewConnectionPannel | undefined; @@ -46,18 +47,21 @@ export class NewConnectionPannel { extensionUri, ); + panel.webview.postMessage({ + command: "refreshLabels", + data: ext.connLabelList, + }); + if (conn) { + const labels = retrieveConnLabelsNames(conn); const connType = this.getConnectionType(conn); const editConnData = this.createEditConnectionMessage(conn, connType); panel.webview.postMessage({ command: "editConnection", data: editConnData, + labels, }); } - panel.webview.postMessage({ - command: "refreshLabels", - data: ext.connLabelList, - }); } public static close() { diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index 3147ccc6..00597e09 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -11,10 +11,11 @@ * specific language governing permissions and limitations under the License. */ -import { ConnectionLabels, Labels } from "../models/labels"; +import { ConnectionLabel, Labels } from "../models/labels"; import { workspace } from "vscode"; import { ext } from "../extensionVariables"; import { kdbOutputLog } from "./core"; +import { InsightsNode, KdbNode } from "../services/kdbTreeProvider"; export function getWorkspaceLabels() { const existingConnLbls = workspace @@ -51,10 +52,10 @@ export function createNewLabel(name: string, colorName: string) { export function getWorkspaceLabelsConnMap() { const existingLabelConnMaps = workspace .getConfiguration() - .get("kdb.labelsConnectionMap"); + .get("kdb.labelsConnectionMap"); ext.labelConnMapList.length = 0; if (existingLabelConnMaps && existingLabelConnMaps.length > 0) { - existingLabelConnMaps.forEach((labelConnMap: ConnectionLabels) => { + existingLabelConnMaps.forEach((labelConnMap: ConnectionLabel) => { ext.labelConnMapList.push(labelConnMap); }); } @@ -105,3 +106,17 @@ export function handleLabelsConnMap(labels: string[], connName: string) { .getConfiguration() .update("kdb.labelsConnectionMap", ext.labelConnMapList, true); } + +export function retrieveConnLabelsNames( + conn: KdbNode | InsightsNode, +): string[] { + const connName = + conn instanceof KdbNode ? conn.details.serverAlias : conn.details.alias; + const labels: string[] = []; + ext.labelConnMapList.forEach((labelConnMap) => { + if (labelConnMap.connections.includes(connName)) { + labels.push(labelConnMap.labelName); + } + }); + return labels; +} diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index b174e909..64a187e5 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -146,6 +146,8 @@ export class KdbNewConnectionView extends LitElement { const message = event.data; if (message.command === "editConnection") { this.connectionData = message.data; + this.labels = message.labels; + this.requestUpdate(); } if (message.command === "refreshLabels") { this.lblNamesList = message.data; @@ -356,17 +358,19 @@ export class KdbNewConnectionView extends LitElement { No Label Selected ${repeat( this.lblNamesList, - (lbl) => lbl, - (lbl) => - html` -
lbl.name, + (lbl) => html` + + +
- ${lbl.name}
-
`, + .colorHex}; border-radius: 50%; float: left; margin-right: 10px; margin-top: 3px;">
+ ${lbl.name} +
+
+ `, )} `; } From 7085173a685aad10cb32457d763bbd6534170bb9 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 11:18:52 +0100 Subject: [PATCH 09/12] fix typo --- src/extensionVariables.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 93984651..3a923739 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -37,7 +37,7 @@ import { ScratchpadFile } from "./models/scratchpad"; import { LocalConnection } from "./classes/localConnection"; import { InsightsConnection } from "./classes/insightsConnection"; import { DataSourceFiles } from "./models/dataSource"; -import { ConnectionLabels, LabelColors, Labels } from "./models/labels"; +import { ConnectionLabel, LabelColors, Labels } from "./models/labels"; // eslint-disable-next-line @typescript-eslint/no-namespace export namespace ext { @@ -86,7 +86,7 @@ export namespace ext { export const kdbNodesWithoutTls: string[] = []; export const kdbConnectionAliasList: string[] = []; export const connLabelList: Labels[] = []; - export const labelConnMapList: ConnectionLabels[] = []; + export const labelConnMapList: ConnectionLabel[] = []; export const maxRetryCount = 5; export let secretSettings: AuthSettings; From 81f2eac4e34e8047feb5703c8adaedca23299a7b Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 11:43:55 +0100 Subject: [PATCH 10/12] improvement --- src/panels/newConnection.ts | 2 ++ .../components/kdbNewConnectionView.ts | 32 ++----------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/src/panels/newConnection.ts b/src/panels/newConnection.ts index b3d8597e..b6df569f 100644 --- a/src/panels/newConnection.ts +++ b/src/panels/newConnection.ts @@ -50,6 +50,7 @@ export class NewConnectionPannel { panel.webview.postMessage({ command: "refreshLabels", data: ext.connLabelList, + colors: ext.labelColors, }); if (conn) { @@ -143,6 +144,7 @@ export class NewConnectionPannel { this._panel.webview.postMessage({ command: "refreshLabels", data: ext.connLabelList, + colors: ext.labelColors, }); }, 500); } diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index 64a187e5..8cca7f0e 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -24,36 +24,7 @@ import { LabelColors, Labels } from "../../models/labels"; @customElement("kdb-new-connection-view") export class KdbNewConnectionView extends LitElement { static styles = [vscodeStyles, kdbStyles, newConnectionStyles]; - lblColorsList: LabelColors[] = [ - { - name: "White", - colorHex: "#FFFFFF", - }, - { - name: "Red", - colorHex: "#CD3131", - }, - { - name: "Green", - colorHex: "#10BC7A", - }, - { - name: "Yellow", - colorHex: "#E5E50E", - }, - { - name: "Blue", - colorHex: "#2371C8", - }, - { - name: "Magenta", - colorHex: "#BC3FBC", - }, - { - name: "Cyan", - colorHex: "#15A7CD", - }, - ]; + lblColorsList: LabelColors[] = []; lblNamesList: Labels[] = []; newLblName = ""; newLblColorName = ""; @@ -151,6 +122,7 @@ export class KdbNewConnectionView extends LitElement { } if (message.command === "refreshLabels") { this.lblNamesList = message.data; + this.lblColorsList = message.colors; this.requestUpdate(); } } From 6c8b4227d950ad1fe8447bd37ee980e9d5012d61 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 12:31:53 +0100 Subject: [PATCH 11/12] add test --- src/utils/connLabel.ts | 8 +- test/suite/utils.test.ts | 155 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 160 insertions(+), 3 deletions(-) diff --git a/src/utils/connLabel.ts b/src/utils/connLabel.ts index 00597e09..76b293bc 100644 --- a/src/utils/connLabel.ts +++ b/src/utils/connLabel.ts @@ -31,7 +31,9 @@ export function getWorkspaceLabels() { export function createNewLabel(name: string, colorName: string) { getWorkspaceLabels(); - const color = ext.labelColors.find((color) => color.name === colorName); + const color = ext.labelColors.find( + (color) => color.name.toLowerCase() === colorName.toLowerCase(), + ); if (name === "") { kdbOutputLog("Label name can't be empty", "ERROR"); } @@ -62,7 +64,9 @@ export function getWorkspaceLabelsConnMap() { } export function addConnToLabel(labelName: string, connName: string) { - const label = ext.connLabelList.find((lbl) => lbl.name === labelName); + const label = ext.connLabelList.find( + (lbl) => lbl.name.toLowerCase() === labelName.toLowerCase(), + ); if (label) { if (ext.labelConnMapList.length > 0) { const labelConnMap = ext.labelConnMapList.find( diff --git a/test/suite/utils.test.ts b/test/suite/utils.test.ts index 3572807c..aa41407b 100644 --- a/test/suite/utils.test.ts +++ b/test/suite/utils.test.ts @@ -23,13 +23,13 @@ import { QueryResultType } from "../../src/models/queryResult"; import { ServerDetails, ServerType } from "../../src/models/server"; import { InsightsNode, KdbNode } from "../../src/services/kdbTreeProvider"; import { QueryHistoryProvider } from "../../src/services/queryHistoryProvider"; -import { KdbResultsViewProvider } from "../../src/services/resultsPanelProvider"; import * as coreUtils from "../../src/utils/core"; import * as cpUtils from "../../src/utils/cpUtils"; import * as dataSourceUtils from "../../src/utils/dataSource"; import * as decodeUtils from "../../src/utils/decode"; import * as executionUtils from "../../src/utils/execution"; import * as executionConsoleUtils from "../../src/utils/executionConsole"; +import * as LabelsUtils from "../../src/utils/connLabel"; import { getNonce } from "../../src/utils/getNonce"; import { getUri } from "../../src/utils/getUri"; import { openUrl } from "../../src/utils/openUrl"; @@ -51,6 +51,7 @@ import { import { DataSourceTypes } from "../../src/models/dataSource"; import { InsightDetails } from "../../src/models/insights"; import { LocalConnection } from "../../src/classes/localConnection"; +import { Labels } from "../../src/models/labels"; interface ITestItem extends vscode.QuickPickItem { id: number; @@ -1593,4 +1594,156 @@ describe("Utils", () => { }); }); }); + + describe("connLabelsUtils", () => { + let getConfigurationStub: sinon.SinonStub; + + beforeEach(() => { + getConfigurationStub = sinon.stub(vscode.workspace, "getConfiguration"); + ext.connLabelList.length = 0; + ext.labelConnMapList.length = 0; + }); + + afterEach(() => { + sinon.restore(); + }); + + it("should get workspace labels", () => { + const labels: Labels[] = [ + { name: "label1", color: { name: "red", colorHex: "#FF0000" } }, + ]; + getConfigurationStub.returns({ + get: sinon.stub().returns(labels), + update: sinon.stub(), + }); + + LabelsUtils.getWorkspaceLabels(); + + assert.strictEqual(ext.connLabelList.length, 1); + + assert.deepStrictEqual(ext.connLabelList, labels); + }); + + it("should create a new label", () => { + getConfigurationStub.returns({ + get: sinon.stub().returns([]), + update: sinon.stub(), + }); + + LabelsUtils.createNewLabel("label1", "red"); + + assert.strictEqual(ext.connLabelList.length, 1); + assert.strictEqual(ext.connLabelList[0].name, "label1"); + assert.strictEqual(ext.connLabelList[0].color.name, "Red"); + }); + + it("should handle empty label name", () => { + getConfigurationStub.returns({ + get: sinon.stub(), + update: sinon.stub(), + }); + const logStub = sinon.stub(coreUtils, "kdbOutputLog"); + + LabelsUtils.createNewLabel("", "red"); + + sinon.assert.calledWith(logStub, "Label name can't be empty", "ERROR"); + }); + + it("should handle no color selected", () => { + getConfigurationStub.returns({ + get: sinon.stub(), + update: sinon.stub(), + }); + const logStub = sinon.stub(coreUtils, "kdbOutputLog"); + + LabelsUtils.createNewLabel("label1", "randomColorName"); + + sinon.assert.calledWith( + logStub, + "No Color selected for the label", + "ERROR", + ); + }); + + it("should get workspace labels connection map", () => { + const connMap = [{ labelName: "label1", connections: ["conn1"] }]; + getConfigurationStub.returns({ + get: sinon.stub().returns(connMap), + update: sinon.stub(), + }); + + LabelsUtils.getWorkspaceLabelsConnMap(); + + assert.deepStrictEqual(ext.labelConnMapList, connMap); + }); + + it("should add connection to label", () => { + ext.connLabelList.push({ + name: "label1", + color: { name: "red", colorHex: "#FF0000" }, + }); + + LabelsUtils.addConnToLabel("label1", "conn1"); + + assert.strictEqual(ext.labelConnMapList.length, 1); + assert.strictEqual(ext.labelConnMapList[0].labelName, "label1"); + assert.deepStrictEqual(ext.labelConnMapList[0].connections, ["conn1"]); + }); + + it("should remove connection from labels", () => { + ext.labelConnMapList.push({ + labelName: "label1", + connections: ["conn1", "conn2"], + }); + + LabelsUtils.removeConnFromLabels("conn1"); + + assert.deepStrictEqual(ext.labelConnMapList[0].connections, ["conn2"]); + }); + + it("should handle labels connection map", () => { + ext.connLabelList.push({ + name: "label1", + color: { name: "Red", colorHex: "#FF0000" }, + }); + ext.labelConnMapList.push({ + labelName: "label1", + connections: ["conn2"], + }); + + getConfigurationStub.returns({ + get: sinon.stub(), + update: sinon.stub(), + }); + + LabelsUtils.handleLabelsConnMap(["label1"], "conn2"); + + assert.strictEqual(ext.labelConnMapList.length, 1); + assert.deepStrictEqual(ext.labelConnMapList[0].connections, ["conn2"]); + }); + + it("should retrieve connection labels names", () => { + ext.labelConnMapList.push({ + labelName: "label1", + connections: ["conn1"], + }); + const conn = new KdbNode( + [], + "conn1", + { + serverName: "kdbservername", + serverAlias: "conn1", + serverPort: "5001", + managed: false, + auth: false, + tls: false, + }, + TreeItemCollapsibleState.None, + ); + + const labels = LabelsUtils.retrieveConnLabelsNames(conn); + + assert.deepStrictEqual(labels, ["label1"]); + }); + }); }); From f61b1601d7827d875e5bddabaa5661016d03ba52 Mon Sep 17 00:00:00 2001 From: Philip Carneiro Date: Fri, 2 Aug 2024 12:59:33 +0100 Subject: [PATCH 12/12] add more tests --- test/suite/webview.test.ts | 72 +++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/test/suite/webview.test.ts b/test/suite/webview.test.ts index dd4d9e54..1bd2320a 100644 --- a/test/suite/webview.test.ts +++ b/test/suite/webview.test.ts @@ -22,7 +22,6 @@ import { ServerType } from "../../src/models/server"; import { InsightDetails } from "../../src/models/insights"; import { - ConnectionType, DataSourceCommand, DataSourceMessage2, } from "../../src/models/messages"; @@ -36,6 +35,7 @@ import { } from "../../src/models/dataSource"; import { MetaObjectPayload } from "../../src/models/meta"; import { html, TemplateResult } from "lit"; +import { ext } from "../../src/extensionVariables"; describe("KdbDataSourceView", () => { let view: KdbDataSourceView; @@ -247,6 +247,20 @@ describe("KdbNewConnectionView", () => { assert.equal(view.connectionData, event.data.data); }); + it('should update connectionData when command is "refreshLabels"', () => { + const event = { + data: { + command: "refreshLabels", + data: ["test"], + colors: ext.labelColors, + }, + }; + + view.handleMessage(event); + + assert.equal(view.lblNamesList, event.data.data); + }); + it('should not update connectionData when command is not "editConnection"', () => { const event = { data: { @@ -442,6 +456,62 @@ describe("KdbNewConnectionView", () => { true, ); }); + + it("should render label dropdown color options", () => { + view.lblColorsList = [ + { name: "red", colorHex: "#FF0000" }, + { name: "green", colorHex: "#00FF00" }, + ]; + + const result = view.renderLblDropdownColorOptions(); + + assert.strictEqual( + JSON.stringify(result).includes("No Color Selected"), + true, + ); + }); + + it("should render label dropdown options", () => { + view.lblNamesList = [ + { name: "label1", color: { colorHex: "#FF0000" } }, + { name: "label2", color: { colorHex: "#00FF00" } }, + ]; + view.labels = ["label1"]; + + const result = view.renderLblDropdownOptions(); + + assert.strictEqual( + JSON.stringify(result).includes("No Label Selected"), + true, + ); + }); + + it("should render New Label Modal", () => { + const result = view.renderNewLabelModal(); + + assert.strictEqual( + JSON.stringify(result).includes("Add a New Label"), + true, + ); + }); + + it("should render New Label Btn", () => { + const result = view.renderNewLblBtn(); + + assert.strictEqual( + JSON.stringify(result).includes("Create New Label"), + true, + ); + }); + + it("should render Connection Label Section", () => { + const result = view.renderConnectionLabelsSection(); + + assert.strictEqual( + JSON.stringify(result).includes("Connection label (optional)"), + true, + ); + }); }); describe("tabClickAction", () => {