private static TypeDeclarationSyntax FindTypeDeclrationToReplace(CSharpSyntaxNode syntaxNode)
        {
            TypeDeclarationSyntax typeSyntax = syntaxNode.DescendantNodesAndSelf().OfType<ClassDeclarationSyntax>().FirstOrDefault();
            if (typeSyntax != null) return typeSyntax;

            typeSyntax = syntaxNode.DescendantNodesAndSelf().OfType<StructDeclarationSyntax>().FirstOrDefault();
            if (typeSyntax != null) return typeSyntax;

            return null;
        }
        public static CSharpSyntaxNode ReplaceClass(CSharpSyntaxNode syntaxNode)
        {
            IEnumerable<TypeDeclarationSyntax> allClassSyntaxes = syntaxNode.DescendantNodesAndSelf().OfType<ClassDeclarationSyntax>();
            IEnumerable<TypeDeclarationSyntax> allStructSyntaxes = syntaxNode.DescendantNodesAndSelf().OfType<StructDeclarationSyntax>();

            var newSyntaxNode = syntaxNode;
            TypeDeclarationSyntax currentSyntax;
            while((currentSyntax = FindTypeDeclrationToReplace(newSyntaxNode))!= null)
            {
                newSyntaxNode = newSyntaxNode.ReplaceNode(currentSyntax, MakeInterface(currentSyntax));
            }

            return newSyntaxNode;
        }
Beispiel #3
0
        public static CSharpSyntaxNode ReplaceClass(CSharpSyntaxNode syntaxNode)
        {
            IEnumerable <TypeDeclarationSyntax> allClassSyntaxes  = syntaxNode.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>();
            IEnumerable <TypeDeclarationSyntax> allStructSyntaxes = syntaxNode.DescendantNodesAndSelf().OfType <StructDeclarationSyntax>();

            var newSyntaxNode = syntaxNode;
            TypeDeclarationSyntax currentSyntax;

            while ((currentSyntax = FindTypeDeclrationToReplace(newSyntaxNode)) != null)
            {
                newSyntaxNode = newSyntaxNode.ReplaceNode(currentSyntax, MakeInterface(currentSyntax));
            }

            return(newSyntaxNode);
        }
Beispiel #4
0
        private static TypeDeclarationSyntax FindTypeDeclrationToReplace(CSharpSyntaxNode syntaxNode)
        {
            TypeDeclarationSyntax typeSyntax = syntaxNode.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().FirstOrDefault();

            if (typeSyntax != null)
            {
                return(typeSyntax);
            }

            typeSyntax = syntaxNode.DescendantNodesAndSelf().OfType <StructDeclarationSyntax>().FirstOrDefault();
            if (typeSyntax != null)
            {
                return(typeSyntax);
            }

            return(null);
        }
        private IEnumerable <ISymbol> GetSymbolUsages(CSharpSyntaxNode node,
                                                      IEnumerable <ISymbol> symbols, SemanticModel semanticModel, IEnumerable <SyntaxNode> nodesToSkip)
        {
            var symbolsSet     = (symbols as IImmutableSet <ISymbol>) ?? symbols.ToImmutableHashSet();
            var nodesToSkipSet = (nodesToSkip as IImmutableSet <SyntaxNode>) ?? nodesToSkip.ToImmutableHashSet();

            foreach (var subNode in node.DescendantNodesAndSelf()
                     .Where(n => !nodesToSkipSet.Contains(n)))
            {
                var symbol = semanticModel.GetSymbolInfo(subNode).Symbol;

                if (symbol != null && symbolsSet.Contains(symbol))
                {
                    yield return(symbol);
                }
            }
        }
Beispiel #6
0
        public static CSharpSyntaxNode AddOptional(CSharpSyntaxNode syntaxNode)
        {
            var interfaces = syntaxNode.DescendantNodesAndSelf().Where(f => f is InterfaceDeclarationSyntax);

            var properties = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is PropertyDeclarationSyntax));
            var methods    = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is MethodDeclarationSyntax));

            return(syntaxNode.ReplaceNodes(properties.Concat(methods), (node, node2) =>
            {
                var property = node as PropertyDeclarationSyntax;
                var method = node as MethodDeclarationSyntax;
                if (property != null)
                {
                    return property.WithIdentifier(SyntaxFactory.Identifier(property.Identifier.ValueText + "?"));
                }

                return method.WithIdentifier(SyntaxFactory.Identifier(method.Identifier.ValueText + "?"));
            }));
        }
        public static CSharpSyntaxNode AddOptional(CSharpSyntaxNode syntaxNode)
        {
            var interfaces = syntaxNode.DescendantNodesAndSelf().Where(f => f is InterfaceDeclarationSyntax);

            var properties = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is PropertyDeclarationSyntax));
            var methods = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is MethodDeclarationSyntax));

            return syntaxNode.ReplaceNodes(properties.Concat(methods), (node, node2) =>
            {
                var property = node as PropertyDeclarationSyntax;
                var method = node as MethodDeclarationSyntax;
                if (property != null)
                {
                    return property.WithIdentifier(SyntaxFactory.Identifier(property.Identifier.ValueText + "?"));
                }

                return method.WithIdentifier(SyntaxFactory.Identifier(method.Identifier.ValueText + "?"));
            });
        }
Beispiel #8
0
 public CSharpSyntaxNode Find(CSharpSyntaxNode root)
 {
     return(root.DescendantNodesAndSelf()
            .OfType <ClassDeclarationSyntax>()
            .FirstOrDefault(n => n.Identifier.Text == Name));
 }