11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4- import { DocumentSymbol , Range , TreeItem , TreeItemCollapsibleState } from "vscode" ;
4+ import { Command , DocumentSymbol , Range , SymbolKind , ThemeIcon , TreeItem , TreeItemCollapsibleState } from "vscode" ;
5+ import { Commands } from "../commands" ;
56import { Explorer } from "../constants" ;
6- import { BaseSymbolNode } from "./baseSymbolNode" ;
77import { ExplorerNode } from "./explorerNode" ;
88import { PrimaryTypeNode } from "./PrimaryTypeNode" ;
99
10- export class DocumentSymbolNode extends BaseSymbolNode {
10+ export class DocumentSymbolNode extends ExplorerNode {
1111
12- constructor ( symbolInfo : DocumentSymbol , parent : PrimaryTypeNode ) {
13- super ( symbolInfo , parent ) ;
12+ private readonly _iconMap : Map < SymbolKind , string > = new Map ( [
13+ [ SymbolKind . Package , "package" ] ,
14+ [ SymbolKind . Class , "class" ] ,
15+ [ SymbolKind . Interface , "interface" ] ,
16+ [ SymbolKind . Enum , "enum" ] ,
17+ [ SymbolKind . EnumMember , "enum-member" ] ,
18+ [ SymbolKind . Constant , "constant" ] ,
19+ [ SymbolKind . Method , "method" ] ,
20+ [ SymbolKind . Function , "method" ] ,
21+ [ SymbolKind . Constructor , "method" ] ,
22+ [ SymbolKind . Field , "field" ] ,
23+ [ SymbolKind . Property , "property" ] ,
24+ [ SymbolKind . Variable , "variable" ] ,
25+ ] ) ;
26+
27+ constructor ( private readonly symbolInfo : DocumentSymbol , parent : PrimaryTypeNode ) {
28+ super ( parent ) ;
1429 }
1530
1631 public getChildren ( ) : ExplorerNode [ ] | Promise < ExplorerNode [ ] > {
1732 const res : ExplorerNode [ ] = [ ] ;
18- if ( this . symbolInfo && ( < DocumentSymbol > this . symbolInfo ) . children && ( < DocumentSymbol > this . symbolInfo ) . children . length ) {
19- ( < DocumentSymbol > this . symbolInfo ) . children . forEach ( ( child ) => {
33+ if ( this . symbolInfo ?. children ? .length ) {
34+ this . symbolInfo . children . forEach ( ( child ) => {
2035 res . push ( new DocumentSymbolNode ( child , this . getParent ( ) as PrimaryTypeNode ) ) ;
2136 } ) ;
2237 }
@@ -25,19 +40,35 @@ export class DocumentSymbolNode extends BaseSymbolNode {
2540
2641 public getTreeItem ( ) : TreeItem | Promise < TreeItem > {
2742 const item = new TreeItem ( this . symbolInfo . name ,
28- ( ( < DocumentSymbol > this . symbolInfo ) . children && ( < DocumentSymbol > this . symbolInfo ) . children . length )
29- ? TreeItemCollapsibleState . Collapsed : TreeItemCollapsibleState . None ) ;
43+ this . symbolInfo ? .children ?. length ? TreeItemCollapsibleState . Collapsed
44+ : TreeItemCollapsibleState . None ) ;
3045 item . iconPath = this . iconPath ;
3146 item . command = this . command ;
3247 return item ;
3348 }
3449
3550 public get range ( ) : Range {
3651 // Using `selectionRange` instead of `range` to make sure the cursor will be pointing to the codes, not the comments
37- return ( < DocumentSymbol > this . symbolInfo ) . selectionRange ;
52+ return this . symbolInfo . selectionRange ;
3853 }
3954
4055 public computeContextValue ( ) : string | undefined {
4156 return `java:${ Explorer . ContextValueType . Symbol } ` ;
4257 }
58+
59+ protected get iconPath ( ) : ThemeIcon {
60+ if ( this . _iconMap . has ( this . symbolInfo . kind ) ) {
61+ const symbolKind = this . _iconMap . get ( this . symbolInfo . kind ) ;
62+ return new ThemeIcon ( `symbol-${ symbolKind } ` ) ;
63+ }
64+ return new ThemeIcon ( "symbol-misc" ) ;
65+ }
66+
67+ protected get command ( ) : Command {
68+ return {
69+ title : "Go to outline" ,
70+ command : Commands . VIEW_PACKAGE_OUTLINE ,
71+ arguments : [ ( this . getParent ( ) as PrimaryTypeNode ) . uri , this . range ] ,
72+ } ;
73+ }
4374}
0 commit comments