Skip to content

Commit

Permalink
add more information to quickinfo for imported symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Jan 4, 2018
1 parent e3a20e7 commit 56fc112
Show file tree
Hide file tree
Showing 49 changed files with 958 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal/
!tests/baselines/reference/project/nodeModules*/**/*
.idea
yarn.lock
yarn-error.log
.parallelperf.*
tests/cases/user/*/package-lock.json
tests/cases/user/*/node_modules/
Expand Down
66 changes: 54 additions & 12 deletions src/services/symbolDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace ts.SymbolDisplay {

// TODO(drosen): Currently completion entry details passes the SemanticMeaning.All instead of using semanticMeaning of location
export function getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker: TypeChecker, symbol: Symbol, sourceFile: SourceFile, enclosingDeclaration: Node,
location: Node, semanticMeaning = getMeaningFromLocation(location)): SymbolDisplayPartsDocumentationAndSymbolKind {
location: Node, semanticMeaning = getMeaningFromLocation(location), alias?: Symbol): SymbolDisplayPartsDocumentationAndSymbolKind {

const displayParts: SymbolDisplayPart[] = [];
let documentation: SymbolDisplayPart[];
Expand All @@ -115,6 +115,7 @@ namespace ts.SymbolDisplay {
let hasAddedSymbolInfo: boolean;
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isExpression(location);
let type: Type;
let documentationFromAlias: SymbolDisplayPart[];

// Class at constructor site need to be shown as constructor apart from property,method, vars
if (symbolKind !== ScriptElementKind.unknown || symbolFlags & SymbolFlags.Class || symbolFlags & SymbolFlags.Alias) {
Expand Down Expand Up @@ -243,6 +244,7 @@ namespace ts.SymbolDisplay {
}
}
if (symbolFlags & SymbolFlags.Class && !hasAddedSymbolInfo && !isThisExpression) {
addAliasPrefixIfNecessary();
if (getDeclarationOfKind(symbol, SyntaxKind.ClassExpression)) {
// Special case for class expressions because we would like to indicate that
// the class name is local to the class body (similar to function expression)
Expand All @@ -258,14 +260,14 @@ namespace ts.SymbolDisplay {
writeTypeParametersOfSymbol(symbol, sourceFile);
}
if ((symbolFlags & SymbolFlags.Interface) && (semanticMeaning & SemanticMeaning.Type)) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
displayParts.push(keywordPart(SyntaxKind.InterfaceKeyword));
displayParts.push(spacePart());
addFullSymbolName(symbol);
writeTypeParametersOfSymbol(symbol, sourceFile);
}
if (symbolFlags & SymbolFlags.TypeAlias) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
displayParts.push(keywordPart(SyntaxKind.TypeKeyword));
displayParts.push(spacePart());
addFullSymbolName(symbol);
Expand All @@ -276,7 +278,7 @@ namespace ts.SymbolDisplay {
addRange(displayParts, typeToDisplayParts(typeChecker, typeChecker.getDeclaredTypeOfSymbol(symbol), enclosingDeclaration, TypeFormatFlags.InTypeAlias));
}
if (symbolFlags & SymbolFlags.Enum) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
if (forEach(symbol.declarations, isConstEnumDeclaration)) {
displayParts.push(keywordPart(SyntaxKind.ConstKeyword));
displayParts.push(spacePart());
Expand All @@ -286,15 +288,15 @@ namespace ts.SymbolDisplay {
addFullSymbolName(symbol);
}
if (symbolFlags & SymbolFlags.Module) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
const declaration = getDeclarationOfKind<ModuleDeclaration>(symbol, SyntaxKind.ModuleDeclaration);
const isNamespace = declaration && declaration.name && declaration.name.kind === SyntaxKind.Identifier;
displayParts.push(keywordPart(isNamespace ? SyntaxKind.NamespaceKeyword : SyntaxKind.ModuleKeyword));
displayParts.push(spacePart());
addFullSymbolName(symbol);
}
if ((symbolFlags & SymbolFlags.TypeParameter) && (semanticMeaning & SemanticMeaning.Type)) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
displayParts.push(punctuationPart(SyntaxKind.OpenParenToken));
displayParts.push(textPart("type parameter"));
displayParts.push(punctuationPart(SyntaxKind.CloseParenToken));
Expand Down Expand Up @@ -354,7 +356,32 @@ namespace ts.SymbolDisplay {
}
}
if (symbolFlags & SymbolFlags.Alias) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
if (!hasAddedSymbolInfo) {
const resolvedSymbol = typeChecker.getAliasedSymbol(symbol);
if (resolvedSymbol !== symbol && resolvedSymbol.declarations && resolvedSymbol.declarations.length > 0) {
const resolvedNode = resolvedSymbol.declarations[0];
const declarationName = ts.getNameOfDeclaration(resolvedNode);
if (declarationName) {
const isExternalModuleDeclaration =
ts.isModuleWithStringLiteralName(resolvedNode) &&
ts.hasModifier(resolvedNode, ModifierFlags.Ambient);
const shouldUseAliasName = symbol.name !== "default" && !isExternalModuleDeclaration;
const resolvedInfo = getSymbolDisplayPartsDocumentationAndSymbolKind(
typeChecker,
resolvedSymbol,
ts.getSourceFileOfNode(resolvedNode),
resolvedNode,
declarationName,
semanticMeaning,
shouldUseAliasName ? symbol : resolvedSymbol);
displayParts.push(...resolvedInfo.displayParts);
displayParts.push(lineBreakPart());
documentationFromAlias = resolvedInfo.documentation;
}
}
}

switch (symbol.declarations[0].kind) {
case SyntaxKind.NamespaceExportDeclaration:
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
Expand Down Expand Up @@ -400,7 +427,7 @@ namespace ts.SymbolDisplay {
if (symbolKind !== ScriptElementKind.unknown) {
if (type) {
if (isThisExpression) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
displayParts.push(keywordPart(SyntaxKind.ThisKeyword));
}
else {
Expand Down Expand Up @@ -472,12 +499,24 @@ namespace ts.SymbolDisplay {
}
}

if (documentation.length === 0 && documentationFromAlias) {
documentation = documentationFromAlias;
}

return { displayParts, documentation, symbolKind, tags };

function addNewLineIfDisplayPartsExist() {
function prefixNextMeaning() {
if (displayParts.length) {
displayParts.push(lineBreakPart());
}
addAliasPrefixIfNecessary();
}

function addAliasPrefixIfNecessary() {
if (alias) {
pushTypePart(ScriptElementKind.alias);
displayParts.push(spacePart());
}
}

function addInPrefix() {
Expand All @@ -486,14 +525,17 @@ namespace ts.SymbolDisplay {
displayParts.push(spacePart());
}

function addFullSymbolName(symbol: Symbol, enclosingDeclaration?: Node) {
const fullSymbolDisplayParts = symbolToDisplayParts(typeChecker, symbol, enclosingDeclaration || sourceFile, /*meaning*/ undefined,
function addFullSymbolName(symbolToDisplay: Symbol, enclosingDeclaration?: Node) {
if (alias && symbolToDisplay === symbol) {
symbolToDisplay = alias;
}
const fullSymbolDisplayParts = symbolToDisplayParts(typeChecker, symbolToDisplay, enclosingDeclaration || sourceFile, /*meaning*/ undefined,
SymbolFormatFlags.WriteTypeParametersOrArguments | SymbolFormatFlags.UseOnlyExternalAliasing);
addRange(displayParts, fullSymbolDisplayParts);
}

function addPrefixForAnyFunctionOrVar(symbol: Symbol, symbolKind: string) {
addNewLineIfDisplayPartsExist();
prefixNextMeaning();
if (symbolKind) {
pushTypePart(symbolKind);
if (symbol && !some(symbol.declarations, d => isArrowFunction(d) || (isFunctionExpression(d) || isClassExpression(d)) && !d.name)) {
Expand Down
Loading

0 comments on commit 56fc112

Please sign in to comment.