private static SyntaxNode CalculateNewRoot(SyntaxNode root, ForEachStatementSyntax foreachSyntax, SemanticModel semanticModel)
        {
            var collection = foreachSyntax.Expression;
            var typeName = foreachSyntax.Type.ToString();
            var invocationToAdd = GetOfTypeInvocation(typeName, collection);
            var namedTypes = semanticModel.LookupNamespacesAndTypes(foreachSyntax.SpanStart).OfType<INamedTypeSymbol>();
            var isUsingAlreadyThere = namedTypes.Any(nt => nt.ToDisplayString() == ofTypeExtensionClass);

            if (isUsingAlreadyThere)
            {
                return root
                    .ReplaceNode(collection, invocationToAdd)
                    .WithAdditionalAnnotations(Formatter.Annotation);
            }
            else
            {
                var usingDirectiveToAdd = SyntaxFactory.UsingDirective(
                    SyntaxFactory.QualifiedName(
                        SyntaxFactory.IdentifierName("System"),
                        SyntaxFactory.IdentifierName("Linq")));

                var annotation = new SyntaxAnnotation("CollectionToChange");
                var newRoot = root.ReplaceNode(
                    collection,
                    collection.WithAdditionalAnnotations(annotation));

                var node = newRoot.GetAnnotatedNodes(annotation).First();
                var closestNamespaceWithUsing = node.AncestorsAndSelf()
                    .OfType<NamespaceDeclarationSyntax>()
                    .FirstOrDefault(n => n.Usings.Count > 0);

                if (closestNamespaceWithUsing != null)
                {
                    newRoot = newRoot.ReplaceNode(
                        closestNamespaceWithUsing,
                        closestNamespaceWithUsing.AddUsings(usingDirectiveToAdd))
                        .WithAdditionalAnnotations(Formatter.Annotation);
                }
                else
                {
                    var compilationUnit = node.FirstAncestorOrSelf<CompilationUnitSyntax>();
                    newRoot = compilationUnit.AddUsings(usingDirectiveToAdd);
                }

                node = newRoot.GetAnnotatedNodes(annotation).First();
                return newRoot
                    .ReplaceNode(node, invocationToAdd)
                    .WithAdditionalAnnotations(Formatter.Annotation);
            }
        }
        public override string GetFullyQualifiedName(string name, int position, SemanticModel semanticModel)
        {
            var typeName = SyntaxFactory.ParseTypeName(name);
            if (typeName is PredefinedTypeSyntax)
            {
                PredefinedType predefinedType;
                if (SyntaxFactsService.TryGetPredefinedType(((PredefinedTypeSyntax)typeName).Keyword, out predefinedType))
                {
                    var specialType = predefinedType.ToSpecialType();
                    return semanticModel.Compilation.GetSpecialType(specialType).GetEscapedFullName();
                }
            }
            else
            {
                var symbols = semanticModel.LookupNamespacesAndTypes(position, name: name);
                if (symbols.Length > 0)
                {
                    return symbols[0].GetEscapedFullName();
                }
            }

            return name;
        }
 private static bool NamespaceNeedsToBeAdded(MethodDeclarationSyntax method,
     SemanticModel semanticModel, string LiteralNotSupportedException, string LiteralSystem)
 {
     return !semanticModel.LookupNamespacesAndTypes(method.Body.CloseBraceToken.SpanStart)
         .OfType<INamedTypeSymbol>()
         .Any(nt =>
             nt.IsType &&
             nt.Name == LiteralNotSupportedException &&
             nt.ContainingNamespace.Name == LiteralSystem);
 }
Esempio n. 4
0
		public void AddImportCompletionData (CompletionResult result, Document document, SemanticModel semanticModel, int position, CancellationToken cancellationToken = default(CancellationToken))
		{
			var ns = new Stack<INamespaceOrTypeSymbol>();
			ns.Push(semanticModel.Compilation.GlobalNamespace);
			
			semanticModel.LookupNamespacesAndTypes(position);
		}
        //
        //		private static bool TryReplaceWithAlias(this ExpressionSyntax node, SemanticModel semanticModel, bool preferAliasToQualifiedName, CancellationToken cancellationToken, out IAliasSymbol aliasReplacement)
        //		{
        //			aliasReplacement = null;
        //
        //			if (!node.IsAliasReplaceableExpression())
        //			{
        //				return false;
        //			}
        //
        //			var symbol = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol;
        //
        //			// If the Symbol is a contrcutor get its containing type
        //			if (symbol.IsConstructor())
        //			{
        //				symbol = symbol.ContainingType;
        //			}
        //
        //			if (node is QualifiedNameSyntax || node is AliasQualifiedNameSyntax)
        //			{
        //				SyntaxAnnotation aliasAnnotationInfo = null;
        //
        //				// The following condition checks if the user has used alias in the original code and
        //				// if so the expression is replaced with the Alias
        //				if (node is QualifiedNameSyntax)
        //				{
        //					var qualifiedNameNode = (QualifiedNameSyntax)node;
        //					if (qualifiedNameNode.Right.Identifier.HasAnnotations(AliasAnnotation.Kind))
        //					{
        //						aliasAnnotationInfo = qualifiedNameNode.Right.Identifier.GetAnnotations(AliasAnnotation.Kind).Single();
        //					}
        //				}
        //
        //				if (node is AliasQualifiedNameSyntax)
        //				{
        //					var aliasQualifiedNameNode = (AliasQualifiedNameSyntax)node;
        //					if (aliasQualifiedNameNode.Name.Identifier.HasAnnotations(AliasAnnotation.Kind))
        //					{
        //						aliasAnnotationInfo = aliasQualifiedNameNode.Name.Identifier.GetAnnotations(AliasAnnotation.Kind).Single();
        //					}
        //				}
        //
        //				if (aliasAnnotationInfo != null)
        //				{
        //					var aliasName = AliasAnnotation.GetAliasName(aliasAnnotationInfo);
        //					var aliasIdentifier = SyntaxFactory.IdentifierName(aliasName);
        //
        //					var aliasTypeInfo = semanticModel.GetSpeculativeAliasInfo(node.SpanStart, aliasIdentifier, SpeculativeBindingOption.BindAsTypeOrNamespace);
        //
        //					if (aliasTypeInfo != null)
        //					{
        //						aliasReplacement = aliasTypeInfo;
        //						return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol);
        //					}
        //				}
        //			}
        //
        //			if (node.Kind() == SyntaxKind.IdentifierName &&
        //				semanticModel.GetAliasInfo((IdentifierNameSyntax)node, cancellationToken) != null)
        //			{
        //				return false;
        //			}
        //
        //			// an alias can only replace a type or namespace
        //			if (symbol == null ||
        //				(symbol.Kind != SymbolKind.Namespace && symbol.Kind != SymbolKind.NamedType))
        //			{
        //				return false;
        //			}
        //
        //			if (node is QualifiedNameSyntax)
        //			{
        //				var qualifiedName = (QualifiedNameSyntax)node;
        //				if (!qualifiedName.Right.HasAnnotation(Simplifier.SpecialTypeAnnotation))
        //				{
        //					var type = semanticModel.GetTypeInfo(node, cancellationToken).Type;
        //					if (type != null)
        //					{
        //						var keywordKind = GetPredefinedKeywordKind(type.SpecialType);
        //						if (keywordKind != SyntaxKind.None)
        //						{
        //							preferAliasToQualifiedName = false;
        //						}
        //					}
        //				}
        //			}
        //
        //			if (node is AliasQualifiedNameSyntax)
        //			{
        //				var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)node;
        //				if (!aliasQualifiedNameSyntax.Name.HasAnnotation(Simplifier.SpecialTypeAnnotation))
        //				{
        //					var type = semanticModel.GetTypeInfo(node, cancellationToken).Type;
        //					if (type != null)
        //					{
        //						var keywordKind = GetPredefinedKeywordKind(type.SpecialType);
        //						if (keywordKind != SyntaxKind.None)
        //						{
        //							preferAliasToQualifiedName = false;
        //						}
        //					}
        //				}
        //			}
        //
        //			aliasReplacement = GetAliasForSymbol((INamespaceOrTypeSymbol)symbol, node.GetFirstToken(), semanticModel, cancellationToken);
        //			if (aliasReplacement != null && preferAliasToQualifiedName)
        //			{
        //				return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol);
        //			}
        //
        //			return false;
        //		}
        //
        // We must verify that the alias actually binds back to the thing it's aliasing.
        // It's possible there's another symbol with the same name as the alias that binds
        // first
        private static bool ValidateAliasForTarget(IAliasSymbol aliasReplacement, SemanticModel semanticModel, ExpressionSyntax node, ISymbol symbol)
        {
            var aliasName = aliasReplacement.Name;

            var boundSymbols = semanticModel.LookupNamespacesAndTypes(node.SpanStart, name: aliasName);

            if (boundSymbols.Length == 1)
            {
                var boundAlias = boundSymbols[0] as IAliasSymbol;
                if (boundAlias != null && aliasReplacement.Target.Equals(symbol))
                {
                    return true;
                }
            }

            return false;
        }
 private int LookupSymbol(SemanticModel model, int pos, INamespaceOrTypeSymbol container, string symbol)
 {
     Console.WriteLine("lookup: " + symbol);
     //var symbols = model.LookupSymbols(0, container, symbol);
     var symbols = model.LookupNamespacesAndTypes(pos, container, symbol);
     foreach (var x in symbols)
         Console.WriteLine(x.ToDisplayString());
     return symbols.Length;
 }