Skip to content

Commit

Permalink
Merge pull request #388 from KxSystems/ee-label
Browse files Browse the repository at this point in the history
Connection view labels
  • Loading branch information
ecmel authored Aug 5, 2024
2 parents fe0a5bb + 03345a8 commit 1a4c1f6
Show file tree
Hide file tree
Showing 21 changed files with 342 additions and 16 deletions.
32 changes: 31 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@
"category": "KX",
"command": "kdb.toggleParameterCache",
"title": "KX: Toggle parameter cache"
},
{
"category": "KX",
"command": "kdb.renameLabel",
"title": "Rename label"
},
{
"category": "KX",
"command": "kdb.editLabelColor",
"title": "Edit label color"
},
{
"category": "KX",
"command": "kdb.deleteLabel",
"title": "Delete label"
}
],
"keybindings": [
Expand Down Expand Up @@ -692,7 +707,7 @@
},
{
"command": "kdb.editConnection",
"when": "view == kdb-servers",
"when": "view == kdb-servers && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)",
"group": "connection@4"
},
{
Expand Down Expand Up @@ -754,6 +769,21 @@
"command": "kdb.deleteFile",
"when": "(view == kdb-datasource-explorer || view == kdb-scratchpad-explorer) && viewItem == artifact",
"group": "kdbWorkspace@2"
},
{
"command": "kdb.renameLabel",
"when": "view == kdb-servers && viewItem == label",
"group": "label@1"
},
{
"command": "kdb.editLabelColor",
"when": "view == kdb-servers && viewItem == label",
"group": "label@2"
},
{
"command": "kdb.deleteLabel",
"when": "view == kdb-servers && viewItem == label",
"group": "label@3"
}
],
"editor/title/run": [
Expand Down
3 changes: 3 additions & 0 deletions resources/dark/labels/label-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/dark/labels/label-cyan.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/dark/labels/label-green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/dark/labels/label-magenta.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/dark/labels/label-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions resources/dark/labels/label-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/dark/labels/label-yellow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-cyan.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-magenta.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions resources/light/labels/label-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/light/labels/label-yellow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 29 additions & 9 deletions src/commands/workspaceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "vscode";
import { ext } from "../extensionVariables";
import { ConnectionManagementService } from "../services/connectionManagerService";
import { InsightsNode, KdbNode } from "../services/kdbTreeProvider";
import { InsightsNode, KdbNode, LabelNode } from "../services/kdbTreeProvider";
import { runQuery } from "./serverCommand";
import { ExecutionTypes } from "../models/execution";
import { importOldDsFiles, oldFilesExists } from "../utils/dataSource";
Expand Down Expand Up @@ -96,17 +96,37 @@ function getServers() {
}

/* istanbul ignore next */
export async function getConnectionForServer(server: string) {
export async function getConnectionForServer(
server: string,
): Promise<InsightsNode | KdbNode | undefined> {
if (server) {
const servers = await ext.serverProvider.getChildren();
return servers.find((item) => {
if (item instanceof InsightsNode) {
return item.details.alias === server;
} else if (item instanceof KdbNode) {
return item.details.serverAlias === server;
const nodes = await ext.serverProvider.getChildren();
const orphan = nodes.find((node) => {
if (node instanceof InsightsNode) {
return node.details.alias === server;
} else if (node instanceof KdbNode) {
return node.details.serverAlias === server;
}
return false;
}) as KdbNode | InsightsNode;
}) as InsightsNode | KdbNode;
if (orphan) {
return orphan;
}
const labels = nodes.filter((server) => server instanceof LabelNode);
for (const label of labels) {
const item = (label as LabelNode).children.find((node) => {
const name =
node instanceof InsightsNode
? node.details.alias
: node instanceof KdbNode
? node.details.serverAlias
: "";
return name === server;
}) as InsightsNode | KdbNode;
if (item) {
return item;
}
}
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ import { QuickFixProvider } from "./services/quickFixProvider";
import { connectClientCommands } from "./commands/clientCommands";
import {
createNewLabel,
deleteLabel,
getWorkspaceLabels,
getWorkspaceLabelsConnMap,
renameLabel,
setLabelColor,
} from "./utils/connLabel";

let client: LanguageClient;
Expand Down Expand Up @@ -506,6 +509,60 @@ export async function activate(context: ExtensionContext) {
ext.serverProvider.reload();
}
}),
commands.registerCommand("kdb.renameLabel", async (item) => {
if (item) {
const name = await window.showInputBox({
prompt: "Enter label name",
value: item.label,
});
if (name) {
renameLabel(item.label, name);
}
}
}),
commands.registerCommand("kdb.editLabelColor", async (item) => {
if (item) {
const colors = ext.labelColors.map((color) => ({
label: color.name,
iconPath: {
light: Uri.file(
path.join(
__filename,
"..",
"..",
"resources",
"light",
"labels",
`label-${color.name.toLowerCase()}.svg`,
),
),
dark: Uri.file(
path.join(
__filename,
"..",
"..",
"resources",
"dark",
"labels",
`label-${color.name.toLowerCase()}.svg`,
),
),
},
}));
const picked = await window.showQuickPick(colors, {
title: "Select label color",
placeHolder: item.source.color.name,
});
if (picked) {
setLabelColor(item.label, picked.label);
}
}
}),
commands.registerCommand("kdb.deleteLabel", (item) => {
if (item) {
deleteLabel(item.label);
}
}),
);

checkOldDatasourceFiles();
Expand Down
71 changes: 65 additions & 6 deletions src/services/kdbTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ import {
} from "../utils/core";
import { ConnectionManagementService } from "./connectionManagerService";
import { InsightsConnection } from "../classes/insightsConnection";
import {
getWorkspaceLabels,
getWorkspaceLabelsConnMap,
retrieveConnLabelsNames,
} from "../utils/connLabel";
import { Labels } from "../models/labels";

export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: EventEmitter<
Expand Down Expand Up @@ -92,15 +98,38 @@ export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
}

async getChildren(element?: TreeItem): Promise<TreeItem[]> {
if (!this.serverList) {
return Promise.resolve([]);
}
if (!this.insightsList) {
return Promise.resolve([]);
if (!this.serverList || !this.insightsList) {
return [];
}

if (!element) {
return Promise.resolve(this.getMergedElements(element));
getWorkspaceLabels();
getWorkspaceLabelsConnMap();

const orphans: TreeItem[] = [];
const nodes = ext.connLabelList.map((label) => new LabelNode(label));
const items = this.getMergedElements(element);

let orphan, found;
for (const item of items) {
orphan = true;
if (item instanceof KdbNode || item instanceof InsightsNode) {
const labels = retrieveConnLabelsNames(item);
for (const label of labels) {
found = nodes.find((node) => label === node.source.name);
if (found) {
found.children.push(item);
orphan = false;
}
}
}
if (orphan) {
orphans.push(item);
}
}
return [...orphans, ...nodes];
} else if (element instanceof LabelNode) {
return element.children;
} else if (
element.contextValue !== undefined &&
ext.kdbrootNodes.indexOf(element.contextValue) !== -1
Expand Down Expand Up @@ -785,3 +814,33 @@ export class QServerNode extends TreeItem {
};
contextValue = this.label;
}

export class LabelNode extends TreeItem {
readonly children: TreeItem[] = [];

constructor(public readonly source: Labels) {
super(source.name, TreeItemCollapsibleState.Collapsed);
this.contextValue = "label";
}

iconPath = {
light: path.join(
__filename,
"..",
"..",
"resources",
"light",
"labels",
`label-${this.source.color.name.toLowerCase()}.svg`,
),
dark: path.join(
__filename,
"..",
"..",
"resources",
"dark",
"labels",
`label-${this.source.color.name.toLowerCase()}.svg`,
),
};
}
46 changes: 46 additions & 0 deletions src/utils/connLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,49 @@ export function retrieveConnLabelsNames(
});
return labels;
}

export function renameLabel(name: string, newName: string) {
getWorkspaceLabels();
const found = ext.connLabelList.find((item) => item.name === name);
if (found) {
found.name = newName;
}
getWorkspaceLabelsConnMap();
const target = ext.labelConnMapList.find((item) => item.labelName === name);
if (target) {
target.labelName = newName;
}
workspace
.getConfiguration()
.update("kdb.labelsConnectionMap", ext.labelConnMapList, true)
.then(() =>
workspace
.getConfiguration()
.update("kdb.connectionLabels", ext.connLabelList, true),
);
}

export function setLabelColor(name: string, color: string) {
getWorkspaceLabels();
const found = ext.connLabelList.find((item) => item.name === name);
if (found) {
const target = ext.labelColors.find((value) => value.name === color);
if (target) {
found.color = target;
}
}
workspace
.getConfiguration()
.update("kdb.connectionLabels", ext.connLabelList, true);
}

export function deleteLabel(name: string) {
getWorkspaceLabels();
const found = ext.connLabelList.find((item) => item.name === name);
if (found) {
ext.connLabelList.splice(ext.connLabelList.indexOf(found), 1);
}
workspace
.getConfiguration()
.update("kdb.connectionLabels", ext.connLabelList, true);
}
Loading

0 comments on commit 1a4c1f6

Please sign in to comment.