Skip to content

Commit

Permalink
change type hierarchy implementation to LSP
Browse files Browse the repository at this point in the history
Signed-off-by: Shi Chen <[email protected]>
  • Loading branch information
CsCherrYY committed Mar 22, 2023
1 parent d169b40 commit c393ba1
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 409 deletions.
52 changes: 28 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1189,34 +1189,27 @@
"category": "Java"
},
{
"command": "java.action.showTypeHierarchy",
"title": "%java.action.showTypeHierarchy%",
"command": "java.action.showClassHierarchy",
"title": "%java.action.showClassHierarchy%",
"icon": "$(type-hierarchy)",
"category": "Java"
},
{
"command": "java.action.showClassHierarchy",
"command": "java.action.showClassHierarchyFromReferenceView",
"title": "%java.action.showClassHierarchy%",
"icon": "$(type-hierarchy)",
"enablement": "typeHierarchyDirection != both && typeHierarchySymbolKind != 10",
"category": "Java"
},
{
"command": "java.action.showSupertypeHierarchy",
"title": "%java.action.showSupertypeHierarchy%",
"icon": "$(type-hierarchy-super)",
"enablement": "typeHierarchyDirection != parents",
"category": "Java"
},
{
"command": "java.action.showSubtypeHierarchy",
"title": "%java.action.showSubtypeHierarchy%",
"icon": "$(type-hierarchy-sub)",
"enablement": "typeHierarchyDirection != children",
"category": "Java"
},
{
"command": "java.action.changeBaseType",
"title": "%java.action.changeBaseType%",
"category": "Java"
},
{
Expand Down Expand Up @@ -1287,7 +1280,7 @@
"group": "navigation@90"
},
{
"command": "java.action.showTypeHierarchy",
"command": "java.action.showClassHierarchy",
"when": "javaLSReady && editorTextFocus && editorLangId == java",
"group": "0_navigation@3"
}
Expand Down Expand Up @@ -1326,12 +1319,12 @@
"when": "javaLSReady"
},
{
"command": "java.action.showTypeHierarchy",
"when": "javaLSReady && editorIsOpen"
"command": "java.action.showClassHierarchy",
"when": "javaLSReady && editorIsOpen && reference-list.source != typeHierarchy"
},
{
"command": "java.action.showClassHierarchy",
"when": "false"
"command": "java.action.showClassHierarchyFromReferenceView",
"when": "javaLSReady && editorIsOpen && reference-list.source == typeHierarchy"
},
{
"command": "java.action.showSubtypeHierarchy",
Expand All @@ -1341,10 +1334,6 @@
"command": "java.action.showSupertypeHierarchy",
"when": "false"
},
{
"command": "java.action.changeBaseType",
"when": "false"
},
{
"command": "java.project.updateSourceAttachment.command",
"when": "false"
Expand All @@ -1368,9 +1357,9 @@
],
"view/title": [
{
"command": "java.action.showClassHierarchy",
"command": "java.action.showClassHierarchyFromReferenceView",
"group": "navigation@-1",
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && typeHierarchySymbolKind != 10"
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == typeHierarchy"
},
{
"command": "java.action.showSupertypeHierarchy",
Expand All @@ -1385,9 +1374,24 @@
],
"view/item/context": [
{
"command": "java.action.changeBaseType",
"command": "java.action.showClassHierarchy",
"group": "1",
"when": "view == references-view.tree && viewItem == java-type-item"
},
{
"command": "java.action.showClassHierarchyFromReferenceView",
"group": "1",
"when": "view == references-view.tree && viewItem == type-item"
},
{
"command": "java.action.showSupertypeHierarchy",
"group": "1",
"when": "view == references-view.tree && viewItem == java-type-item"
},
{
"command": "java.action.showSubtypeHierarchy",
"group": "1",
"when": "view == references-view.tree && reference-list.hasResult && reference-list.source == javaTypeHierarchy && viewItem != 'false'"
"when": "view == references-view.tree && viewItem == java-type-item"
}
]
}
Expand Down
2 changes: 0 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
"java.project.listSourcePaths": "List All Java Source Paths",
"java.show.server.task.status": "Show Build Job Status",
"java.action.navigateToSuperImplementation": "Go to Super Implementation",
"java.action.showTypeHierarchy": "Show Type Hierarchy",
"java.action.showClassHierarchy": "Show Class Hierarchy",
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
"java.action.changeBaseType": "Base on this Type",
"java.project.createModuleInfo.command": "Create module-info.java",
"java.clean.sharedIndexes": "Clean Shared Indexes"
}
2 changes: 0 additions & 2 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
"java.project.listSourcePaths": "列出所有 Java 源代码路径",
"java.show.server.task.status": "显示工作状态",
"java.action.navigateToSuperImplementation": "转到父类实现",
"java.action.showTypeHierarchy": "显示类型层次结构",
"java.action.showClassHierarchy": "显示类的继承关系",
"java.action.showSupertypeHierarchy": "显示父类层次结构",
"java.action.showSubtypeHierarchy": "显示子类层次结构",
"java.action.changeBaseType": "基于此类型",
"java.project.createModuleInfo.command": "创建 module-info.java",
"java.clean.sharedIndexes": "清理共享的索引文件"
}
22 changes: 5 additions & 17 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,33 +223,21 @@ export namespace Commands {
*/
export const NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND = 'java.action.navigateToSuperImplementation';
/**
* Open Type Hierarchy of given Selection.
*/
export const SHOW_TYPE_HIERARCHY = 'java.action.showTypeHierarchy';
/**
* Show SuperType Hierarchy of given Selection.
* Show Supertype Hierarchy of given type.
*/
export const SHOW_SUPERTYPE_HIERARCHY = 'java.action.showSupertypeHierarchy';
/**
* Show SubType Hierarchy of given Selection.
* Show Subtype Hierarchy of given type.
*/
export const SHOW_SUBTYPE_HIERARCHY = 'java.action.showSubtypeHierarchy';
/**
* Show Type Hierarchy of given Selection.
* Show Class Hierarchy of given type.
*/
export const SHOW_CLASS_HIERARCHY = 'java.action.showClassHierarchy';
/**
* Change the base type of Type Hierarchy.
*/
export const CHANGE_BASE_TYPE = 'java.action.changeBaseType';
/**
* Open the given TypeHierarchy Item.
*/
export const OPEN_TYPE_HIERARCHY = 'java.navigate.openTypeHierarchy';
/**
* Resolve the given TypeHierarchy Item.
* Show Class Hierarchy of given type from reference view.
*/
export const RESOLVE_TYPE_HIERARCHY = 'java.navigate.resolveTypeHierarchy';
export const SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW = 'java.action.showClassHierarchyFromReferenceView';
/**
* Show server task status
*/
Expand Down
78 changes: 58 additions & 20 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as fse from 'fs-extra';
import { findRuntimes } from "jdk-utils";
import * as net from 'net';
import * as path from 'path';
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, Uri, ViewColumn, window, workspace } from "vscode";
import { CancellationToken, CodeActionKind, commands, ConfigurationTarget, DocumentSelector, EventEmitter, ExtensionContext, extensions, languages, Location, ProgressLocation, TextEditor, TypeHierarchyItem, Uri, ViewColumn, window, workspace } from "vscode";
import { ConfigurationParams, ConfigurationRequest, LanguageClientOptions, Location as LSLocation, MessageType, Position as LSPosition, TextDocumentPositionParams, WorkspaceEdit } from "vscode-languageclient";
import { TypeHierarchyFeature } from 'vscode-languageclient/lib/common/typeHierarchy';
import { LanguageClient, StreamInfo } from "vscode-languageclient/node";
import { apiManager } from "./apiManager";
import * as buildPath from './buildpath';
Expand Down Expand Up @@ -34,7 +35,7 @@ import { snippetCompletionProvider } from "./snippetCompletionProvider";
import * as sourceAction from './sourceAction';
import { askForProjects, projectConfigurationUpdate, upgradeGradle } from "./standardLanguageClientUtils";
import { TracingLanguageClient } from './TracingLanguageClient';
import { TypeHierarchyDirection, TypeHierarchyItem } from "./typeHierarchy/protocol";
import { CodeTypeHierarchyItem, showSubtypeHierarchyReferenceViewCommand, showSupertypeHierarchyReferenceViewCommand, showTypeHierarchyReferenceViewCommand } from "./typeHierarchy/protocol";
import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree";
import { getAllJavaProjects, getJavaConfig, getJavaConfiguration } from "./utils";

Expand Down Expand Up @@ -105,7 +106,7 @@ export class StandardLanguageClient {

// Create the language client and start the client.
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions);

this.languageClient.registerFeature(new TypeHierarchyFeature(this.languageClient));
this.registerCommandsForStandardServer(context, jdtEventEmitter);
fileEventHandler.registerFileEventHandlers(this.languageClient, context);

Expand Down Expand Up @@ -364,31 +365,68 @@ export class StandardLanguageClient {
}
}));

context.subscriptions.push(commands.registerCommand(Commands.SHOW_TYPE_HIERARCHY, (location: any) => {
if (location instanceof Uri) {
typeHierarchyTree.setTypeHierarchy(new Location(location, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
} else {
if (window.activeTextEditor?.document?.languageId !== "java") {
return;
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, async (anchor: any) => {
try {
if (anchor instanceof Uri) { // comes from context menu
await typeHierarchyTree.setTypeHierarchy(new Location(anchor, window.activeTextEditor.selection.active));
} else if (anchor instanceof TypeHierarchyItem) { // comes from class hierarchy view item
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.uri, anchor.range.start));
} else { // comes from command palette
if (window.activeTextEditor?.document?.languageId !== "java") {
return;
}
await typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active));
}
} catch (e) {
if (e?.message) {
// show message in the selection when call from editor context menu
if (anchor instanceof Uri) {
showNoLocationFound(e.message);
} else {
window.showErrorMessage(e.message);
}
}
typeHierarchyTree.setTypeHierarchy(new Location(window.activeTextEditor.document.uri, window.activeTextEditor.selection.active), TypeHierarchyDirection.both);
}
}));

context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY, () => {
typeHierarchyTree.changeDirection(TypeHierarchyDirection.both);
}));

context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, () => {
typeHierarchyTree.changeDirection(TypeHierarchyDirection.parents);
context.subscriptions.push(commands.registerCommand(Commands.SHOW_CLASS_HIERARCHY_FROM_REFERENCE_VIEW, async (anchor?: any) => {
try {
if (!anchor) { // comes from reference-view's title or command palette
await typeHierarchyTree.setTypeHierarchyFromReferenceView();
} else if (anchor.item instanceof TypeHierarchyItem) { // comes from reference-view's item
await typeHierarchyTree.setTypeHierarchy(new Location(anchor.item.uri, anchor.item.range.start));
}
} catch (e) {
if (e?.message) {
window.showErrorMessage(e.message);
}
}
}));

context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, () => {
typeHierarchyTree.changeDirection(TypeHierarchyDirection.children);
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUPERTYPE_HIERARCHY, async (anchor?: any) => {
let location: Location;
if (!anchor) {
location = typeHierarchyTree.getAnchor();
} else if (anchor instanceof TypeHierarchyItem) {
location = new Location(anchor.uri, anchor.range.start);
}
if (location) {
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
await commands.executeCommand(showSupertypeHierarchyReferenceViewCommand, location);
}
}));

context.subscriptions.push(commands.registerCommand(Commands.CHANGE_BASE_TYPE, async (item: TypeHierarchyItem) => {
typeHierarchyTree.changeBaseItem(item);
context.subscriptions.push(commands.registerCommand(Commands.SHOW_SUBTYPE_HIERARCHY, async (anchor?: any) => {
let location: Location;
if (!anchor) {
location = typeHierarchyTree.getAnchor();
} else if (anchor instanceof TypeHierarchyItem) {
location = new Location(anchor.uri, anchor.range.start);
}
if (location) {
await commands.executeCommand(showTypeHierarchyReferenceViewCommand);
await commands.executeCommand(showSubtypeHierarchyReferenceViewCommand, location);
}
}));

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

0 comments on commit c393ba1

Please sign in to comment.