Ejemplo n.º 1
0
        public override SyntaxNode VisitVariableDeclaration(VariableDeclarationSyntax node)
        {
            if (node.Parent is LocalDeclarationStatementSyntax localDeclarationStatement)
            {
                if (localDeclarationStatement.Modifiers.Any(SyntaxKind.ConstKeyword))
                {
                    return(base.VisitVariableDeclaration(node));
                }
            }
            else if (node.Parent is FieldDeclarationSyntax fieldDeclaration)
            {
                if (fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword))
                {
                    return(base.VisitVariableDeclaration(node));
                }
            }

            /*else if (node.Parent is PropertyDeclarationSyntax)
             * {
             *  var n = node.Parent as PropertyDeclarationSyntax;
             *  if (n.Modifiers.Any(SyntaxKind.ConstKeyword)) return base.VisitVariableDeclaration(node);
             * }*/
            if (node.Ancestors().Any(x => x is LambdaExpressionSyntax || x is ParenthesizedLambdaExpressionSyntax))
            {
                return(base.VisitVariableDeclaration(node));
            }
            if (_variableRewrites.TryGetValue(node, out int clusterID))
            {
                SyntaxNode n = node.WithType(SyntaxFactory.ParseTypeName("Cluster" + clusterID.ToString()));
                Rewrote = true;
                return(n);
            }

            return(base.VisitVariableDeclaration(node));
        }
        private async Task <Document> ExtractVariableDeclarationAsync(Document document, VariableDeclarationSyntax declaration, CancellationToken cancellationToken)
        {
            var oldClassNode = declaration.Ancestors(false).OfType <ClassDeclarationSyntax>().Single();

            _currentClassNode = oldClassNode;

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            _methodGenerator = new GenerateMethodFromStatementSyntaxWalker(semanticModel);
            _methodGenerator.VisitVariableDeclaration(declaration);

            RemoveVariableDeclarations();
            InsertExtractedMethods();
            ReplaceVariableCallsWithMethodCalls();

            var rootWithNewClass = document.GetSyntaxTreeAsync(cancellationToken).Result.GetRoot().ReplaceNode(oldClassNode, _currentClassNode);

            return(document.WithSyntaxRoot(rootWithNewClass));
        }