Skip to content

Commit bff041f

Browse files
committed
adopt new reference-view API
Signed-off-by: Shi Chen <[email protected]>
1 parent a8fee04 commit bff041f

11 files changed

+176
-34
lines changed

Diff for: package.json

+59-6
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,21 @@
965965
"category": "Java"
966966
},
967967
{
968-
"command": "java.action.changeBaseType",
969-
"title": "%java.action.changeBaseType%",
968+
"command": "java.action.showClassHierarchyFromReferenceView",
969+
"title": "%java.action.showClassHierarchy%",
970+
"icon": "$(type-hierarchy)",
971+
"category": "Java"
972+
},
973+
{
974+
"command": "java.action.showSupertypeHierarchy",
975+
"title": "%java.action.showSupertypeHierarchy%",
976+
"icon": "$(type-hierarchy-super)",
977+
"category": "Java"
978+
},
979+
{
980+
"command": "java.action.showSubtypeHierarchy",
981+
"title": "%java.action.showSubtypeHierarchy%",
982+
"icon": "$(type-hierarchy-sub)",
970983
"category": "Java"
971984
}
972985
],
@@ -1050,10 +1063,18 @@
10501063
},
10511064
{
10521065
"command": "java.action.showClassHierarchy",
1053-
"when": "javaLSReady && editorIsOpen"
1066+
"when": "javaLSReady && editorIsOpen && reference-list.source != typeHierarchy"
10541067
},
10551068
{
1056-
"command": "java.action.changeBaseType",
1069+
"command": "java.action.showClassHierarchyFromReferenceView",
1070+
"when": "javaLSReady && editorIsOpen && reference-list.source == typeHierarchy"
1071+
},
1072+
{
1073+
"command": "java.action.showSubtypeHierarchy",
1074+
"when": "false"
1075+
},
1076+
{
1077+
"command": "java.action.showSupertypeHierarchy",
10571078
"when": "false"
10581079
},
10591080
{
@@ -1077,11 +1098,43 @@
10771098
"when": "java:serverMode == LightWeight"
10781099
}
10791100
],
1101+
"view/title": [
1102+
{
1103+
"command": "java.action.showClassHierarchyFromReferenceView",
1104+
"group": "navigation@-1",
1105+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == typeHierarchy"
1106+
},
1107+
{
1108+
"command": "java.action.showSupertypeHierarchy",
1109+
"group": "navigation@0",
1110+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy"
1111+
},
1112+
{
1113+
"command": "java.action.showSubtypeHierarchy",
1114+
"group": "navigation@1",
1115+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy"
1116+
}
1117+
],
10801118
"view/item/context": [
10811119
{
1082-
"command": "java.action.changeBaseType",
1120+
"command": "java.action.showClassHierarchy",
1121+
"group": "1",
1122+
"when": "view == references-view.tree && viewItem == java-type-item"
1123+
},
1124+
{
1125+
"command": "java.action.showClassHierarchyFromReferenceView",
1126+
"group": "1",
1127+
"when": "view == references-view.tree && viewItem == type-item"
1128+
},
1129+
{
1130+
"command": "java.action.showSupertypeHierarchy",
1131+
"group": "1",
1132+
"when": "view == references-view.tree && viewItem == java-type-item"
1133+
},
1134+
{
1135+
"command": "java.action.showSubtypeHierarchy",
10831136
"group": "1",
1084-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && viewItem != 'false'"
1137+
"when": "view == references-view.tree && viewItem == java-type-item"
10851138
}
10861139
]
10871140
}

Diff for: package.nls.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"java.show.server.task.status": "Show Build Job Status",
1616
"java.action.navigateToSuperImplementation": "Go to Super Implementation",
1717
"java.action.showClassHierarchy": "Show Class Hierarchy",
18-
"java.action.changeBaseType": "Base on this Type"
19-
}
18+
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
19+
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy"
20+
}

Diff for: package.nls.zh.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"java.show.server.task.status": "显示工作状态",
1616
"java.action.navigateToSuperImplementation": "转到父类实现",
1717
"java.action.showClassHierarchy": "显示类的继承关系",
18-
"java.action.changeBaseType": "基于此类型"
19-
}
18+
"java.action.showSupertypeHierarchy": "显示父类层次结构",
19+
"java.action.showSubtypeHierarchy": "显示子类层次结构"
20+
}

Diff for: src/commands.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,21 @@ export namespace Commands {
193193
*/
194194
export const NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND = 'java.action.navigateToSuperImplementation';
195195
/**
196-
* Show Class Hierarchy of given Selection.
196+
* Show Supertype Hierarchy of given type.
197+
*/
198+
export const SHOW_SUPERTYPE_HIERARCHY = 'java.action.showSupertypeHierarchy';
199+
/**
200+
* Show Subtype Hierarchy of given type.
201+
*/
202+
export const SHOW_SUBTYPE_HIERARCHY = 'java.action.showSubtypeHierarchy';
203+
/**
204+
* Show Class Hierarchy of given type.
197205
*/
198206
export const SHOW_CLASS_HIERARCHY = 'java.action.showClassHierarchy';
199207
/**
200-
* Change the base type of Type Hierarchy.
208+
* Show Class Hierarchy of given type from reference view.
201209
*/
202-
export const CHANGE_BASE_TYPE = 'java.action.changeBaseType';
210+
export const SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW = 'java.action.showClassHierarchyFromReferenceView';
203211
/**
204212
* Show server task status
205213
*/

Diff for: src/standardLanguageClient.ts

+60-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { ExtensionContext, window, workspace, commands, Uri, ProgressLocation, ViewColumn, EventEmitter, extensions, Location, languages, CodeActionKind, TextEditor, CancellationToken, ConfigurationTarget, Range, Position } from "vscode";
3+
import { ExtensionContext, window, workspace, commands, Uri, ProgressLocation, ViewColumn, EventEmitter, extensions, Location, languages, CodeActionKind, TextEditor, CancellationToken, ConfigurationTarget, Range, Position, TypeHierarchyItem } from "vscode";
44
import { Commands } from "./commands";
55
import { serverStatus, ServerStatusKind } from "./serverStatus";
66
import { prepareExecutable, awaitServerConnection } from "./javaServerStarter";
@@ -35,7 +35,7 @@ import { findRuntimes, IJavaRuntime } from "jdk-utils";
3535
import { snippetCompletionProvider } from "./snippetCompletionProvider";
3636
import { JavaInlayHintsProvider } from "./inlayHintsProvider";
3737
import { TypeHierarchyFeature } from "vscode-languageclient/lib/common/proposed.typeHierarchy";
38-
import { CodeTypeHierarchyItem } from "./typeHierarchy/protocol";
38+
import { CodeTypeHierarchyItem, showSubtypeHierarchyReferenceViewCommand, showSupertypeHierarchyReferenceViewCommand, showTypeHierarchyReferenceViewCommand } from "./typeHierarchy/protocol";
3939

4040
const extensionName = 'Language Support for Java';
4141
const GRADLE_CHECKSUM = "gradle/checksum/prompt";
@@ -376,19 +376,68 @@ export class StandardLanguageClient {
376376
}
377377
}));
378378

379-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, (location: any) => {
380-
if (location instanceof Uri) {
381-
typeHierarchyTree.setTypeHierarchy(new Location(location, window.activeTextEditor.selection.active));
382-
} else {
383-
if (window.activeTextEditor?.document?.languageId !== "java") {
384-
return;
379+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, async (anchor: any) => {
380+
try {
381+
if (anchor instanceof Uri) { // comes from context menu
382+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor, window.activeTextEditor.selection.active));
383+
} else if (anchor instanceof TypeHierarchyItem) { // comes from class hierarchy view item
384+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.uri, anchor.range.start));
385+
} else { // comes from command palette
386+
if (window.activeTextEditor?.document?.languageId !== "java") {
387+
return;
388+
}
389+
await typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active));
390+
}
391+
} catch (e) {
392+
if (e?.message) {
393+
// show message in the selection when call from editor context menu
394+
if (anchor instanceof Uri) {
395+
showNoLocationFound(e.message);
396+
} else {
397+
window.showErrorMessage(e.message);
398+
}
385399
}
386-
typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active));
387400
}
388401
}));
389402

390-
context.subscriptions.push(commands.registerCommand(Commands.CHANGE_BASE_TYPE, async (item: CodeTypeHierarchyItem) => {
391-
typeHierarchyTree.changeBaseItem(item);
403+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW, async (anchor?: any) => {
404+
try {
405+
if (!anchor) { // comes from reference-view's title or command palette
406+
await typeHierarchyTree.setTypeHierarchyFromReferenceView();
407+
} else if (anchor.item instanceof TypeHierarchyItem) { // comes from reference-view's item
408+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.item.uri, anchor.item.range.start));
409+
}
410+
} catch (e) {
411+
if (e?.message) {
412+
window.showErrorMessage(e.message);
413+
}
414+
}
415+
}));
416+
417+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, async (anchor?: any) => {
418+
let location: Location;
419+
if (!anchor) {
420+
location = typeHierarchyTree.getAnchor();
421+
} else if (anchor instanceof TypeHierarchyItem) {
422+
location = new Location(anchor.uri, anchor.range.start);
423+
}
424+
if (location) {
425+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
426+
await commands.executeCommand(showSupertypeHierarchyReferenceViewCommand, location);
427+
}
428+
}));
429+
430+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, async (anchor?: any) => {
431+
let location: Location;
432+
if (!anchor) {
433+
location = typeHierarchyTree.getAnchor();
434+
} else if (anchor instanceof TypeHierarchyItem) {
435+
location = new Location(anchor.uri, anchor.range.start);
436+
}
437+
if (location) {
438+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
439+
await commands.executeCommand(showSubtypeHierarchyReferenceViewCommand, location);
440+
}
392441
}));
393442

394443
context.subscriptions.push(commands.registerCommand(Commands.COMPILE_WORKSPACE, (isFullCompile: boolean, token?: CancellationToken) => {

Diff for: src/typeHierarchy/model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class TypeHierarchyTreeDataProvider implements vscode.TreeDataProvider<CodeTypeH
9292
return undefined;
9393
}
9494
const treeItem: vscode.TreeItem = (element === this.model.getBaseItem()) ? new vscode.TreeItem({ label: element.name, highlights: [[0, element.name.length]] }) : new vscode.TreeItem(element.name);
95-
treeItem.contextValue = (element === this.model.getBaseItem() || !element.uri || element.kind === vscode.SymbolKind.Interface) ? "false" : "true";
95+
treeItem.contextValue = "java-type-item";
9696
treeItem.description = element.detail;
9797
treeItem.iconPath = TypeHierarchyTreeDataProvider.getThemeIcon(element.kind);
9898
treeItem.command = (element.uri) ? {

Diff for: src/typeHierarchy/protocol.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as vscode from "vscode";
22

3+
export const showTypeHierarchyReferenceViewCommand = "references-view.showTypeHierarchy";
4+
export const showSupertypeHierarchyReferenceViewCommand = "references-view.showSupertypes";
5+
export const showSubtypeHierarchyReferenceViewCommand = "references-view.showSubtypes";
6+
37
export class CodeTypeHierarchyItem extends vscode.TypeHierarchyItem {
48
children?: CodeTypeHierarchyItem[];
59
data: any;

Diff for: src/typeHierarchy/references-view.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export interface SymbolTree {
2727
* @param input A symbol tree input object
2828
*/
2929
setInput(input: SymbolTreeInput<unknown>): void;
30+
31+
/**
32+
* Get the contents of the references viewlet.
33+
*
34+
* @returns The current symbol tree input object
35+
*/
36+
getInput(): SymbolTreeInput<unknown> | undefined;
3037
}
3138

3239
/**

Diff for: src/typeHierarchy/typeHierarchyTree.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { getActiveLanguageClient } from "../extension";
55
import { showNoLocationFound } from "../standardLanguageClient";
66
import { TypeHierarchyTreeInput } from "./model";
77
import { CodeTypeHierarchyItem } from "./protocol";
8-
import { SymbolTree } from "./references-view";
8+
import { SymbolTree, SymbolTreeInput } from "./references-view";
99
import { ToCodeTypeHierarchyItem } from "./util";
1010

1111
export class TypeHierarchyTree {
1212
private api: SymbolTree;
1313
private client: LanguageClient;
1414
private cancelTokenSource: vscode.CancellationTokenSource;
15+
private anchor: vscode.Location;
1516
public initialized: boolean;
1617

1718
constructor() {
@@ -52,22 +53,38 @@ export class TypeHierarchyTree {
5253
return;
5354
}
5455
if (!protocolItems || protocolItems.length === 0) {
55-
showNoLocationFound('No Class Hierarchy found');
56-
return;
56+
throw new Error("No Class Hierarchy found");
5757
}
5858
const protocolItem = protocolItems[0];
5959
const symbolKind = this.client.protocol2CodeConverter.asSymbolKind(protocolItem.kind);
6060
if (symbolKind === vscode.SymbolKind.Interface) {
61-
showNoLocationFound('Class Hierarchy is not available for interfaces');
62-
return;
61+
throw new Error("Class Hierarchy is not available for interfaces");
6362
}
6463
const item: CodeTypeHierarchyItem = ToCodeTypeHierarchyItem(this.client, protocolItem);
6564
const input: TypeHierarchyTreeInput = new TypeHierarchyTreeInput(location, this.cancelTokenSource.token, item);
65+
this.anchor = input.location;
6666
this.api.setInput(input);
6767
}
6868

69-
public async changeBaseItem(item: CodeTypeHierarchyItem): Promise<void> {
70-
this.setTypeHierarchy(new vscode.Location(item.uri, item.range.start));
69+
public async setTypeHierarchyFromReferenceView(): Promise<void> {
70+
if (!this.initialized) {
71+
await this.initialize();
72+
}
73+
if (!this.api) {
74+
return;
75+
}
76+
const treeInput: SymbolTreeInput<unknown> = this.api.getInput();
77+
if (!treeInput) {
78+
return;
79+
}
80+
const location = treeInput.location;
81+
if (location) {
82+
await this.setTypeHierarchy(location);
83+
}
84+
}
85+
86+
public getAnchor(): vscode.Location | undefined {
87+
return this.anchor;
7188
}
7289

7390
private async isValidRequestPosition(uri: vscode.Uri, position: vscode.Position) {

Diff for: src/typeHierarchy/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CancellationToken, commands, SymbolKind } from "vscode";
1+
import { CancellationToken, SymbolKind } from "vscode";
22
import { LanguageClient, Proposed, TypeHierarchyItem } from "vscode-languageclient/node";
33
import { CodeTypeHierarchyItem } from "./protocol";
44

Diff for: test/standard-mode-suite/extension.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ suite('Java Language Extension - Standard', () => {
6666
Commands.SWITCH_SERVER_MODE,
6767
Commands.UPDATE_SOURCE_ATTACHMENT_CMD,
6868
Commands.RUNTIME_VALIDATION_OPEN,
69-
Commands.CHANGE_BASE_TYPE,
7069
Commands.SHOW_CLASS_HIERARCHY,
70+
Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW,
71+
Commands.SHOW_SUPERTYPE_HIERARCHY,
72+
Commands.SHOW_SUBTYPE_HIERARCHY,
7173
].sort();
7274
const foundJavaCommands = commands.filter((value) => {
7375
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)