Skip to content

Commit 782c619

Browse files
Added tooltip (#808)
1 parent 5ab8da9 commit 782c619

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/views/component-viewer/component-viewer-tree-view.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,23 @@ export class ComponentViewerTreeDataProvider implements vscode.TreeDataProvider<
5050
/**
5151
* Called by VS Code to lazily populate tooltip details for tree items.
5252
*/
53-
public resolveTreeItem(treeItem: vscode.TreeItem, element: ScvdGuiInterface): vscode.TreeItem {
53+
public resolveTreeItem(item: vscode.TreeItem, element: ScvdGuiInterface): vscode.ProviderResult<vscode.TreeItem> {
5454
const perfStartTime = perf?.startUi() ?? 0;
55-
treeItem.tooltip = element.getGuiLineInfo() ?? '';
55+
const guiName = element.getGuiName();
56+
const guiValue = element.getGuiValue();
57+
if (guiName && guiValue) {
58+
const tooltip = new vscode.MarkdownString(`**${guiName}** \n${guiValue}`);
59+
tooltip.supportHtml = true;
60+
item.tooltip = tooltip;
61+
} else if (guiName) {
62+
item.tooltip = new vscode.MarkdownString(`**${guiName}**`);
63+
} else if(guiValue) {
64+
item.tooltip = guiValue;
65+
} else {
66+
item.tooltip = undefined;
67+
}
5668
perf?.endUi(perfStartTime, 'treeViewResolveItemMs', 'treeViewResolveItemCalls');
57-
return treeItem;
69+
return item;
5870
}
5971

6072
public getChildren(element?: ScvdGuiInterface): ScvdGuiInterface[] {

src/views/component-viewer/test/unit/component-viewer-tree-view.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Unit test for ComponentViewerTreeDataProvider.
2020
*/
2121

22+
import * as vscode from 'vscode';
2223
import { ComponentViewerTreeDataProvider } from '../../component-viewer-tree-view';
2324
import type { ScvdGuiInterface } from '../../model/scvd-gui-interface';
2425

@@ -30,10 +31,20 @@ jest.mock('vscode', () => {
3031
public event = jest.fn();
3132
}
3233

34+
class MarkdownString {
35+
public value: string;
36+
public supportHtml = false;
37+
38+
constructor(value: string) {
39+
this.value = value;
40+
}
41+
}
42+
3343
class TreeItem {
3444
public label: string;
3545
public collapsibleState: number | undefined;
3646
public description: string | undefined;
47+
public tooltip: string | MarkdownString | undefined;
3748
public id: string | undefined;
3849
public contextValue: string | undefined;
3950

@@ -44,6 +55,7 @@ jest.mock('vscode', () => {
4455

4556
return {
4657
EventEmitter,
58+
MarkdownString,
4759
TreeItem,
4860
TreeItemCollapsibleState: {
4961
Collapsed: 1,
@@ -104,12 +116,38 @@ describe('ComponentViewerTreeDataProvider', () => {
104116
expect(treeItemWithChildren.collapsibleState).toBe(1);
105117
expect(treeItemWithChildren.description).toBe('Value');
106118
expect(treeItemWithChildren.id).toBe('id-1');
119+
const resolvedWith = provider.resolveTreeItem(treeItemWithChildren, withChildren) as vscode.TreeItem;
120+
expect(resolvedWith.tooltip).toBeInstanceOf(vscode.MarkdownString);
121+
const tooltipWith = resolvedWith.tooltip as { value: string; supportHtml: boolean };
122+
expect(tooltipWith.value).toBe('**Node** \nValue');
123+
expect(tooltipWith.supportHtml).toBe(true);
107124

108125
const treeItemWithout = provider.getTreeItem(withoutChildren);
109126
expect(treeItemWithout.label).toBe('UNKNOWN');
110127
expect(treeItemWithout.collapsibleState).toBe(0);
111128
expect(treeItemWithout.description).toBe('');
112129
expect(treeItemWithout.id).toBeUndefined();
130+
const resolvedWithout = provider.resolveTreeItem(treeItemWithout, withoutChildren) as vscode.TreeItem;
131+
expect(resolvedWithout.tooltip).toBeUndefined();
132+
});
133+
134+
it('formats tooltip for name-only, value-only, and empty values', () => {
135+
const provider = new ComponentViewerTreeDataProvider();
136+
const nameOnly = makeGui({ getGuiValue: () => undefined });
137+
const valueOnly = makeGui({ getGuiName: () => undefined });
138+
const emptyBoth = makeGui({ getGuiName: () => undefined, getGuiValue: () => undefined });
139+
140+
const nameItem = provider.resolveTreeItem(new vscode.TreeItem('Label'), nameOnly) as vscode.TreeItem;
141+
expect(nameItem.tooltip).toBeInstanceOf(vscode.MarkdownString);
142+
const nameTooltip = nameItem.tooltip as { value: string; supportHtml: boolean };
143+
expect(nameTooltip.value).toBe('**Node**');
144+
expect(nameTooltip.supportHtml).toBe(false);
145+
146+
const valueItem = provider.resolveTreeItem(new vscode.TreeItem('Label'), valueOnly) as vscode.TreeItem;
147+
expect(valueItem.tooltip).toBe('Value');
148+
149+
const emptyItem = provider.resolveTreeItem(new vscode.TreeItem('Label'), emptyBoth) as vscode.TreeItem;
150+
expect(emptyItem.tooltip).toBeUndefined();
113151
});
114152

115153
it('assigns locked context values for root and child nodes', () => {

0 commit comments

Comments
 (0)