This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
DeclarationStructureTreeItem.ts
121 lines (107 loc) · 4.42 KB
/
DeclarationStructureTreeItem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
ClassDeclaration,
Declaration,
FunctionDeclaration,
GetterDeclaration,
InterfaceDeclaration,
MethodDeclaration,
PropertyDeclaration,
SetterDeclaration,
VariableDeclaration,
} from 'typescript-parser';
import { stringTemplate } from 'typescript-parser/utilities/StringTemplate';
import { CompletionItemKind, ExtensionContext, TreeItemCollapsibleState } from 'vscode';
import { getItemKind } from '../../utilities/utilityFunctions';
import { BaseStructureTreeItem } from './BaseStructureTreeItem';
const fileTemplate = stringTemplate`./src/extension/assets/icons/declarations/${0}.svg`;
/**
* Function to calculate the displayed name of the declaration structure item.
*
* @param {Declaration} declaration
* @returns {string}
*/
function getDeclarationLabel(declaration: Declaration): string {
if (
declaration instanceof FunctionDeclaration ||
declaration instanceof MethodDeclaration
) {
const params = declaration.parameters.map(p => p.name + (p.type ? `: ${p.type}` : '')).join(', ');
return `${declaration.name}(${params})${declaration.type ? `: ${declaration.type}` : ''}`;
}
if (declaration instanceof PropertyDeclaration) {
return declaration.name + (declaration.type ? `: ${declaration.type}` : '');
}
if (
declaration instanceof ClassDeclaration ||
declaration instanceof InterfaceDeclaration
) {
return declaration.name + (declaration.typeParameters ? `<${declaration.typeParameters.join(', ')}>` : '');
}
if (
declaration instanceof GetterDeclaration ||
declaration instanceof SetterDeclaration
) {
return `${declaration instanceof GetterDeclaration ? 'get' : 'set'}() ${declaration.name}` +
`${declaration.type ? `: ${declaration.type}` : ''}`;
}
return declaration.name;
}
/**
* Structure item that represents a typescript declaration of any way.
*
* @export
* @class DeclarationStructureTreeItem
* @extends {BaseStructureTreeItem}
*/
export class DeclarationStructureTreeItem extends BaseStructureTreeItem {
public get iconPath(): string | undefined {
switch (getItemKind(this.declaration)) {
case CompletionItemKind.Class:
case CompletionItemKind.Keyword:
return this.context.asAbsolutePath(fileTemplate('class'));
case CompletionItemKind.Interface:
return this.context.asAbsolutePath(fileTemplate('interface'));
case CompletionItemKind.Enum:
return this.context.asAbsolutePath(fileTemplate('enum'));
case CompletionItemKind.Function:
case CompletionItemKind.Method:
return this.context.asAbsolutePath(fileTemplate('callable'));
case CompletionItemKind.Module:
return this.context.asAbsolutePath(fileTemplate('module'));
case CompletionItemKind.Property:
return this.context.asAbsolutePath(fileTemplate('property'));
default:
break;
}
if (getItemKind(this.declaration) === CompletionItemKind.Variable) {
return (this.declaration as VariableDeclaration).isConst ?
this.context.asAbsolutePath(fileTemplate('const')) :
this.context.asAbsolutePath(fileTemplate('variable'));
}
return this.context.asAbsolutePath(fileTemplate('default'));
}
constructor(public declaration: Declaration, private context: ExtensionContext) {
super(getDeclarationLabel(declaration));
if (
declaration instanceof ClassDeclaration ||
declaration instanceof InterfaceDeclaration
) {
this.collapsibleState = TreeItemCollapsibleState.Collapsed;
} else {
this.command = this.command = this.createJumpToCommand([declaration]);
}
}
public getChildren(): BaseStructureTreeItem[] {
if (
this.declaration instanceof ClassDeclaration ||
this.declaration instanceof InterfaceDeclaration
) {
return [
...this.declaration.accessors.map(p => new DeclarationStructureTreeItem(p, this.context)),
...this.declaration.properties.map(p => new DeclarationStructureTreeItem(p, this.context)),
...this.declaration.methods.map(m => new DeclarationStructureTreeItem(m, this.context)),
];
}
return [];
}
}