Skip to content

Commit 819c31a

Browse files
committed
change type hierarchy implementation to LSP
Signed-off-by: Shi Chen <chenshi@microsoft.com>
1 parent 09e2c83 commit 819c31a

11 files changed

+211
-412
lines changed

package.json

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,34 +1199,27 @@
11991199
"category": "Java"
12001200
},
12011201
{
1202-
"command": "java.action.showTypeHierarchy",
1203-
"title": "%java.action.showTypeHierarchy%",
1202+
"command": "java.action.showClassHierarchy",
1203+
"title": "%java.action.showClassHierarchy%",
1204+
"icon": "$(type-hierarchy)",
12041205
"category": "Java"
12051206
},
12061207
{
1207-
"command": "java.action.showClassHierarchy",
1208+
"command": "java.action.showClassHierarchyFromReferenceView",
12081209
"title": "%java.action.showClassHierarchy%",
12091210
"icon": "$(type-hierarchy)",
1210-
"enablement": "typeHierarchyDirection != both && typeHierarchySymbolKind != 10",
12111211
"category": "Java"
12121212
},
12131213
{
12141214
"command": "java.action.showSupertypeHierarchy",
12151215
"title": "%java.action.showSupertypeHierarchy%",
12161216
"icon": "$(type-hierarchy-super)",
1217-
"enablement": "typeHierarchyDirection != parents",
12181217
"category": "Java"
12191218
},
12201219
{
12211220
"command": "java.action.showSubtypeHierarchy",
12221221
"title": "%java.action.showSubtypeHierarchy%",
12231222
"icon": "$(type-hierarchy-sub)",
1224-
"enablement": "typeHierarchyDirection != children",
1225-
"category": "Java"
1226-
},
1227-
{
1228-
"command": "java.action.changeBaseType",
1229-
"title": "%java.action.changeBaseType%",
12301223
"category": "Java"
12311224
},
12321225
{
@@ -1297,7 +1290,7 @@
12971290
"group": "navigation@90"
12981291
},
12991292
{
1300-
"command": "java.action.showTypeHierarchy",
1293+
"command": "java.action.showClassHierarchy",
13011294
"when": "javaLSReady && editorTextFocus && editorLangId == java",
13021295
"group": "0_navigation@3"
13031296
}
@@ -1336,12 +1329,12 @@
13361329
"when": "javaLSReady"
13371330
},
13381331
{
1339-
"command": "java.action.showTypeHierarchy",
1340-
"when": "javaLSReady && editorIsOpen"
1332+
"command": "java.action.showClassHierarchy",
1333+
"when": "javaLSReady && editorIsOpen && reference-list.source != typeHierarchy"
13411334
},
13421335
{
1343-
"command": "java.action.showClassHierarchy",
1344-
"when": "false"
1336+
"command": "java.action.showClassHierarchyFromReferenceView",
1337+
"when": "javaLSReady && editorIsOpen && reference-list.source == typeHierarchy"
13451338
},
13461339
{
13471340
"command": "java.action.showSubtypeHierarchy",
@@ -1351,10 +1344,6 @@
13511344
"command": "java.action.showSupertypeHierarchy",
13521345
"when": "false"
13531346
},
1354-
{
1355-
"command": "java.action.changeBaseType",
1356-
"when": "false"
1357-
},
13581347
{
13591348
"command": "java.project.updateSourceAttachment.command",
13601349
"when": "false"
@@ -1378,9 +1367,9 @@
13781367
],
13791368
"view/title": [
13801369
{
1381-
"command": "java.action.showClassHierarchy",
1370+
"command": "java.action.showClassHierarchyFromReferenceView",
13821371
"group": "navigation@-1",
1383-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && typeHierarchySymbolKind != 10"
1372+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == typeHierarchy"
13841373
},
13851374
{
13861375
"command": "java.action.showSupertypeHierarchy",
@@ -1395,9 +1384,24 @@
13951384
],
13961385
"view/item/context": [
13971386
{
1398-
"command": "java.action.changeBaseType",
1387+
"command": "java.action.showClassHierarchy",
1388+
"group": "1",
1389+
"when": "view == references-view.tree && viewItem == java-type-item"
1390+
},
1391+
{
1392+
"command": "java.action.showClassHierarchyFromReferenceView",
1393+
"group": "1",
1394+
"when": "view == references-view.tree && viewItem == type-item"
1395+
},
1396+
{
1397+
"command": "java.action.showSupertypeHierarchy",
1398+
"group": "1",
1399+
"when": "view == references-view.tree && viewItem == java-type-item"
1400+
},
1401+
{
1402+
"command": "java.action.showSubtypeHierarchy",
13991403
"group": "1",
1400-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && viewItem != 'false'"
1404+
"when": "view == references-view.tree && viewItem == java-type-item"
14011405
}
14021406
]
14031407
}

package.nls.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
"java.project.listSourcePaths": "List All Java Source Paths",
1818
"java.show.server.task.status": "Show Build Job Status",
1919
"java.action.navigateToSuperImplementation": "Go to Super Implementation",
20-
"java.action.showTypeHierarchy": "Show Type Hierarchy",
2120
"java.action.showClassHierarchy": "Show Class Hierarchy",
2221
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
2322
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
24-
"java.action.changeBaseType": "Base on this Type",
2523
"java.project.createModuleInfo.command": "Create module-info.java",
2624
"java.clean.sharedIndexes": "Clean Shared Indexes"
2725
}

package.nls.zh-cn.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
"java.project.listSourcePaths": "列出所有 Java 源代码路径",
1818
"java.show.server.task.status": "显示工作状态",
1919
"java.action.navigateToSuperImplementation": "转到父类实现",
20-
"java.action.showTypeHierarchy": "显示类型层次结构",
2120
"java.action.showClassHierarchy": "显示类的继承关系",
2221
"java.action.showSupertypeHierarchy": "显示父类层次结构",
2322
"java.action.showSubtypeHierarchy": "显示子类层次结构",
24-
"java.action.changeBaseType": "基于此类型",
2523
"java.project.createModuleInfo.command": "创建 module-info.java",
2624
"java.clean.sharedIndexes": "清理共享的索引文件"
2725
}

src/commands.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,33 +223,21 @@ export namespace Commands {
223223
*/
224224
export const NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND = 'java.action.navigateToSuperImplementation';
225225
/**
226-
* Open Type Hierarchy of given Selection.
227-
*/
228-
export const SHOW_TYPE_HIERARCHY = 'java.action.showTypeHierarchy';
229-
/**
230-
* Show SuperType Hierarchy of given Selection.
226+
* Show Supertype Hierarchy of given type.
231227
*/
232228
export const SHOW_SUPERTYPE_HIERARCHY = 'java.action.showSupertypeHierarchy';
233229
/**
234-
* Show SubType Hierarchy of given Selection.
230+
* Show Subtype Hierarchy of given type.
235231
*/
236232
export const SHOW_SUBTYPE_HIERARCHY = 'java.action.showSubtypeHierarchy';
237233
/**
238-
* Show Type Hierarchy of given Selection.
234+
* Show Class Hierarchy of given type.
239235
*/
240236
export const SHOW_CLASS_HIERARCHY = 'java.action.showClassHierarchy';
241237
/**
242-
* Change the base type of Type Hierarchy.
243-
*/
244-
export const CHANGE_BASE_TYPE = 'java.action.changeBaseType';
245-
/**
246-
* Open the given TypeHierarchy Item.
247-
*/
248-
export const OPEN_TYPE_HIERARCHY = 'java.navigate.openTypeHierarchy';
249-
/**
250-
* Resolve the given TypeHierarchy Item.
238+
* Show Class Hierarchy of given type from reference view.
251239
*/
252-
export const RESOLVE_TYPE_HIERARCHY = 'java.navigate.resolveTypeHierarchy';
240+
export const SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW = 'java.action.showClassHierarchyFromReferenceView';
253241
/**
254242
* Show server task status
255243
*/

src/standardLanguageClient.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as fse from 'fs-extra';
44
import { findRuntimes } from "jdk-utils";
55
import * as net from 'net';
66
import * as path from 'path';
7-
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace } from "vscode";
7+
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, TypeHierarchyItem, Uri, ViewColumn, window, workspace } from "vscode";
88
import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, TextDocumentPositionParams, WorkspaceEdit } from "vscode-languageclient";
9+
import { TypeHierarchyFeature } from 'vscode-languageclient/lib/common/typeHierarchy';
910
import { LanguageClient, StreamInfo } from "vscode-languageclient/node";
1011
import { apiManager } from "./apiManager";
1112
import * as buildPath from './buildpath';
@@ -34,7 +35,7 @@ import { snippetCompletionProvider } from "./snippetCompletionProvider";
3435
import * as sourceAction from './sourceAction';
3536
import { askForProjects, projectConfigurationUpdate, upgradeGradle } from "./standardLanguageClientUtils";
3637
import { TracingLanguageClient } from './TracingLanguageClient';
37-
import { TypeHierarchyDirection, TypeHierarchyItem } from "./typeHierarchy/protocol";
38+
import { CodeTypeHierarchyItem, showSubtypeHierarchyReferenceViewCommand, showSupertypeHierarchyReferenceViewCommand, showTypeHierarchyReferenceViewCommand } from "./typeHierarchy/protocol";
3839
import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree";
3940
import { getAllJavaProjects, getJavaConfig, getJavaConfiguration } from "./utils";
4041
import { Telemetry } from "./telemetry";
@@ -107,7 +108,7 @@ export class StandardLanguageClient {
107108

108109
// Create the language client and start the client.
109110
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions);
110-
111+
this.languageClient.registerFeature(new TypeHierarchyFeature(this.languageClient));
111112
this.registerCommandsForStandardServer(context, jdtEventEmitter);
112113
fileEventHandler.registerFileEventHandlers(this.languageClient, context);
113114

@@ -376,31 +377,68 @@ export class StandardLanguageClient {
376377
}
377378
}));
378379

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

390-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, () => {
391-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.both);
392-
}));
393-
394-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, () => {
395-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.parents);
404+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW, async (anchor?: any) => {
405+
try {
406+
if (!anchor) { // comes from reference-view's title or command palette
407+
await typeHierarchyTree.setTypeHierarchyFromReferenceView();
408+
} else if (anchor.item instanceof TypeHierarchyItem) { // comes from reference-view's item
409+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.item.uri, anchor.item.range.start));
410+
}
411+
} catch (e) {
412+
if (e?.message) {
413+
window.showErrorMessage(e.message);
414+
}
415+
}
396416
}));
397417

398-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, () => {
399-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.children);
418+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, async (anchor?: any) => {
419+
let location: Location;
420+
if (!anchor) {
421+
location = typeHierarchyTree.getAnchor();
422+
} else if (anchor instanceof TypeHierarchyItem) {
423+
location = new Location(anchor.uri, anchor.range.start);
424+
}
425+
if (location) {
426+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
427+
await commands.executeCommand(showSupertypeHierarchyReferenceViewCommand, location);
428+
}
400429
}));
401430

402-
context.subscriptions.push(commands.registerCommand(Commands.CHANGE_BASE_TYPE, async (item: TypeHierarchyItem) => {
403-
typeHierarchyTree.changeBaseItem(item);
431+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, async (anchor?: any) => {
432+
let location: Location;
433+
if (!anchor) {
434+
location = typeHierarchyTree.getAnchor();
435+
} else if (anchor instanceof TypeHierarchyItem) {
436+
location = new Location(anchor.uri, anchor.range.start);
437+
}
438+
if (location) {
439+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
440+
await commands.executeCommand(showSubtypeHierarchyReferenceViewCommand, location);
441+
}
404442
}));
405443

406444
context.subscriptions.push(commands.registerCommand(Commands.BUILD_PROJECT, async (uris: Uri[] | Uri, isFullBuild: boolean, token: CancellationToken) => {

0 commit comments

Comments
 (0)