feat(xugu): expose package members in schema tree

This commit is contained in:
Elias authored and GitHub committed 2026-08-10 17:05:54 +08:00
1 parent 37f13018d3
commit ad9075e18f
15 files changed
+922 -28

No files matched your search

@@ -27,6 +27,13 @@ describe("sidebar tree item layout", () => {
expect(canTreeNodeShowExpander({ type: "consul-overview", childCount: 0 })).toBe(false);
});
it("shows an expander only for package nodes explicitly marked as containers", () => {
expect(canTreeNodeShowExpander({ type: "package", childCount: 0 })).toBe(false);
expect(canTreeNodeShowExpander({ type: "package", childCount: 0, explicitContainer: true })).toBe(true);
expect(canTreeNodeShowExpander({ type: "package-body", childCount: 0, explicitContainer: true })).toBe(false);
expect(canTreeNodeShowExpander({ type: "procedure", childCount: 0, explicitContainer: true })).toBe(false);
});
it("aligns comments to the longest sibling name without crossing parent groups", () => {
const widths = alignedSidebarCommentLabelWidths([
{ id: "tables", depth: 1, alignable: false, hasComment: false, labelWidth: 0 },
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { isDirectNavigationTreeNode, objectSourceKindForTreeNode, shouldActivateTreeNodeOnSingleClick, treeNodeRowAction } from "@/lib/sidebar/treeNodeClick";
import { isDirectNavigationTreeNode, objectSourceKindForTreeNode, objectSourceTargetForTreeNode, shouldActivateTreeNodeOnSingleClick, treeNodeRowAction } from "@/lib/sidebar/treeNodeClick";
describe("treeNodeClick", () => {
it("opens synonym nodes as synonym source", () => {
@@ -17,4 +17,48 @@ describe("treeNodeClick", () => {
expect(treeNodeRowAction("consul-root", false, "double")).toBe("toggle");
expect(treeNodeRowAction("consul-overview", false, "double")).toBe("toggle");
});
it("expands package containers while preserving source behavior for leaf packages", () => {
expect(treeNodeRowAction("package", true)).toBe("toggle");
expect(treeNodeRowAction("package", false)).toBe("open-source");
});
it("routes package members to their owning package body", () => {
expect(
objectSourceTargetForTreeNode({
id: "pkg:member",
label: "calculate(p_value IN INT)",
type: "function",
objectName: "calculate",
parentName: "business_api",
parentSchema: "app_schema",
parentType: "package",
schema: "ignored_schema",
signature: "p_value IN INT",
}),
).toEqual({
name: "business_api",
schema: "app_schema",
objectType: "PACKAGE",
signature: "p_value IN INT",
});
});
it("keeps standalone routine source routing unchanged", () => {
expect(objectSourceTargetForTreeNode({ id: "proc", label: "standalone_proc", type: "procedure", schema: "app" })).toEqual({
name: "standalone_proc",
schema: "app",
objectType: "PROCEDURE",
signature: undefined,
});
});
it("does not reinterpret unrelated parent metadata as a package member", () => {
expect(objectSourceTargetForTreeNode({ id: "routine", label: "child_routine", type: "function", schema: "app", parentName: "other_parent" })).toEqual({
name: "child_routine",
schema: "app",
objectType: "FUNCTION",
signature: undefined,
});
});
});
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import type { CompletionAssistantCandidate, TreeNode } from "@/types/database";
import { buildXuguPackageMemberNodes, markXuguPackageNodesExpandable } from "@/lib/sidebar/xuguPackageMembers";
function packageNode(): TreeNode {
return {
id: "conn:db:schema:package:business_api",
label: "business_api",
type: "package",
objectName: "business_api",
connectionId: "conn",
database: "db",
schema: "app_schema",
valid: false,
};
}
describe("Xugu package member tree", () => {
it("marks package specifications as expandable without changing other objects", () => {
const nodes: TreeNode[] = [packageNode(), { id: "body", label: "business_api", type: "package-body" }, { id: "proc", label: "standalone", type: "procedure" }];
const result = markXuguPackageNodesExpandable(nodes);
expect(result[0]?.children).toEqual([]);
expect(result[1]?.children).toBeUndefined();
expect(result[2]?.children).toBeUndefined();
});
it("builds public procedure and function children with overload identities", () => {
const candidates: CompletionAssistantCandidate[] = [
{ name: "calculate", kind: "procedure", signature: "p_value IN INT" },
{ name: "calculate", kind: "procedure", signature: "p_value IN VARCHAR" },
{ name: "lookup", kind: "function", signature: null, data_type: "VARCHAR" },
{ name: "ignored_table", kind: "table" },
];
const result = buildXuguPackageMemberNodes(packageNode(), candidates);
expect(result.map((node) => [node.type, node.label])).toEqual([
["procedure", "calculate(p_value IN INT)"],
["procedure", "calculate(p_value IN VARCHAR)"],
["function", "lookup"],
]);
expect(result.every((node) => node.parentName === "business_api" && node.parentSchema === "app_schema" && node.parentType === "package" && node.valid === false)).toBe(true);
expect(new Set(result.map((node) => node.id)).size).toBe(3);
});
it("deduplicates exact duplicate metadata without merging case-distinct members", () => {
const candidates: CompletionAssistantCandidate[] = [
{ name: "MixedCase", kind: "function", signature: "p INT" },
{ name: "MixedCase", kind: "function", signature: "p INT" },
{ name: "MIXEDCASE", kind: "function", signature: "p INT" },
{ name: "", kind: "procedure" },
];
const result = buildXuguPackageMemberNodes(packageNode(), candidates);
expect(result.map((node) => node.objectName)).toEqual(["MixedCase", "MIXEDCASE"]);
});
});
@@ -155,8 +155,8 @@ export function canTreeNodeExpand(type: TreeNodeType): boolean {
return !leafTypes.has(type);
}
export function canTreeNodeShowExpander({ type, childCount }: { type: TreeNodeType; childCount?: number }): boolean {
if (!canTreeNodeExpand(type)) return false;
export function canTreeNodeShowExpander({ type, childCount, explicitContainer = false }: { type: TreeNodeType; childCount?: number; explicitContainer?: boolean }): boolean {
if (!canTreeNodeExpand(type) && !(type === "package" && explicitContainer)) return false;
if (childCount === 0 && emptyContainerTypes.has(type)) return false;
return true;
}
@@ -58,6 +58,32 @@ export function objectSourceKindForTreeNode(type: TreeNodeType): ObjectSourceKin
return null;
}
export interface TreeNodeObjectSourceTarget {
name: string;
schema?: string;
objectType: ObjectSourceKind;
signature?: string;
}
export function objectSourceTargetForTreeNode(node: TreeNode): TreeNodeObjectSourceTarget | null {
if ((node.type === "procedure" || node.type === "function") && node.parentType === "package" && node.parentName) {
return {
name: node.parentName,
schema: node.parentSchema || node.schema,
objectType: "PACKAGE",
signature: node.signature,
};
}
const objectType = objectSourceKindForTreeNode(node.type);
if (!objectType) return null;
return {
name: node.objectName || node.label,
schema: node.schema,
objectType,
signature: node.signature,
};
}
export function isDocumentBrowserTreeNode(type: TreeNodeType): boolean {
return documentBrowserNodeTypes.has(type);
}
@@ -66,6 +92,7 @@ export function treeNodeRowAction(type: TreeNodeType, canExpand: boolean, activa
if (!shouldActivateTreeNodeOnSingleClick(type, activation)) return "none";
if (type === "extension") return "open-extension-details";
if (dataNodeTypes.has(type)) return "open-data";
if (type === "package" && canExpand) return "toggle";
if (sourceNodeTypes.has(type)) return "open-source";
if (toggleLeafNodeTypes.has(type)) return "toggle";
if (canExpand) return "toggle";
@@ -0,0 +1,40 @@
import type { CompletionAssistantCandidate, TreeNode } from "@/types/database";
export function markXuguPackageNodesExpandable(nodes: TreeNode[]): TreeNode[] {
return nodes.map((node) => (node.type === "package" ? { ...node, children: node.children ?? [] } : node));
}
export function buildXuguPackageMemberNodes(packageNode: TreeNode, candidates: readonly CompletionAssistantCandidate[]): TreeNode[] {
const parentName = packageNode.objectName || packageNode.label;
const parentSchema = packageNode.schema;
const seen = new Set<string>();
const children: TreeNode[] = [];
for (const candidate of candidates) {
if (candidate.kind !== "procedure" && candidate.kind !== "function") continue;
const name = candidate.name.trim();
if (!name) continue;
const signature = candidate.signature?.trim() || "";
const key = `${candidate.kind}\0${name}\0${signature}`;
if (seen.has(key)) continue;
seen.add(key);
children.push({
id: `${packageNode.id}:member:${candidate.kind}:${name}:${signature}`,
label: signature ? `${name}(${signature})` : name,
type: candidate.kind,
objectName: name,
signature: signature || undefined,
parentName,
parentSchema,
parentType: "package",
valid: packageNode.valid,
connectionId: packageNode.connectionId,
database: packageNode.database,
schema: parentSchema,
isExpanded: false,
children: undefined,
});
}
return children;
}