Skip to content

Commit e840aee

Browse files
robstrykerclaude
andcommitted
Restore Class Hierarchy view polish lost in the LSP protocol rewrite
The LSP-based rewrite simplified TypeHierarchyTreeDataProvider down to always resolving children eagerly before deciding collapsible state, and dropped three refinements the pre-rewrite custom-protocol version had: - java.typeHierarchy.lazyLoad: the setting existed in package.json but nothing read it anymore, so toggling it had no effect. - The java.lang.Object subtype placeholder: expanding Object with no placeholder would otherwise invoke typeHierarchy/subtypes for every class in the workspace. - The shared getThemeIcon helper in themeUtils.ts, duplicated here as a private static copy instead of reused. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 10a0d00 commit e840aee

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

‎src/typeHierarchy/model.ts‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { SymbolItemNavigation, SymbolTreeInput, SymbolTreeModel } from "./refere
44
import { getActiveLanguageClient } from "../extension";
55
import { LanguageClient, Proposed, TypeHierarchySubtypesParams, TypeHierarchySubtypesRequest } from "vscode-languageclient/node";
66
import { getRootItem, toCodeTypeHierarchyItem } from "./util";
7-
import { CancellationToken, commands } from "vscode";
7+
import { CancellationToken, commands, workspace } from "vscode";
8+
import { getThemeIcon } from "../themeUtils";
89

910
export class TypeHierarchyTreeInput implements SymbolTreeInput<CodeTypeHierarchyItem> {
1011
readonly contextValue: string = "javaTypeHierarchy";
@@ -81,10 +82,12 @@ export class TypeHierarchyModel implements SymbolItemNavigation<CodeTypeHierarch
8182
class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeHierarchyItem> {
8283
private readonly emitter = new vscode.EventEmitter<CodeTypeHierarchyItem | undefined>();
8384
private readonly modelListener: vscode.Disposable;
85+
private readonly lazyLoad: boolean;
8486
public readonly onDidChangeTreeData: vscode.Event<CodeTypeHierarchyItem> = this.emitter.event;
8587

8688
constructor(readonly model: TypeHierarchyModel, readonly client: LanguageClient, readonly token: CancellationToken) {
8789
this.modelListener = model.onDidChangeEvent(e => this.emitter.fire(e instanceof CodeTypeHierarchyItem ? e : undefined));
90+
this.lazyLoad = workspace.getConfiguration().get("java.typeHierarchy.lazyLoad");
8891
}
8992

9093
dispose(): void {
@@ -99,7 +102,7 @@ class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeH
99102
const treeItem: vscode.TreeItem = (element === this.model.getBaseItem()) ? new vscode.TreeItem({ label: element.name, highlights: [[0, element.name.length]] }) : new vscode.TreeItem(element.name);
100103
treeItem.contextValue = "java-type-item";
101104
treeItem.description = element.detail;
102-
treeItem.iconPath = TypeHierarchyTreeDataProvider.getThemeIcon(element.kind);
105+
treeItem.iconPath = getThemeIcon(element.kind);
103106
treeItem.command = (element.uri) ? {
104107
command: 'vscode.open',
105108
title: 'Open Type Definition Location',
@@ -110,9 +113,15 @@ class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeH
110113
// workaround: set a specific id to refresh the collapsible state for treeItems, see: https://github.com/microsoft/vscode/issues/114614#issuecomment-763428052
111114
treeItem.id = `${element.data["element"]}${Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)}`;
112115
if (element.children) {
113-
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
114-
} else {
116+
treeItem.collapsibleState = (element.children.length === 0) ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Expanded;
117+
} else if (this.lazyLoad) {
118+
// Don't resolve children up front: accept that leaf nodes will show a disclosure
119+
// arrow that collapses to nothing once expanded, in exchange for not issuing a
120+
// subtypes request for every visible node just to render the tree.
115121
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
122+
} else {
123+
element.children = await this.resolveChildren(element);
124+
treeItem.collapsibleState = (element.children.length === 0) ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed;
116125
}
117126
return treeItem;
118127
}
@@ -124,6 +133,24 @@ class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeH
124133
if (element.children) {
125134
return element.children;
126135
}
136+
return this.resolveChildren(element);
137+
}
138+
139+
private async resolveChildren(element: CodeTypeHierarchyItem): Promise<CodeTypeHierarchyItem[]> {
140+
if (element.name === "Object" && element.detail === "java.lang") {
141+
// Avoid fetching every class in the workspace: java.lang.Object's subtypes are
142+
// rarely useful to enumerate and can be prohibitively expensive to resolve.
143+
return [{
144+
name: "All classes are subtypes of java.lang.Object.",
145+
kind: undefined,
146+
children: [],
147+
detail: undefined,
148+
uri: undefined,
149+
range: undefined,
150+
selectionRange: undefined,
151+
data: undefined,
152+
} as CodeTypeHierarchyItem];
153+
}
127154
const params: TypeHierarchySubtypesParams = {
128155
item: this.client.code2ProtocolConverter.asTypeHierarchyItem(element),
129156
};
@@ -134,17 +161,4 @@ class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeH
134161
}
135162
return childrenCodeItems;
136163
}
137-
138-
private static themeIconIds = [
139-
'symbol-file', 'symbol-module', 'symbol-namespace', 'symbol-package', 'symbol-class', 'symbol-method',
140-
'symbol-property', 'symbol-field', 'symbol-constructor', 'symbol-enum', 'symbol-interface',
141-
'symbol-function', 'symbol-variable', 'symbol-constant', 'symbol-string', 'symbol-number', 'symbol-boolean',
142-
'symbol-array', 'symbol-object', 'symbol-key', 'symbol-null', 'symbol-enum-member', 'symbol-struct',
143-
'symbol-event', 'symbol-operator', 'symbol-type-parameter'
144-
];
145-
146-
private static getThemeIcon(kind: vscode.SymbolKind): vscode.ThemeIcon | undefined {
147-
const id = TypeHierarchyTreeDataProvider.themeIconIds[kind];
148-
return id ? new vscode.ThemeIcon(id) : undefined;
149-
}
150164
}

0 commit comments

Comments
 (0)