From 235a2d2dbb1e96ee50f0b48ebeb5ac4d4d52a520 Mon Sep 17 00:00:00 2001 From: lxk955 <164191365+lxk955@users.noreply.github.com> Date: Sun, 4 Oct 2026 22:27:30 +0800 Subject: [PATCH] feat(sidebar): show comments for procedures and functions in object tree Refs: #9962 --- .../src/components/sidebar/TreeItem.vue | 4 +- .../TreeItem.routineComments.spec.ts | 151 ++++++++++++++++++ .../sidebar/sidebarTreeItemLayout.spec.ts | 50 +++++- .../src/lib/sidebar/sidebarTreeItemLayout.ts | 10 +- 4 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 apps/desktop/src/components/sidebar/__tests__/TreeItem.routineComments.spec.ts diff --git a/apps/desktop/src/components/sidebar/TreeItem.vue b/apps/desktop/src/components/sidebar/TreeItem.vue index 65baec70ff..cf56ec154a 100644 --- a/apps/desktop/src/components/sidebar/TreeItem.vue +++ b/apps/desktop/src/components/sidebar/TreeItem.vue @@ -56,7 +56,7 @@ import { Input } from "@/components/ui/input"; import { Switch } from "@/components/ui/switch"; import LightTooltip from "@/components/ui/LightTooltip.vue"; import type { ColumnInfo, ConnectionConfig, CustomTypeTreeMemberMeta, DatabaseType, TreeNode, TriggerInfo } from "@/types/database"; -import { alignedCommentLeadingWidth, canTreeNodePin, canTreeNodeShowExpander, sidebarTreeNodeComment, trailingCommentAvailableWidth, trailingCommentGapPx, treeItemPaddingLeft, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout"; +import { alignedCommentLeadingWidth, canTreeNodePin, canTreeNodeShowExpander, isSidebarCommentSupportedType, sidebarTreeNodeComment, trailingCommentAvailableWidth, trailingCommentGapPx, treeItemPaddingLeft, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout"; import { clearActiveTableReferencePayload, createColumnReferencePayload, @@ -639,7 +639,7 @@ const detailTooltip = computed(() => { } const column = node.type === "column" ? (node.meta as ColumnInfo | undefined) : undefined; const comment = column && "comment" in column ? column.comment : node.comment; - if ((!comment && !column) || (node.type !== "schema" && node.type !== "table" && node.type !== "view" && node.type !== "column")) return null; + if ((!comment && !column) || !isSidebarCommentSupportedType(node.type)) return null; const rows: DetailTooltipRow[] = [ { label: t("connection.name"), value: visibleLabel(node) }, ...(column ? [{ label: t("structureEditor.nullable"), value: t(column.is_nullable ? "structureEditor.nullable" : "structureEditor.notNull") }] : []), diff --git a/apps/desktop/src/components/sidebar/__tests__/TreeItem.routineComments.spec.ts b/apps/desktop/src/components/sidebar/__tests__/TreeItem.routineComments.spec.ts new file mode 100644 index 0000000000..3ee16cbf04 --- /dev/null +++ b/apps/desktop/src/components/sidebar/__tests__/TreeItem.routineComments.spec.ts @@ -0,0 +1,151 @@ +// @vitest-environment happy-dom + +import { createApp, defineComponent, h, nextTick, ref, type App } from "vue"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import i18n, { setLocale } from "@/i18n"; +import TreeItem from "@/components/sidebar/TreeItem.vue"; +import { createSidebarTreeRuntime, sidebarTreeRuntimeKey, type SidebarTreeRuntimeHost } from "@/lib/sidebar/sidebarTreeRuntime"; +import type { ConnectionConfig, TreeNode } from "@/types/database"; + +let commentMode = "comment-inline"; + +const connectionStore = { + activeConnectionId: "connection-1", + connectedIds: new Set(["connection-1"]), + connectingIds: new Set(), + connectionMultiSelectActive: false, + connections: [], + getConfig: (): ConnectionConfig => + ({ + id: "connection-1", + name: "Local SQL Server", + db_type: "sqlserver", + host: "127.0.0.1", + port: 1433, + username: "sa", + database: "master", + }) as ConnectionConfig, + getSidebarVisibleFilterSummary: () => null, + clearConnectionError: vi.fn(), + isDefaultDatabase: () => false, + isDefaultSchema: () => false, + isPinnedTreeNodeReorderTarget: () => false, + isTreeNodeChildrenLoaded: () => false, + isTreeNodePinned: () => false, + selectedTreeNodeId: null as string | null, + selectedTreeNodeIds: [] as string[], + selectedTreeNodeIdsSet: new Set(), + sidebarTableSearchQueries: {}, + tableNameFilterForScope: () => undefined, + treeNodes: [], + treeSelectionAnchorId: null as string | null, +}; + +vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => connectionStore })); +vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => ({ openDatabaseKeys: new Set() }) })); +vi.mock("@/stores/settingsStore", () => ({ + useSettingsStore: () => ({ + editorSettings: { + shortcuts: { openDataInNewTab: "" }, + sidebarActivation: "double", + sidebarAllowHorizontalScroll: false, + sidebarHiddenTablePrefixes: [], + sidebarObjectInfoMode: commentMode, + sidebarShowConnectionNotes: false, + sidebarShowTooltips: true, + }, + }), +})); +vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) })); + +const mountedApps: App[] = []; + +async function mountTreeItem(initialNode: TreeNode, commentLabelWidth?: number) { + const container = document.createElement("div"); + document.body.appendChild(container); + const node = ref(initialNode); + const host: SidebarTreeRuntimeHost = { + activeConnectionId: () => "connection-1", + connectionConfig: () => connectionStore.getConfig(), + isExpanded: () => false, + isLoading: () => false, + isSelected: () => false, + isFocused: () => false, + loadChildren: vi.fn(), + toggleNode: vi.fn(), + }; + const runtime = createSidebarTreeRuntime(); + runtime.bindHost(host); + const app = createApp( + defineComponent({ + setup: () => () => h(TreeItem, { node: node.value, depth: 3, commentLabelWidth }), + }), + ); + mountedApps.push(app); + app.use(i18n); + app.provide(sidebarTreeRuntimeKey, runtime); + app.mount(container); + await nextTick(); + return { container, node, host }; +} + +beforeEach(async () => { + commentMode = "comment-inline"; + await setLocale("en"); +}); + +afterEach(async () => { + for (const app of mountedApps.splice(0)) app.unmount(); + document.body.replaceChildren(); + await setLocale("en"); +}); + +describe("TreeItem routine and object comments (#9962)", () => { + it("renders inline comments for procedures and functions", async () => { + const procedureNode: TreeNode = { + id: "connection-1:master:dbo:usp_calculate_tax:PROCEDURE", + label: "usp_calculate_tax", + type: "procedure", + comment: "Calculates order taxes", + connectionId: "connection-1", + database: "master", + schema: "dbo", + }; + + const { container } = await mountTreeItem(procedureNode); + const commentEl = container.querySelector(".sidebar-object-comment"); + expect(commentEl).not.toBeNull(); + expect(commentEl?.textContent).toBe("Calculates order taxes"); + }); + + it("renders inline comments for functions", async () => { + const functionNode: TreeNode = { + id: "connection-1:master:dbo:fn_get_total:FUNCTION", + label: "fn_get_total", + type: "function", + comment: "Sums line items", + connectionId: "connection-1", + database: "master", + schema: "dbo", + }; + + const { container } = await mountTreeItem(functionNode); + const commentEl = container.querySelector(".sidebar-object-comment"); + expect(commentEl).not.toBeNull(); + expect(commentEl?.textContent).toBe("Sums line items"); + }); + + it("does not render comment element when procedure has no comment", async () => { + const procedureNode: TreeNode = { + id: "connection-1:master:dbo:usp_no_comment:PROCEDURE", + label: "usp_no_comment", + type: "procedure", + connectionId: "connection-1", + database: "master", + schema: "dbo", + }; + + const { container } = await mountTreeItem(procedureNode); + expect(container.querySelector(".sidebar-object-comment")).toBeNull(); + }); +}); diff --git a/apps/desktop/src/lib/__tests__/sidebar/sidebarTreeItemLayout.spec.ts b/apps/desktop/src/lib/__tests__/sidebar/sidebarTreeItemLayout.spec.ts index 6ee3b61436..cf122b5f60 100644 --- a/apps/desktop/src/lib/__tests__/sidebar/sidebarTreeItemLayout.spec.ts +++ b/apps/desktop/src/lib/__tests__/sidebar/sidebarTreeItemLayout.spec.ts @@ -1,5 +1,17 @@ import { describe, expect, it } from "vitest"; -import { alignedCommentLeadingWidth, alignedSidebarCommentLabelWidths, canTreeNodeShowExpander, sidebarTreeNaturalContentWidth, sidebarTreeNodeComment, trailingCommentAvailableWidth, treeItemPaddingLeft, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout"; +import { + alignedCommentLeadingWidth, + alignedSidebarCommentLabelWidths, + canTreeNodeShowExpander, + isSidebarCommentAlignableNode, + isSidebarCommentSupportedType, + sidebarTreeNaturalContentWidth, + sidebarTreeNodeComment, + trailingCommentAvailableWidth, + treeItemPaddingLeft, + treeLabelWidthClass, + usesFullWidthTreeLabel, +} from "@/lib/sidebar/sidebarTreeItemLayout"; import type { TreeNode } from "@/types/database"; describe("sidebar tree item layout", () => { @@ -106,4 +118,40 @@ describe("sidebar tree item layout", () => { expect(sidebarTreeNaturalContentWidth(items, measure, 16)).toBe(4 * 16 + 8 + 54 + "nested".length * 7 + 20); expect(sidebarTreeNaturalContentWidth(items, measure, 32)).toBe(4 * 32 + 8 + 54 + "nested".length * 7 + 20); }); + + it("supports comments and full-width labels for stored procedures and functions (#9962)", () => { + const procedure: TreeNode = { id: "p1", label: "calculate_tax", type: "procedure", comment: "Computes order tax" }; + const func: TreeNode = { id: "f1", label: "get_user_name", type: "function", comment: "Returns formatted user name" }; + const trigger: TreeNode = { id: "t1", label: "trg_audit", type: "trigger", comment: "Audit changes" }; + const seq: TreeNode = { id: "s1", label: "order_id_seq", type: "sequence", comment: "Order id sequence" }; + + expect(sidebarTreeNodeComment(procedure, false)).toBe("Computes order tax"); + expect(sidebarTreeNodeComment(func, false)).toBe("Returns formatted user name"); + expect(sidebarTreeNodeComment(trigger, false)).toBe("Audit changes"); + expect(sidebarTreeNodeComment(seq, false)).toBe("Order id sequence"); + + expect(isSidebarCommentSupportedType("procedure")).toBe(true); + expect(isSidebarCommentSupportedType("function")).toBe(true); + expect(isSidebarCommentSupportedType("materialized_view")).toBe(true); + expect(isSidebarCommentAlignableNode(procedure)).toBe(true); + expect(isSidebarCommentAlignableNode(func)).toBe(true); + + expect(usesFullWidthTreeLabel("procedure", true)).toBe(true); + expect(usesFullWidthTreeLabel("procedure", true, true)).toBe(false); + expect(usesFullWidthTreeLabel("function", true)).toBe(true); + expect(usesFullWidthTreeLabel("function", true, true)).toBe(false); + }); + + it("aligns routine comments among sibling procedures and functions", () => { + const widths = alignedSidebarCommentLabelWidths([ + { id: "group-procedures", depth: 1, alignable: false, hasComment: false, labelWidth: 0 }, + { id: "proc-short", depth: 2, alignable: true, hasComment: true, labelWidth: 50 }, + { id: "proc-long", depth: 2, alignable: true, hasComment: false, labelWidth: 120 }, + { id: "group-functions", depth: 1, alignable: false, hasComment: false, labelWidth: 0 }, + { id: "func-one", depth: 2, alignable: true, hasComment: true, labelWidth: 80 }, + ]); + + expect(widths.get("proc-short")).toBe(120); + expect(widths.get("func-one")).toBe(80); + }); }); diff --git a/apps/desktop/src/lib/sidebar/sidebarTreeItemLayout.ts b/apps/desktop/src/lib/sidebar/sidebarTreeItemLayout.ts index fefc7e8742..ed759a9e5c 100644 --- a/apps/desktop/src/lib/sidebar/sidebarTreeItemLayout.ts +++ b/apps/desktop/src/lib/sidebar/sidebarTreeItemLayout.ts @@ -43,7 +43,7 @@ const leafTypes: Set = new Set([ "datafile", ]); -const fullWidthLabelTypes: Set = new Set(["table", "view", "materialized_view", "mongo-collection", "mongo-bucket", "dynamodb-table", "vector-collection", "elasticsearch-index"]); +const fullWidthLabelTypes: Set = new Set(["table", "view", "materialized_view", "procedure", "function", "mongo-collection", "mongo-bucket", "dynamodb-table", "vector-collection", "elasticsearch-index"]); const emptyContainerTypes: Set = new Set(["saved-sql-root", "saved-sql-folder", "type"]); @@ -69,7 +69,7 @@ const pinnableTypes: Set = new Set([ "nacos-namespace", ]); -const commentTypes: Set = new Set(["connection", "schema", "table", "view", "materialized_view", "column", "mongo-collection", "dynamodb-table", "vector-collection", "elasticsearch-index"]); +const commentTypes: Set = new Set(["connection", "schema", "table", "view", "materialized_view", "column", "procedure", "function", "trigger", "sequence", "synonym", "package", "mongo-collection", "dynamodb-table", "vector-collection", "elasticsearch-index"]); export const SIDEBAR_INDENT_DEFAULT_PX = 16; @@ -149,8 +149,12 @@ export function sidebarTreeNodeComment(node: TreeNode, showConnectionNotes: bool return node.comment || null; } +export function isSidebarCommentSupportedType(type: TreeNodeType): boolean { + return commentTypes.has(type); +} + export function isSidebarCommentAlignableNode(node: TreeNode): boolean { - return commentTypes.has(node.type); + return isSidebarCommentSupportedType(node.type); } export function usesFullWidthTreeLabel(type: TreeNodeType, allowHorizontalScroll: boolean, hasTrailingComment = false): boolean {