Skip to content

Commit

Permalink
Display hints in single line
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl committed Dec 25, 2020
1 parent 679b58d commit 446bee4
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/services/inlineHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace ts.InlineHints {
}

const initializerType = checker.getTypeAtLocation(decl.initializer);
const typeDisplayString = displayPartsToString(typeToDisplayParts(checker, initializerType));
const typeDisplayString = printTypeInSingleLine(initializerType);
if (typeDisplayString) {
addTypeHints(typeDisplayString, decl.name.end);
}
Expand Down Expand Up @@ -143,11 +143,11 @@ namespace ts.InlineHints {
}

if (valueDeclaration.type) {
return valueDeclaration.type.getText();
return printNodeInSingleLine(valueDeclaration.type);
}

const signatureParamType = checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration);
return displayPartsToString(typeToDisplayParts(checker, signatureParamType));
return printTypeInSingleLine(signatureParamType);
}

function truncation(text: string, maxLength: number) {
Expand All @@ -156,5 +156,38 @@ namespace ts.InlineHints {
}
return text;
}

function createSignleLineWriter(writer: DisplayPartsSymbolWriter): DisplayPartsSymbolWriter {
return {
...writer,
writeLine: () => writer.writeSpace(" ")
};
}

function printTypeInSingleLine(type: Type) {
const flags = NodeBuilderFlags.IgnoreErrors | TypeFormatFlags.AllowUniqueESSymbolType | TypeFormatFlags.UseAliasDefinedOutsideCurrentScope;
const displayParts = mapToDisplayParts(writer => {
const singleLineWriter = createSignleLineWriter(writer);
const typeNode = checker.typeToTypeNode(type, /*enclosingDeclaration*/ undefined, flags, singleLineWriter);
Debug.assertIsDefined(typeNode, "should always get typenode");

writeNodeInSignleLine(typeNode, singleLineWriter);
});
return displayPartsToString(displayParts);
}

function printNodeInSingleLine(node: Node) {
const displayParts = mapToDisplayParts(writer => {
const singleLineWriter = createSignleLineWriter(writer);
writeNodeInSignleLine(node, singleLineWriter);
});
return displayPartsToString(displayParts);
}

function writeNodeInSignleLine(node: Node, writer: DisplayPartsSymbolWriter) {
const options: PrinterOptions = { removeComments: true };
const printer = createPrinter(options);
printer.writeNode(EmitHint.Unspecified, node, /*sourceFile*/ file, writer);
}
}
}

0 comments on commit 446bee4

Please sign in to comment.