private UsingDirectiveSyntax QualifyUsingDirective(UsingDirectiveSyntax usingDirective) { NameSyntax originalName = usingDirective.Name; NameSyntax rewrittenName; switch (originalName.Kind()) { case SyntaxKind.QualifiedName: case SyntaxKind.IdentifierName: case SyntaxKind.GenericName: if (originalName.Parent.IsKind(SyntaxKind.UsingDirective) || originalName.Parent.IsKind(SyntaxKind.TypeArgumentList)) { var symbol = this.semanticModel.GetSymbolInfo(originalName, cancellationToken: CancellationToken.None).Symbol; if (symbol == null) { rewrittenName = originalName; break; } if (symbol is INamespaceSymbol) { // TODO: Preserve inner trivia string fullName = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); NameSyntax replacement = SyntaxFactory.ParseName(fullName); if (!originalName.DescendantNodesAndSelf().OfType <AliasQualifiedNameSyntax>().Any()) { replacement = replacement.ReplaceNodes( replacement.DescendantNodesAndSelf().OfType <AliasQualifiedNameSyntax>(), (originalNode2, rewrittenNode2) => rewrittenNode2.Name); } rewrittenName = replacement.WithTriviaFrom(originalName); break; } else if (symbol is INamedTypeSymbol) { // TODO: Preserve inner trivia // TODO: simplify after qualification string fullName; if (SpecialTypeHelper.IsPredefinedType(((INamedTypeSymbol)symbol).OriginalDefinition.SpecialType)) { fullName = "global::System." + symbol.Name; } else { fullName = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } NameSyntax replacement = SyntaxFactory.ParseName(fullName); if (!originalName.DescendantNodesAndSelf().OfType <AliasQualifiedNameSyntax>().Any()) { replacement = replacement.ReplaceNodes( replacement.DescendantNodesAndSelf().OfType <AliasQualifiedNameSyntax>(), (originalNode2, rewrittenNode2) => rewrittenNode2.Name); } rewrittenName = replacement.WithTriviaFrom(originalName); break; } else { rewrittenName = originalName; break; } } else { rewrittenName = originalName; break; } case SyntaxKind.AliasQualifiedName: case SyntaxKind.PredefinedType: default: rewrittenName = originalName; break; } if (rewrittenName == originalName) { return(usingDirective); } return(usingDirective.ReplaceNode(originalName, rewrittenName)); }