@@ -4,8 +4,9 @@ import * as fse from 'fs-extra';
44import { findRuntimes } from "jdk-utils" ;
55import * as net from 'net' ;
66import * 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" ;
88import { ConfigurationParams , ConfigurationRequest , LanguageClientOptions , Location as LSLocation , MessageType , Position as LSPosition , TextDocumentPositionParams , WorkspaceEdit } from "vscode-languageclient" ;
9+ import { TypeHierarchyFeature } from 'vscode-languageclient/lib/common/typeHierarchy' ;
910import { LanguageClient , StreamInfo } from "vscode-languageclient/node" ;
1011import { apiManager } from "./apiManager" ;
1112import * as buildPath from './buildpath' ;
@@ -34,7 +35,7 @@ import { snippetCompletionProvider } from "./snippetCompletionProvider";
3435import * as sourceAction from './sourceAction' ;
3536import { askForProjects , projectConfigurationUpdate , upgradeGradle } from "./standardLanguageClientUtils" ;
3637import { TracingLanguageClient } from './TracingLanguageClient' ;
37- import { TypeHierarchyDirection , TypeHierarchyItem } from "./typeHierarchy/protocol" ;
38+ import { CodeTypeHierarchyItem , showSubtypeHierarchyReferenceViewCommand , showSupertypeHierarchyReferenceViewCommand , showTypeHierarchyReferenceViewCommand } from "./typeHierarchy/protocol" ;
3839import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree" ;
3940import { 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