diff --git a/src/commands/serverCommand.ts b/src/commands/serverCommand.ts index 6e9fea9e..a277b704 100644 --- a/src/commands/serverCommand.ts +++ b/src/commands/serverCommand.ts @@ -68,7 +68,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"; +import { handleLabelsConnMap, removeConnFromLabels } from "../utils/connLabel"; export async function addNewConnection(): Promise { NewConnectionPannel.close(); @@ -138,7 +138,7 @@ export async function addInsightsConnection( const newInsights = getInsights(); if (newInsights != undefined) { if (labels && labels.length > 0) { - handleLabelsConnMap(labels, insightsData.alias); + await handleLabelsConnMap(labels, insightsData.alias); } ext.serverProvider.refreshInsights(newInsights); Telemetry.sendEvent("Connection.Created.Insights"); @@ -193,6 +193,7 @@ export async function editInsightsConnection( const oldKey = getKeyForServerName(oldAlias); const newKey = insightsData.alias; if (insights[oldKey] && oldAlias !== insightsData.alias) { + removeConnFromLabels(oldAlias); const uInsights = Object.keys(insights).filter((insight) => { return insight !== oldKey; }); @@ -225,6 +226,8 @@ export async function editInsightsConnection( if (newInsights != undefined) { if (labels && labels.length > 0) { await handleLabelsConnMap(labels, insightsData.alias); + } else { + removeConnFromLabels(insightsData.alias); } ext.serverProvider.refreshInsights(newInsights); Telemetry.sendEvent("Connection.Edited.Insights"); @@ -384,7 +387,7 @@ export async function addKdbConnection( const newServers = getServers(); if (newServers != undefined) { if (labels && labels.length > 0) { - handleLabelsConnMap(labels, kdbData.serverAlias); + await handleLabelsConnMap(labels, kdbData.serverAlias); } Telemetry.sendEvent("Connection.Created.QProcess"); ext.serverProvider.refresh(newServers); @@ -487,6 +490,8 @@ export async function editKdbConnection( if (newServers != undefined) { if (labels && labels.length > 0) { await handleLabelsConnMap(labels, kdbData.serverAlias); + } else { + removeConnFromLabels(kdbData.serverAlias); } ext.serverProvider.refresh(newServers); Telemetry.sendEvent("Connection.Edited.KDB"); @@ -499,6 +504,7 @@ export async function editKdbConnection( `Edited KDB connection: ${kdbData.serverAlias}`, ); if (oldKey !== newKey) { + removeConnFromLabels(oldKey); removeAuthConnection(oldKey); if (kdbData.auth) { addAuthConnection(newKey, kdbData.username!, kdbData.password!); diff --git a/src/services/connectionManagerService.ts b/src/services/connectionManagerService.ts index ff8f8d7f..1df1ab20 100644 --- a/src/services/connectionManagerService.ts +++ b/src/services/connectionManagerService.ts @@ -47,8 +47,15 @@ export class ConnectionManagementService { ): LocalConnection | InsightsConnection | undefined { return ext.connectedConnectionList.find( (connection: LocalConnection | InsightsConnection) => { + if (!connLabel) { + return false; + } + const escapedConnLabel = connLabel.replace( + /[-[\]{}()*+?.,\\^$|#\s]/g, + "\\$&", + ); const regex = new RegExp( - `\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${connLabel}\\]`, + `\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${escapedConnLabel}\\]`, ); return ( connLabel === connection.connLabel || regex.test(connection.connLabel) @@ -62,8 +69,12 @@ export class ConnectionManagementService { } public isConnected(connLabel: string): boolean { + const escapedConnLabel = connLabel.replace( + /[-[\]{}()*+?.,\\^$|#\s]/g, + "\\$&", + ); const regex = new RegExp( - `\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${connLabel}\\]`, + `\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${escapedConnLabel}\\]`, ); return ( ext.connectedContextStrings.includes(connLabel) || diff --git a/src/webview/components/kdbNewConnectionView.ts b/src/webview/components/kdbNewConnectionView.ts index 8fede5eb..646eb393 100644 --- a/src/webview/components/kdbNewConnectionView.ts +++ b/src/webview/components/kdbNewConnectionView.ts @@ -135,8 +135,8 @@ export class KdbNewConnectionView extends LitElement { this.editAuth = !this.editAuth; } - renderServerNameDesc() { - return this.isBundledQ + renderServerNameDesc(isBundleQ?: boolean) { + return isBundleQ ? html`Name your server "local"; this name has been reserved for use by the packaged q in the kdb VS Code extension and must be used to access @@ -149,8 +149,8 @@ export class KdbNewConnectionView extends LitElement { >`; } - renderServerNameField(serverType: ServerType) { - return this.isBundledQ + renderServerNameField(serverType: ServerType, isBundleQ?: boolean) { + return isBundleQ ? html``; } - renderServerName(serverType: ServerType) { + renderServerName(serverType: ServerType, isBundleQ?: boolean) { return html` -
${this.renderServerNameField(serverType)}
+
+ ${this.renderServerNameField(serverType, isBundleQ)} +
- ${this.renderServerNameDesc()} + ${this.renderServerNameDesc(isBundleQ)}
`; } - renderPortNumberDesc() { - return this.isBundledQ + renderPortNumberDesc(isBundleQ?: boolean) { + return isBundleQ ? html`Ensure the port number you use does not conflict with another port.`; } - renderPortNumber() { + renderPortNumber(isBundleQ?: boolean) { return html`
- ${this.renderPortNumberDesc()} + ${this.renderPortNumberDesc(isBundleQ)}
`; } - renderConnAddDesc(serverType: ServerType) { - return this.isBundledQ + renderConnAddDesc(serverType: ServerType, isBundleQ?: boolean) { + return isBundleQ ? html`The localhost connection is already set up for you.` : serverType === ServerType.KDB ? html`Set the IP of your kdb+ database connection.` @@ -233,8 +235,8 @@ export class KdbNewConnectionView extends LitElement { connection must be deployed for kdb VS Code to access.`; } - renderConnAddress(serverType: ServerType) { - return this.isBundledQ + renderConnAddress(serverType: ServerType, isBundleQ?: boolean) { + return isBundleQ ? html`
- ${this.renderServerName(ServerType.KDB)} + ${this.renderServerName(ServerType.KDB, true)}
- ${this.renderConnAddress(ServerType.KDB)} + ${this.renderConnAddress(ServerType.KDB, true)}
-
${this.renderPortNumber()}
+
${this.renderPortNumber(true)}
${this.renderConnectionLabelsSection()}
@@ -826,7 +828,7 @@ export class KdbNewConnectionView extends LitElement { render() { if (!this.connectionData) { - return this.renderNewConnectionForm(); + return html` ${this.renderNewConnectionForm()} `; } else { return html` ${this.renderEditConnectionForm()} `; } diff --git a/test/suite/commands.test.ts b/test/suite/commands.test.ts index 7f7123d7..548de197 100644 --- a/test/suite/commands.test.ts +++ b/test/suite/commands.test.ts @@ -985,7 +985,7 @@ describe("serverCommand", () => { }); it("should add new Insights connection", async () => { getInsightsStub.returns({}); - await serverCommand.addInsightsConnection(insightsData); + await serverCommand.addInsightsConnection(insightsData, ["lblTest"]); sinon.assert.calledOnce(updateInsightsStub); windowMock .expects("showInformationMessage") @@ -1035,7 +1035,7 @@ describe("serverCommand", () => { it("should add new Kdb connection", async () => { getServersStub.returns({}); - await serverCommand.addKdbConnection(kdbData); + await serverCommand.addKdbConnection(kdbData, false, ["lblTest"]); sinon.assert.calledOnce(updateServersStub); windowMock .expects("showInformationMessage") diff --git a/test/suite/webview.test.ts b/test/suite/webview.test.ts index 1bd2320a..007d9f2b 100644 --- a/test/suite/webview.test.ts +++ b/test/suite/webview.test.ts @@ -373,8 +373,7 @@ describe("KdbNewConnectionView", () => { }); it("should render port number desc for KDB server", () => { - view.isBundledQ = false; - const result = view.renderPortNumberDesc(ServerType.KDB); + const result = view.renderPortNumberDesc(); assert.strictEqual( JSON.stringify(result).includes("Set port number"), true, @@ -393,8 +392,7 @@ describe("KdbNewConnectionView", () => { }); it("should render port number for KDB server", () => { - view.isBundledQ = false; - const result = view.renderPortNumber(ServerType.KDB); + const result = view.renderPortNumber(); assert.strictEqual( JSON.stringify(result).includes("Set port number"), true, @@ -421,9 +419,10 @@ describe("KdbNewConnectionView", () => { }); it("should render connection address for Bundled q", () => { - const result = view.renderConnAddDesc(ServerType.KDB); + const result = view.renderConnAddDesc(ServerType.KDB, true); + console.log(JSON.stringify(result)); assert.strictEqual( - result.strings[0].includes("lready set up for you"), + result.strings[0].includes("already set up for you"), true, ); }); @@ -439,9 +438,8 @@ describe("KdbNewConnectionView", () => { ); }); - it("should render connection address for bundled q", () => { - view.isBundledQ = true; - const result = view.renderConnAddress(ServerType.KDB); + it("should render connection address for Bundled q", () => { + const result = view.renderConnAddress(ServerType.KDB, true); assert.strictEqual( JSON.stringify(result).includes("127.0.0.1 or localhost"), false, @@ -691,7 +689,6 @@ describe("KdbNewConnectionView", () => { serverName: "local", }; const result = view.renderBundleQEditForm(); - console.log(JSON.stringify(result)); assert.ok(result.strings[0].includes('
')); assert.ok(result.strings[1].includes('
')); assert.ok(result.strings[2].includes('
')); @@ -716,7 +713,6 @@ describe("KdbNewConnectionView", () => { serverName: "local", }; const result = view.renderMyQEditForm(); - console.log(JSON.stringify(result)); assert.ok(result.strings[0].includes('
')); assert.ok(result.strings[1].includes('
')); assert.ok(result.strings[2].includes('
')); @@ -741,7 +737,6 @@ describe("KdbNewConnectionView", () => { serverName: "local", }; const result = view.renderInsightsEditForm(); - console.log(JSON.stringify(result)); assert.ok(result.strings[0].includes('
')); assert.ok(result.strings[1].includes('
')); assert.ok(result.strings[2].includes('
'));