@@ -7,8 +7,10 @@ namespace ts.InlineHints {
7
7
whitespaceAfter ?: boolean ;
8
8
}
9
9
10
+ const maxHintsLength = 20 ;
11
+
10
12
export function provideInlineHints ( context : InlineHintsContext ) : HintInfo [ ] {
11
- const { file, program, span, cancellationToken } = context ;
13
+ const { file, program, span, cancellationToken, preferences } = context ;
12
14
13
15
const checker = program . getTypeChecker ( ) ;
14
16
const result : HintInfo [ ] = [ ] ;
@@ -40,15 +42,46 @@ namespace ts.InlineHints {
40
42
return ;
41
43
}
42
44
43
- if ( isCallExpression ( node ) || isNewExpression ( node ) ) {
45
+ if ( preferences . includeInlineParameterName && ( isCallExpression ( node ) || isNewExpression ( node ) ) ) {
44
46
visitCallOrNewExpression ( node ) ;
45
47
}
46
- else if ( isArrowFunction ( node ) || isFunctionExpression ( node ) ) {
48
+ else if ( preferences . includeInlineFunctionParameterType && ( isArrowFunction ( node ) || isFunctionExpression ( node ) ) ) {
47
49
visitFunctionExpressionLike ( node ) ;
48
50
}
51
+ else if ( preferences . includeInlineVariableType && ( isVariableDeclaration ( node ) ) ) {
52
+ visitVariableDeclaration ( node ) ;
53
+ }
49
54
return forEachChild ( node , visitor ) ;
50
55
}
51
56
57
+ function addNameHints ( text : string , position : number ) {
58
+ result . push ( {
59
+ text : `${ truncation ( text , maxHintsLength ) } :` ,
60
+ position,
61
+ whitespaceAfter : true ,
62
+ } ) ;
63
+ }
64
+
65
+ function addTypeHints ( text : string , position : number ) {
66
+ result . push ( {
67
+ text : `:${ truncation ( text , maxHintsLength ) } ` ,
68
+ position,
69
+ whitespaceBefore : true ,
70
+ } ) ;
71
+ }
72
+
73
+ function visitVariableDeclaration ( decl : VariableDeclaration ) {
74
+ if ( decl . type || ! decl . initializer ) {
75
+ return ;
76
+ }
77
+
78
+ const initializerType = checker . getTypeAtLocation ( decl . initializer ) ;
79
+ const typeDisplayString = displayPartsToString ( typeToDisplayParts ( checker , initializerType ) ) ;
80
+ if ( typeDisplayString ) {
81
+ addTypeHints ( typeDisplayString , decl . name . end ) ;
82
+ }
83
+ }
84
+
52
85
function visitCallOrNewExpression ( expr : CallExpression | NewExpression ) {
53
86
if ( ! expr . arguments || ! expr . arguments . length ) {
54
87
return ;
@@ -66,11 +99,7 @@ namespace ts.InlineHints {
66
99
const arg = expr . arguments [ i ] ;
67
100
const argumentName = isIdentifier ( arg ) ? arg . text : undefined ;
68
101
if ( ! argumentName || argumentName !== parameterName ) {
69
- result . push ( {
70
- text : `${ unescapeLeadingUnderscores ( parameterName ) } :` ,
71
- position : expr . arguments [ i ] . getStart ( ) ,
72
- whitespaceAfter : true ,
73
- } ) ;
102
+ addNameHints ( unescapeLeadingUnderscores ( parameterName ) , expr . arguments [ i ] . getStart ( ) ) ;
74
103
}
75
104
}
76
105
}
@@ -98,25 +127,34 @@ namespace ts.InlineHints {
98
127
continue ;
99
128
}
100
129
101
- const signatureParam = signature . parameters [ i ] ;
102
- const signatureParamType = checker . getTypeOfSymbolAtLocation ( signatureParam , signatureParam . valueDeclaration ) ;
103
-
104
- const valueDeclaration = signatureParam . valueDeclaration ;
105
- if ( ! valueDeclaration || ! isParameter ( valueDeclaration ) || ! valueDeclaration . type ) {
106
- continue ;
107
- }
108
-
109
- const typeDisplayString = displayPartsToString ( typeToDisplayParts ( checker , signatureParamType ) ) ;
130
+ const typeDisplayString = getParameterDeclarationTypeDisplayString ( signature . parameters [ i ] ) ;
110
131
if ( ! typeDisplayString ) {
111
132
continue ;
112
133
}
113
134
114
- result . push ( {
115
- text : `:${ typeDisplayString } ` ,
116
- position : param . end ,
117
- whitespaceBefore : true ,
118
- } ) ;
135
+ addTypeHints ( typeDisplayString , param . end ) ;
136
+ }
137
+ }
138
+
139
+ function getParameterDeclarationTypeDisplayString ( symbol : Symbol ) {
140
+ const valueDeclaration = symbol . valueDeclaration ;
141
+ if ( ! valueDeclaration || ! isParameter ( valueDeclaration ) ) {
142
+ return undefined ;
143
+ }
144
+
145
+ if ( valueDeclaration . type ) {
146
+ return valueDeclaration . type . getText ( ) ;
147
+ }
148
+
149
+ const signatureParamType = checker . getTypeOfSymbolAtLocation ( symbol , symbol . valueDeclaration ) ;
150
+ return displayPartsToString ( typeToDisplayParts ( checker , signatureParamType ) ) ;
151
+ }
152
+
153
+ function truncation ( text : string , maxLength : number ) {
154
+ if ( text . length > maxLength ) {
155
+ return text . substr ( 0 , maxLength - "..." . length ) + "..." ;
119
156
}
157
+ return text ;
120
158
}
121
159
}
122
160
}
0 commit comments