Skip to content

Commit c393ba1

Browse files
committed
change type hierarchy implementation to LSP
Signed-off-by: Shi Chen <[email protected]>
1 parent d169b40 commit c393ba1

11 files changed

+210
-409
lines changed

Diff for: package.json

+28-24
Original file line numberDiff line numberDiff line change
@@ -1189,34 +1189,27 @@
11891189
"category": "Java"
11901190
},
11911191
{
1192-
"command": "java.action.showTypeHierarchy",
1193-
"title": "%java.action.showTypeHierarchy%",
1192+
"command": "java.action.showClassHierarchy",
1193+
"title": "%java.action.showClassHierarchy%",
1194+
"icon": "$(type-hierarchy)",
11941195
"category": "Java"
11951196
},
11961197
{
1197-
"command": "java.action.showClassHierarchy",
1198+
"command": "java.action.showClassHierarchyFromReferenceView",
11981199
"title": "%java.action.showClassHierarchy%",
11991200
"icon": "$(type-hierarchy)",
1200-
"enablement": "typeHierarchyDirection != both && typeHierarchySymbolKind != 10",
12011201
"category": "Java"
12021202
},
12031203
{
12041204
"command": "java.action.showSupertypeHierarchy",
12051205
"title": "%java.action.showSupertypeHierarchy%",
12061206
"icon": "$(type-hierarchy-super)",
1207-
"enablement": "typeHierarchyDirection != parents",
12081207
"category": "Java"
12091208
},
12101209
{
12111210
"command": "java.action.showSubtypeHierarchy",
12121211
"title": "%java.action.showSubtypeHierarchy%",
12131212
"icon": "$(type-hierarchy-sub)",
1214-
"enablement": "typeHierarchyDirection != children",
1215-
"category": "Java"
1216-
},
1217-
{
1218-
"command": "java.action.changeBaseType",
1219-
"title": "%java.action.changeBaseType%",
12201213
"category": "Java"
12211214
},
12221215
{
@@ -1287,7 +1280,7 @@
12871280
"group": "navigation@90"
12881281
},
12891282
{
1290-
"command": "java.action.showTypeHierarchy",
1283+
"command": "java.action.showClassHierarchy",
12911284
"when": "javaLSReady && editorTextFocus && editorLangId == java",
12921285
"group": "0_navigation@3"
12931286
}
@@ -1326,12 +1319,12 @@
13261319
"when": "javaLSReady"
13271320
},
13281321
{
1329-
"command": "java.action.showTypeHierarchy",
1330-
"when": "javaLSReady && editorIsOpen"
1322+
"command": "java.action.showClassHierarchy",
1323+
"when": "javaLSReady && editorIsOpen && reference-list.source != typeHierarchy"
13311324
},
13321325
{
1333-
"command": "java.action.showClassHierarchy",
1334-
"when": "false"
1326+
"command": "java.action.showClassHierarchyFromReferenceView",
1327+
"when": "javaLSReady && editorIsOpen && reference-list.source == typeHierarchy"
13351328
},
13361329
{
13371330
"command": "java.action.showSubtypeHierarchy",
@@ -1341,10 +1334,6 @@
13411334
"command": "java.action.showSupertypeHierarchy",
13421335
"when": "false"
13431336
},
1344-
{
1345-
"command": "java.action.changeBaseType",
1346-
"when": "false"
1347-
},
13481337
{
13491338
"command": "java.project.updateSourceAttachment.command",
13501339
"when": "false"
@@ -1368,9 +1357,9 @@
13681357
],
13691358
"view/title": [
13701359
{
1371-
"command": "java.action.showClassHierarchy",
1360+
"command": "java.action.showClassHierarchyFromReferenceView",
13721361
"group": "navigation@-1",
1373-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && typeHierarchySymbolKind != 10"
1362+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == typeHierarchy"
13741363
},
13751364
{
13761365
"command": "java.action.showSupertypeHierarchy",
@@ -1385,9 +1374,24 @@
13851374
],
13861375
"view/item/context": [
13871376
{
1388-
"command": "java.action.changeBaseType",
1377+
"command": "java.action.showClassHierarchy",
1378+
"group": "1",
1379+
"when": "view == references-view.tree && viewItem == java-type-item"
1380+
},
1381+
{
1382+
"command": "java.action.showClassHierarchyFromReferenceView",
1383+
"group": "1",
1384+
"when": "view == references-view.tree && viewItem == type-item"
1385+
},
1386+
{
1387+
"command": "java.action.showSupertypeHierarchy",
1388+
"group": "1",
1389+
"when": "view == references-view.tree && viewItem == java-type-item"
1390+
},
1391+
{
1392+
"command": "java.action.showSubtypeHierarchy",
13891393
"group": "1",
1390-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && viewItem != 'false'"
1394+
"when": "view == references-view.tree && viewItem == java-type-item"
13911395
}
13921396
]
13931397
}

Diff for: package.nls.json

-2
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
}

Diff for: package.nls.zh-cn.json

-2
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
}

Diff for: src/commands.ts

+5-17
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
*/

Diff for: src/standardLanguageClient.ts

+58-20
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

@@ -105,7 +106,7 @@ export class StandardLanguageClient {
105106

106107
// Create the language client and start the client.
107108
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions);
108-
109+
this.languageClient.registerFeature(new TypeHierarchyFeature(this.languageClient));
109110
this.registerCommandsForStandardServer(context, jdtEventEmitter);
110111
fileEventHandler.registerFileEventHandlers(this.languageClient, context);
111112

@@ -364,31 +365,68 @@ export class StandardLanguageClient {
364365
}
365366
}));
366367

367-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_TYPE_HIERARCHY, (location: any) => {
368-
if (location instanceof Uri) {
369-
typeHierarchyTree.setTypeHierarchy(new Location(location, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
370-
} else {
371-
if (window.activeTextEditor?.document?.languageId !== "java") {
372-
return;
368+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, async (anchor: any) => {
369+
try {
370+
if (anchor instanceof Uri) { // comes from context menu
371+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor, window.activeTextEditor.selection.active));
372+
} else if (anchor instanceof TypeHierarchyItem) { // comes from class hierarchy view item
373+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.uri, anchor.range.start));
374+
} else { // comes from command palette
375+
if (window.activeTextEditor?.document?.languageId !== "java") {
376+
return;
377+
}
378+
await typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active));
379+
}
380+
} catch (e) {
381+
if (e?.message) {
382+
// show message in the selection when call from editor context menu
383+
if (anchor instanceof Uri) {
384+
showNoLocationFound(e.message);
385+
} else {
386+
window.showErrorMessage(e.message);
387+
}
373388
}
374-
typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
375389
}
376390
}));
377391

378-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, () => {
379-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.both);
380-
}));
381-
382-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, () => {
383-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.parents);
392+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW, async (anchor?: any) => {
393+
try {
394+
if (!anchor) { // comes from reference-view's title or command palette
395+
await typeHierarchyTree.setTypeHierarchyFromReferenceView();
396+
} else if (anchor.item instanceof TypeHierarchyItem) { // comes from reference-view's item
397+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.item.uri, anchor.item.range.start));
398+
}
399+
} catch (e) {
400+
if (e?.message) {
401+
window.showErrorMessage(e.message);
402+
}
403+
}
384404
}));
385405

386-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, () => {
387-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.children);
406+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, async (anchor?: any) => {
407+
let location: Location;
408+
if (!anchor) {
409+
location = typeHierarchyTree.getAnchor();
410+
} else if (anchor instanceof TypeHierarchyItem) {
411+
location = new Location(anchor.uri, anchor.range.start);
412+
}
413+
if (location) {
414+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
415+
await commands.executeCommand(showSupertypeHierarchyReferenceViewCommand, location);
416+
}
388417
}));
389418

390-
context.subscriptions.push(commands.registerCommand(Commands.CHANGE_BASE_TYPE, async (item: TypeHierarchyItem) => {
391-
typeHierarchyTree.changeBaseItem(item);
419+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, async (anchor?: any) => {
420+
let location: Location;
421+
if (!anchor) {
422+
location = typeHierarchyTree.getAnchor();
423+
} else if (anchor instanceof TypeHierarchyItem) {
424+
location = new Location(anchor.uri, anchor.range.start);
425+
}
426+
if (location) {
427+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
428+
await commands.executeCommand(showSubtypeHierarchyReferenceViewCommand, location);
429+
}
392430
}));
393431

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

0 commit comments

Comments
 (0)