diff --git a/package.json b/package.json index 8872bd57..70ce5eea 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "onCommand:kdb.newConnection.createNewBundledConnection", "onCommand:kdb.removeConnection", "onCommand:kdb.connect", + "onCommand:kdb.active.connection", "onCommand:kdb.disconnect", "onCommand:kdb.addAuthentication", "onCommand:kdb.enableTLS", @@ -219,6 +220,10 @@ "command": "kdb.connect", "title": "Connect server" }, + { + "command": "kdb.active.connection", + "title": "Active connection" + }, { "command": "kdb.addAuthentication", "title": "Add Authentication", @@ -470,6 +475,10 @@ "command": "kdb.connect", "when": "false" }, + { + "command": "kdb.active.connection", + "when": "false" + }, { "command": "kdb.disconnect", "when": "false" @@ -532,6 +541,11 @@ "when": "view == kdb-servers && viewItem not in kdb.connected && viewItem in kdb.rootNodes", "group": "connection@1" }, + { + "command": "kdb.active.connection", + "when": "view == kdb-servers && viewItem in kdb.connected && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)", + "group": "connection@1" + }, { "command": "kdb.addAuthentication", "when": "view == kdb-servers && viewItem not in kdb.insightsNodes && viewItem in kdb.kdbNodesWithoutAuth", diff --git a/src/commands/serverCommand.ts b/src/commands/serverCommand.ts index 9de28f7c..3742527e 100644 --- a/src/commands/serverCommand.ts +++ b/src/commands/serverCommand.ts @@ -687,6 +687,13 @@ export async function connect(viewItem: KdbNode): Promise { ext.serverProvider.reload(); } +export function activeConnection(viewItem: KdbNode | InsightsNode): void { + const connMngService = new ConnectionManagementService(); + connMngService.setActiveConnection(viewItem); + refreshDataSourcesPanel(); + ext.serverProvider.reload(); +} + export async function disconnect(connLabel?: string): Promise { const insightsNode = ext.kdbinsightsNodes.find((n) => ext.connectionNode instanceof InsightsNode diff --git a/src/extension.ts b/src/extension.ts index aea38894..8dd639aa 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -50,6 +50,7 @@ import { stopLocalProcessByServerName, } from "./commands/installTools"; import { + activeConnection, addInsightsConnection, addKdbConnection, addNewConnection, @@ -164,6 +165,12 @@ export async function activate(context: ExtensionContext) { commands.registerCommand("kdb.connect", async (viewItem: KdbNode) => { await connect(viewItem); }), + commands.registerCommand( + "kdb.active.connection", + async (viewItem: KdbNode) => { + activeConnection(viewItem); + }, + ), commands.registerCommand("kdb.enableTLS", async (viewItem: KdbNode) => { await enableTLS(viewItem.children[0]); diff --git a/src/extensionVariables.ts b/src/extensionVariables.ts index 3049b1a5..32c07283 100644 --- a/src/extensionVariables.ts +++ b/src/extensionVariables.ts @@ -47,6 +47,7 @@ export namespace ext { export let connection: LocalConnection | undefined; export let activeConnection: LocalConnection | undefined; export const connectedConnectionList: Array = []; + export const connectedContextStrings: Array = []; export const connectionsList: Array = []; export let hideDetailedConsoleQueryOutput: boolean; export let connectionNode: KdbNode | InsightsNode | undefined; diff --git a/src/services/connectionManagerService.ts b/src/services/connectionManagerService.ts index c94b485c..3542b264 100644 --- a/src/services/connectionManagerService.ts +++ b/src/services/connectionManagerService.ts @@ -46,6 +46,13 @@ export class ConnectionManagementService { return connection.details.serverName + ":" + connection.details.serverPort; } + public removeConnectionFromContextString(connLabel: string): void { + const index = ext.connectedContextStrings.indexOf(connLabel); + if (index > -1) { + ext.connectedContextStrings.splice(index, 1); + } + } + public async connect(connLabel: string): Promise { // check if connection exists const connection = this.retrieveConnection(connLabel); @@ -64,7 +71,7 @@ export class ConnectionManagementService { authCredentials ? authCredentials.split(":") : undefined, connection.details.tls, ); - await localConnection.connect((err, conn) => { + localConnection.connect((err, conn) => { if (err) { window.showErrorMessage(err.message); return; @@ -75,31 +82,35 @@ export class ConnectionManagementService { `Connection established successfully to: ${connLabel}`, ); - commands.executeCommand("setContext", "kdb.connected", [ - `${connLabel}` + " (connected)", - ]); + ext.connectedContextStrings.push(connLabel); + + commands.executeCommand( + "setContext", + "kdb.connected", + ext.connectedContextStrings, + ); ext.connectionNode = connection; Telemetry.sendEvent("Connection.Connected.QProcess"); ext.serverProvider.reload(); - this.setActiveConnection(connLabel); + this.setActiveConnection(connection); + ext.connectedConnectionList.push(localConnection); + ext.activeConnection = localConnection; } }); - ext.connectedConnectionList.push(localConnection); - ext.activeConnection = localConnection; } else { // work with Insights nodes } } - public setActiveConnection(connLabel: string): void { - const connection = this.retrieveConnectedConnection(connLabel); + public setActiveConnection(node: KdbNode | InsightsNode): void { + const connection = this.retrieveConnectedConnection(node.label); if (!connection) { return; } commands.executeCommand("setContext", "kdb.connected.active", [ - `${connLabel}` + " (active)", + `${node.label}`, ]); Telemetry.sendEvent("Connection.Connected.Active"); ext.activeConnection = connection; @@ -108,11 +119,12 @@ export class ConnectionManagementService { public disconnect(connLabel: string): void { const connection = this.retrieveConnectedConnection(connLabel); + const connectionNode = this.retrieveConnection(connLabel); if (!connection) { return; } const isLocal = this.isLocalConnection(); - if (isLocal) { + if (isLocal && connectionNode) { connection.getConnection()?.close(() => { ext.connectedConnectionList.splice( ext.connectedConnectionList.indexOf(connection), @@ -120,7 +132,12 @@ export class ConnectionManagementService { ); ext.activeConnection = undefined; ext.connectionNode = undefined; - commands.executeCommand("setContext", "kdb.connected", false); + this.removeConnectionFromContext(connectionNode); + commands.executeCommand( + "setContext", + "kdb.connected", + ext.connectedContextStrings, + ); commands.executeCommand("setContext", "kdb.connected.active", false); Telemetry.sendEvent("Connection.Disconnected"); ext.outputChannel.appendLine(