@@ -7,8 +7,10 @@ namespace ts.InlineHints {
77 whitespaceAfter ?: boolean ;
88 }
99
10+ const maxHintsLength = 20 ;
11+
1012 export function provideInlineHints ( context : InlineHintsContext ) : HintInfo [ ] {
11- const { file, program, span, cancellationToken } = context ;
13+ const { file, program, span, cancellationToken, preferences } = context ;
1214
1315 const checker = program . getTypeChecker ( ) ;
1416 const result : HintInfo [ ] = [ ] ;
@@ -40,15 +42,46 @@ namespace ts.InlineHints {
4042 return ;
4143 }
4244
43- if ( isCallExpression ( node ) || isNewExpression ( node ) ) {
45+ if ( preferences . includeInlineParameterName && ( isCallExpression ( node ) || isNewExpression ( node ) ) ) {
4446 visitCallOrNewExpression ( node ) ;
4547 }
46- else if ( isArrowFunction ( node ) || isFunctionExpression ( node ) ) {
48+ else if ( preferences . includeInlineFunctionParameterType && ( isArrowFunction ( node ) || isFunctionExpression ( node ) ) ) {
4749 visitFunctionExpressionLike ( node ) ;
4850 }
51+ else if ( preferences . includeInlineVariableType && ( isVariableDeclaration ( node ) ) ) {
52+ visitVariableDeclaration ( node ) ;
53+ }
4954 return forEachChild ( node , visitor ) ;
5055 }
5156
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+
5285 function visitCallOrNewExpression ( expr : CallExpression | NewExpression ) {
5386 if ( ! expr . arguments || ! expr . arguments . length ) {
5487 return ;
@@ -66,11 +99,7 @@ namespace ts.InlineHints {
6699 const arg = expr . arguments [ i ] ;
67100 const argumentName = isIdentifier ( arg ) ? arg . text : undefined ;
68101 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 ( ) ) ;
74103 }
75104 }
76105 }
@@ -98,25 +127,34 @@ namespace ts.InlineHints {
98127 continue ;
99128 }
100129
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 ] ) ;
110131 if ( ! typeDisplayString ) {
111132 continue ;
112133 }
113134
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 ) + "..." ;
119156 }
157+ return text ;
120158 }
121159 }
122160}
0 commit comments