Skip to content

Commit 679b58d

Browse files
committed
Support configure inline hints
1 parent ee6527e commit 679b58d

File tree

9 files changed

+102
-35
lines changed

9 files changed

+102
-35
lines changed

src/compiler/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8245,6 +8245,9 @@ namespace ts {
82458245
readonly providePrefixAndSuffixTextForRename?: boolean;
82468246
readonly includePackageJsonAutoImports?: "auto" | "on" | "off";
82478247
readonly provideRefactorNotApplicableReason?: boolean;
8248+
readonly includeInlineParameterName?: boolean;
8249+
readonly includeInlineFunctionParameterType?: boolean;
8250+
readonly includeInlineVariableType?: boolean;
82488251
}
82498252

82508253
/** Represents a bigint literal value without requiring bigint support */

src/harness/harnessLanguageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ namespace Harness.LanguageService {
599599
provideCallHierarchyOutgoingCalls(fileName: string, position: number) {
600600
return unwrapJSONCallResult(this.shim.provideCallHierarchyOutgoingCalls(fileName, position));
601601
}
602-
provideInlineHints(fileName: string, span: ts.TextSpan) {
603-
return unwrapJSONCallResult(this.shim.provideInlineHints(fileName, span));
602+
provideInlineHints(fileName: string, span: ts.TextSpan, preference: ts.InlineHintsOptions) {
603+
return unwrapJSONCallResult(this.shim.provideInlineHints(fileName, span, preference));
604604
}
605605
getEmitOutput(fileName: string): ts.EmitOutput {
606606
return unwrapJSONCallResult(this.shim.getEmitOutput(fileName));

src/server/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ namespace ts.server {
14241424
private provideInlineHints(args: protocol.ProvideInlineHintsRequestArgs) {
14251425
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
14261426
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!;
1427-
const hints = languageService.provideInlineHints(file, args);
1427+
const hints = languageService.provideInlineHints(file, args, this.getPreferences(file));
14281428

14291429
return hints.map(hint => ({
14301430
text: hint.text,

src/services/inlineHints.ts

+60-22
Original file line numberDiff line numberDiff line change
@@ -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
}

src/services/services.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2467,12 +2467,13 @@ namespace ts {
24672467
};
24682468
}
24692469

2470-
function getInlineHintsContext(file: SourceFile, span: TextSpan): InlineHintsContext {
2470+
function getInlineHintsContext(file: SourceFile, span: TextSpan, preferences: UserPreferences): InlineHintsContext {
24712471
return {
24722472
file,
24732473
program: getProgram()!,
24742474
host,
24752475
span,
2476+
preferences,
24762477
cancellationToken,
24772478
};
24782479
}
@@ -2520,10 +2521,10 @@ namespace ts {
25202521
return declaration ? CallHierarchy.getOutgoingCalls(program, declaration) : [];
25212522
}
25222523

2523-
function provideInlineHints(fileName: string, span: TextSpan): InlineHint[] {
2524+
function provideInlineHints(fileName: string, span: TextSpan, preferences: InlineHintsOptions = emptyOptions): InlineHint[] {
25242525
synchronizeHostData();
25252526
const sourceFile = getValidSourceFile(fileName);
2526-
return InlineHints.provideInlineHints(getInlineHintsContext(sourceFile, span));
2527+
return InlineHints.provideInlineHints(getInlineHintsContext(sourceFile, span, preferences));
25272528
}
25282529

25292530
const ls: LanguageService = {

src/services/shims.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ namespace ts {
280280
prepareCallHierarchy(fileName: string, position: number): string;
281281
provideCallHierarchyIncomingCalls(fileName: string, position: number): string;
282282
provideCallHierarchyOutgoingCalls(fileName: string, position: number): string;
283-
provideInlineHints(fileName: string, span: TextSpan): string;
283+
provideInlineHints(fileName: string, span: TextSpan, preference: InlineHintsOptions | undefined): string;
284284
getEmitOutput(fileName: string): string;
285285
getEmitOutputObject(fileName: string): EmitOutput;
286286

@@ -1067,10 +1067,10 @@ namespace ts {
10671067
);
10681068
}
10691069

1070-
public provideInlineHints(fileName: string, span: TextSpan): string {
1070+
public provideInlineHints(fileName: string, span: TextSpan, preference: InlineHintsOptions | undefined): string {
10711071
return this.forwardJSONCall(
1072-
`provideInlineHints('${fileName}', '${JSON.stringify(span)}')`,
1073-
() => this.languageService.provideInlineHints(fileName, span)
1072+
`provideInlineHints('${fileName}', '${JSON.stringify(span)}', ${JSON.stringify(preference)})`,
1073+
() => this.languageService.provideInlineHints(fileName, span, preference)
10741074
);
10751075
}
10761076

src/services/types.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ namespace ts {
481481
provideCallHierarchyIncomingCalls(fileName: string, position: number): CallHierarchyIncomingCall[];
482482
provideCallHierarchyOutgoingCalls(fileName: string, position: number): CallHierarchyOutgoingCall[];
483483

484-
provideInlineHints(fileName: string, span: TextSpan): InlineHint[]
484+
provideInlineHints(fileName: string, span: TextSpan, preferences: UserPreferences | undefined): InlineHint[]
485485

486486
getOutliningSpans(fileName: string): OutliningSpan[];
487487
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];
@@ -564,6 +564,12 @@ namespace ts {
564564
includeInsertTextCompletions?: boolean;
565565
}
566566

567+
export interface InlineHintsOptions extends UserPreferences {
568+
readonly includeInlineParameterName?: boolean;
569+
readonly includeInlineFunctionParameterType?: boolean,
570+
readonly includeInlineVariableType?: boolean;
571+
}
572+
567573
export type SignatureHelpTriggerCharacter = "," | "(" | "<";
568574
export type SignatureHelpRetriggerCharacter = SignatureHelpTriggerCharacter | ")";
569575

@@ -1497,5 +1503,6 @@ namespace ts {
14971503
cancellationToken: CancellationToken;
14981504
host: LanguageServiceHost;
14991505
span: TextSpan;
1506+
preferences: UserPreferences;
15001507
}
15011508
}

tests/baselines/reference/api/tsserverlibrary.d.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,9 @@ declare namespace ts {
38723872
readonly providePrefixAndSuffixTextForRename?: boolean;
38733873
readonly includePackageJsonAutoImports?: "auto" | "on" | "off";
38743874
readonly provideRefactorNotApplicableReason?: boolean;
3875+
readonly includeInlineParameterName?: boolean;
3876+
readonly includeInlineFunctionParameterType?: boolean;
3877+
readonly includeInlineVariableType?: boolean;
38753878
}
38763879
/** Represents a bigint literal value without requiring bigint support */
38773880
export interface PseudoBigInt {
@@ -5543,7 +5546,7 @@ declare namespace ts {
55435546
prepareCallHierarchy(fileName: string, position: number): CallHierarchyItem | CallHierarchyItem[] | undefined;
55445547
provideCallHierarchyIncomingCalls(fileName: string, position: number): CallHierarchyIncomingCall[];
55455548
provideCallHierarchyOutgoingCalls(fileName: string, position: number): CallHierarchyOutgoingCall[];
5546-
provideInlineHints(fileName: string, span: TextSpan): InlineHint[];
5549+
provideInlineHints(fileName: string, span: TextSpan, preferences: UserPreferences | undefined): InlineHint[];
55475550
getOutliningSpans(fileName: string): OutliningSpan[];
55485551
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];
55495552
getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[];
@@ -5603,6 +5606,11 @@ declare namespace ts {
56035606
/** @deprecated Use includeCompletionsWithInsertText */
56045607
includeInsertTextCompletions?: boolean;
56055608
}
5609+
interface InlineHintsOptions extends UserPreferences {
5610+
readonly includeInlineParameterName?: boolean;
5611+
readonly includeInlineFunctionParameterType?: boolean;
5612+
readonly includeInlineVariableType?: boolean;
5613+
}
56065614
type SignatureHelpTriggerCharacter = "," | "(" | "<";
56075615
type SignatureHelpRetriggerCharacter = SignatureHelpTriggerCharacter | ")";
56085616
interface SignatureHelpItemsOptions {
@@ -6329,6 +6337,7 @@ declare namespace ts {
63296337
cancellationToken: CancellationToken;
63306338
host: LanguageServiceHost;
63316339
span: TextSpan;
6340+
preferences: UserPreferences;
63326341
}
63336342
}
63346343
declare namespace ts {

tests/baselines/reference/api/typescript.d.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,9 @@ declare namespace ts {
38723872
readonly providePrefixAndSuffixTextForRename?: boolean;
38733873
readonly includePackageJsonAutoImports?: "auto" | "on" | "off";
38743874
readonly provideRefactorNotApplicableReason?: boolean;
3875+
readonly includeInlineParameterName?: boolean;
3876+
readonly includeInlineFunctionParameterType?: boolean;
3877+
readonly includeInlineVariableType?: boolean;
38753878
}
38763879
/** Represents a bigint literal value without requiring bigint support */
38773880
export interface PseudoBigInt {
@@ -5543,7 +5546,7 @@ declare namespace ts {
55435546
prepareCallHierarchy(fileName: string, position: number): CallHierarchyItem | CallHierarchyItem[] | undefined;
55445547
provideCallHierarchyIncomingCalls(fileName: string, position: number): CallHierarchyIncomingCall[];
55455548
provideCallHierarchyOutgoingCalls(fileName: string, position: number): CallHierarchyOutgoingCall[];
5546-
provideInlineHints(fileName: string, span: TextSpan): InlineHint[];
5549+
provideInlineHints(fileName: string, span: TextSpan, preferences: UserPreferences | undefined): InlineHint[];
55475550
getOutliningSpans(fileName: string): OutliningSpan[];
55485551
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];
55495552
getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[];
@@ -5603,6 +5606,11 @@ declare namespace ts {
56035606
/** @deprecated Use includeCompletionsWithInsertText */
56045607
includeInsertTextCompletions?: boolean;
56055608
}
5609+
interface InlineHintsOptions extends UserPreferences {
5610+
readonly includeInlineParameterName?: boolean;
5611+
readonly includeInlineFunctionParameterType?: boolean;
5612+
readonly includeInlineVariableType?: boolean;
5613+
}
56065614
type SignatureHelpTriggerCharacter = "," | "(" | "<";
56075615
type SignatureHelpRetriggerCharacter = SignatureHelpTriggerCharacter | ")";
56085616
interface SignatureHelpItemsOptions {
@@ -6329,6 +6337,7 @@ declare namespace ts {
63296337
cancellationToken: CancellationToken;
63306338
host: LanguageServiceHost;
63316339
span: TextSpan;
6340+
preferences: UserPreferences;
63326341
}
63336342
}
63346343
declare namespace ts {

0 commit comments

Comments
 (0)