Skip to content

Commit f805497

Browse files
CsCherrYYrobstryker
authored andcommitted
change type hierarchy implementation to LSP
Signed-off-by: Shi Chen <chenshi@microsoft.com>
1 parent 1e405bd commit f805497

11 files changed

Lines changed: 220 additions & 408 deletions

File tree

‎package.json‎

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,34 +1840,27 @@
18401840
"category": "Java"
18411841
},
18421842
{
1843-
"command": "java.action.showTypeHierarchy",
1844-
"title": "%java.action.showTypeHierarchy%",
1843+
"command": "java.action.showClassHierarchy",
1844+
"title": "%java.action.showClassHierarchy%",
1845+
"icon": "$(type-hierarchy)",
18451846
"category": "Java"
18461847
},
18471848
{
1848-
"command": "java.action.showClassHierarchy",
1849+
"command": "java.action.showClassHierarchyFromReferenceView",
18491850
"title": "%java.action.showClassHierarchy%",
18501851
"icon": "$(type-hierarchy)",
1851-
"enablement": "typeHierarchyDirection != both && typeHierarchySymbolKind != 10",
18521852
"category": "Java"
18531853
},
18541854
{
18551855
"command": "java.action.showSupertypeHierarchy",
18561856
"title": "%java.action.showSupertypeHierarchy%",
18571857
"icon": "$(type-hierarchy-super)",
1858-
"enablement": "typeHierarchyDirection != parents",
18591858
"category": "Java"
18601859
},
18611860
{
18621861
"command": "java.action.showSubtypeHierarchy",
18631862
"title": "%java.action.showSubtypeHierarchy%",
18641863
"icon": "$(type-hierarchy-sub)",
1865-
"enablement": "typeHierarchyDirection != children",
1866-
"category": "Java"
1867-
},
1868-
{
1869-
"command": "java.action.changeBaseType",
1870-
"title": "%java.action.changeBaseType%",
18711864
"category": "Java"
18721865
},
18731866
{
@@ -2020,7 +2013,7 @@
20202013
"group": "navigation@90"
20212014
},
20222015
{
2023-
"command": "java.action.showTypeHierarchy",
2016+
"command": "java.action.showClassHierarchy",
20242017
"when": "javaLSReady && editorTextFocus && editorLangId == java",
20252018
"group": "0_navigation@3"
20262019
},
@@ -2064,12 +2057,12 @@
20642057
"when": "javaLSReady"
20652058
},
20662059
{
2067-
"command": "java.action.showTypeHierarchy",
2068-
"when": "javaLSReady && editorIsOpen"
2060+
"command": "java.action.showClassHierarchy",
2061+
"when": "javaLSReady && editorIsOpen && reference-list.source != typeHierarchy"
20692062
},
20702063
{
2071-
"command": "java.action.showClassHierarchy",
2072-
"when": "false"
2064+
"command": "java.action.showClassHierarchyFromReferenceView",
2065+
"when": "javaLSReady && editorIsOpen && reference-list.source == typeHierarchy"
20732066
},
20742067
{
20752068
"command": "java.action.showSubtypeHierarchy",
@@ -2079,10 +2072,6 @@
20792072
"command": "java.action.showSupertypeHierarchy",
20802073
"when": "false"
20812074
},
2082-
{
2083-
"command": "java.action.changeBaseType",
2084-
"when": "false"
2085-
},
20862075
{
20872076
"command": "java.project.updateSourceAttachment.command",
20882077
"when": "false"
@@ -2126,9 +2115,9 @@
21262115
],
21272116
"view/title": [
21282117
{
2129-
"command": "java.action.showClassHierarchy",
2118+
"command": "java.action.showClassHierarchyFromReferenceView",
21302119
"group": "navigation@-1",
2131-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && typeHierarchySymbolKind != 10"
2120+
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == typeHierarchy"
21322121
},
21332122
{
21342123
"command": "java.action.showSupertypeHierarchy",
@@ -2143,9 +2132,24 @@
21432132
],
21442133
"view/item/context": [
21452134
{
2146-
"command": "java.action.changeBaseType",
2135+
"command": "java.action.showClassHierarchy",
2136+
"group": "1",
2137+
"when": "view == references-view.tree && viewItem == java-type-item"
2138+
},
2139+
{
2140+
"command": "java.action.showClassHierarchyFromReferenceView",
2141+
"group": "1",
2142+
"when": "view == references-view.tree && viewItem == type-item"
2143+
},
2144+
{
2145+
"command": "java.action.showSupertypeHierarchy",
2146+
"group": "1",
2147+
"when": "view == references-view.tree && viewItem == java-type-item"
2148+
},
2149+
{
2150+
"command": "java.action.showSubtypeHierarchy",
21472151
"group": "1",
2148-
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && viewItem != 'false'"
2152+
"when": "view == references-view.tree && viewItem == java-type-item"
21492153
}
21502154
]
21512155
}

‎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
"java.server.restart": "Restart Java Language Server",

‎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
"java.action.filesExplorerPasteAction": "将剪贴板文本粘贴到文件中",

‎src/commands.ts‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,33 +229,21 @@ export namespace Commands {
229229
*/
230230
export const NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND = 'java.action.navigateToSuperImplementation';
231231
/**
232-
* Open Type Hierarchy of given Selection.
233-
*/
234-
export const SHOW_TYPE_HIERARCHY = 'java.action.showTypeHierarchy';
235-
/**
236-
* Show SuperType Hierarchy of given Selection.
232+
* Show Supertype Hierarchy of given type.
237233
*/
238234
export const SHOW_SUPERTYPE_HIERARCHY = 'java.action.showSupertypeHierarchy';
239235
/**
240-
* Show SubType Hierarchy of given Selection.
236+
* Show Subtype Hierarchy of given type.
241237
*/
242238
export const SHOW_SUBTYPE_HIERARCHY = 'java.action.showSubtypeHierarchy';
243239
/**
244-
* Show Type Hierarchy of given Selection.
240+
* Show Class Hierarchy of given type.
245241
*/
246242
export const SHOW_CLASS_HIERARCHY = 'java.action.showClassHierarchy';
247243
/**
248-
* Change the base type of Type Hierarchy.
249-
*/
250-
export const CHANGE_BASE_TYPE = 'java.action.changeBaseType';
251-
/**
252-
* Open the given TypeHierarchy Item.
253-
*/
254-
export const OPEN_TYPE_HIERARCHY = 'java.navigate.openTypeHierarchy';
255-
/**
256-
* Resolve the given TypeHierarchy Item.
244+
* Show Class Hierarchy of given type from reference view.
257245
*/
258-
export const RESOLVE_TYPE_HIERARCHY = 'java.navigate.resolveTypeHierarchy';
246+
export const SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW = 'java.action.showClassHierarchyFromReferenceView';
259247
/**
260248
* Show server task status
261249
*/

‎src/standardLanguageClient.ts‎

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import * as net from 'net';
44
import * as path from 'path';
5-
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from "vscode";
5+
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, TypeHierarchyItem, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from "vscode";
66
import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, TextDocumentPositionParams, WorkspaceEdit, StaticFeature, ClientCapabilities, FeatureState, TelemetryEventNotification } from "vscode-languageclient";
7+
import { TypeHierarchyFeature } from 'vscode-languageclient/lib/common/typeHierarchy';
78
import { LanguageClient, StreamInfo } from "vscode-languageclient/node";
89
import { apiManager } from "./apiManager";
910
import * as buildPath from './buildpath';
@@ -32,7 +33,7 @@ import { snippetCompletionProvider } from "./snippetCompletionProvider";
3233
import * as sourceAction from './sourceAction';
3334
import { askForProjects, projectConfigurationUpdate, upgradeGradle } from "./standardLanguageClientUtils";
3435
import { TracingLanguageClient } from './TracingLanguageClient';
35-
import { TypeHierarchyDirection, TypeHierarchyItem } from "./typeHierarchy/protocol";
36+
import { CodeTypeHierarchyItem, showSubtypeHierarchyReferenceViewCommand, showSupertypeHierarchyReferenceViewCommand, showTypeHierarchyReferenceViewCommand } from "./typeHierarchy/protocol";
3637
import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree";
3738
import { getAllJavaProjects, getAllProjects, getJavaConfiguration, isPrereleaseOrInsiderVersion } from "./utils";
3839
import { Telemetry } from "./telemetry";
@@ -109,6 +110,7 @@ export class StandardLanguageClient {
109110
// Create the language client and start the client.
110111
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions, DEBUG);
111112
this.languageClient.registerFeature(new DisableWillRenameFeature());
113+
this.languageClient.registerFeature(new TypeHierarchyFeature(this.languageClient));
112114

113115
this.registerCommandsForStandardServer(context, jdtEventEmitter);
114116
fileEventHandler.registerFileEventHandlers(this.languageClient, context);
@@ -490,31 +492,68 @@ export class StandardLanguageClient {
490492
}
491493
}));
492494

493-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_TYPE_HIERARCHY, (location: any) => {
494-
if (location instanceof Uri) {
495-
typeHierarchyTree.setTypeHierarchy(new Location(location, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
496-
} else {
497-
if (window.activeTextEditor?.document?.languageId !== "java") {
498-
return;
495+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, async (anchor: any) => {
496+
try {
497+
if (anchor instanceof Uri) { // comes from context menu
498+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor, window.activeTextEditor.selection.active));
499+
} else if (anchor instanceof TypeHierarchyItem) { // comes from class hierarchy view item
500+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.uri, anchor.range.start));
501+
} else { // comes from command palette
502+
if (window.activeTextEditor?.document?.languageId !== "java") {
503+
return;
504+
}
505+
await typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active));
506+
}
507+
} catch (e) {
508+
if (e?.message) {
509+
// show message in the selection when call from editor context menu
510+
if (anchor instanceof Uri) {
511+
showNoLocationFound(e.message);
512+
} else {
513+
window.showErrorMessage(e.message);
514+
}
499515
}
500-
typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
501516
}
502517
}));
503518

504-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, () => {
505-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.both);
506-
}));
507-
508-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, () => {
509-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.parents);
519+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW, async (anchor?: any) => {
520+
try {
521+
if (!anchor) { // comes from reference-view's title or command palette
522+
await typeHierarchyTree.setTypeHierarchyFromReferenceView();
523+
} else if (anchor.item instanceof TypeHierarchyItem) { // comes from reference-view's item
524+
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.item.uri, anchor.item.range.start));
525+
}
526+
} catch (e) {
527+
if (e?.message) {
528+
window.showErrorMessage(e.message);
529+
}
530+
}
510531
}));
511532

512-
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, () => {
513-
typeHierarchyTree.changeDirection(TypeHierarchyDirection.children);
533+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, async (anchor?: any) => {
534+
let location: Location;
535+
if (!anchor) {
536+
location = typeHierarchyTree.getAnchor();
537+
} else if (anchor instanceof TypeHierarchyItem) {
538+
location = new Location(anchor.uri, anchor.range.start);
539+
}
540+
if (location) {
541+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
542+
await commands.executeCommand(showSupertypeHierarchyReferenceViewCommand, location);
543+
}
514544
}));
515545

516-
context.subscriptions.push(commands.registerCommand(Commands.CHANGE_BASE_TYPE, async (item: TypeHierarchyItem) => {
517-
typeHierarchyTree.changeBaseItem(item);
546+
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, async (anchor?: any) => {
547+
let location: Location;
548+
if (!anchor) {
549+
location = typeHierarchyTree.getAnchor();
550+
} else if (anchor instanceof TypeHierarchyItem) {
551+
location = new Location(anchor.uri, anchor.range.start);
552+
}
553+
if (location) {
554+
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
555+
await commands.executeCommand(showSubtypeHierarchyReferenceViewCommand, location);
556+
}
518557
}));
519558

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

0 commit comments

Comments
 (0)