Exemplo n.º 1
0
        public SyntaxNode link(SyntaxNode node, out Compilation result)
        {
            var           tree  = node.SyntaxTree;
            SemanticModel model = compilation_.GetSemanticModel(node.SyntaxTree);
            node = node.ReplaceNodes(node.GetAnnotatedNodes(Compiler.LinkerAnnotationId), (oldNode, newNode) =>
            {
                var annotation = oldNode.GetAnnotations(Compiler.LinkerAnnotationId).First();
                if (oldNode.SyntaxTree != model.SyntaxTree)
                {
                    oldNode = model.SyntaxTree.GetRoot().GetAnnotatedNodes(Compiler.LinkerAnnotationId).
                                Where(i => i.GetAnnotations(Compiler.LinkerAnnotationId).First().Data == annotation.Data).First();
                }

                return ctx_.Link(oldNode, newNode, annotation.Data, model);
            });

            result = compilation_.ReplaceSyntaxTree(tree, node.SyntaxTree);
            model  = result.GetSemanticModel(node.SyntaxTree);
            return ctx_.ApplyLinkerInfo(node, model, result, out result);
        }
        private static SyntaxNode StripMultipleBlankLines(SyntaxNode syntaxRoot)
        {
            var replaceMap = new Dictionary<SyntaxToken, SyntaxToken>();

            var usingDirectives = syntaxRoot.GetAnnotatedNodes(UsingCodeFixAnnotation).Cast<UsingDirectiveSyntax>();

            foreach (var usingDirective in usingDirectives)
            {
                var nextToken = usingDirective.SemicolonToken.GetNextToken(true);

                // start at -1 to compensate for the always present end-of-line.
                var count = -1;

                // count the blanks lines at the end of the using statement.
                foreach (var trivia in usingDirective.SemicolonToken.TrailingTrivia.Reverse())
                {
                    if (!trivia.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        break;
                    }

                    count++;
                }

                // count the blank lines at the start of the next token
                foreach (var trivia in nextToken.LeadingTrivia)
                {
                    if (!trivia.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        break;
                    }

                    count++;
                }

                if (count > 1)
                {
                    replaceMap[nextToken] = nextToken.WithLeadingTrivia(nextToken.LeadingTrivia.Skip(count - 1));
                }
            }

            var newSyntaxRoot = syntaxRoot.ReplaceTokens(replaceMap.Keys, (original, rewritten) => replaceMap[original]);
            return newSyntaxRoot;
        }
        private Document PerformAction(Document document, SemanticModel model, SyntaxNode root, String name,
            PropertyDeclarationSyntax newProperty, SyntaxAnnotation propAnno, SyntaxAnnotation fieldAnno)
        {
            var oldField = root.GetAnnotatedNodes(fieldAnno).First() as FieldDeclarationSyntax;
            if (oldField.Declaration.Variables.Count == 1)
            {
                var newRoot = root.RemoveNode(oldField, SyntaxRemoveOptions.KeepNoTrivia);
                var oldProperty = newRoot.GetAnnotatedNodes(propAnno).First();
                newRoot = newRoot.ReplaceNode((SyntaxNode)oldProperty, newProperty);

                return document.WithSyntaxRoot(newRoot);
            }
            else
            {
                FieldDeclarationSyntax newField = oldField.WithDeclaration(SyntaxFactory.VariableDeclaration(oldField.Declaration.Type));
                //need to replace the field with one missing the variable field
                foreach (var variable in oldField.Declaration.Variables)
                {
                    if (!variable.Identifier.ValueText.Equals(name))
                        newField = newField.AddDeclarationVariables(variable);
                }
                var newRoot = root.ReplaceNode((SyntaxNode)oldField, newField.WithAdditionalAnnotations(Formatter.Annotation).WithLeadingTrivia(oldField.GetLeadingTrivia()));
                var oldProperty = newRoot.GetAnnotatedNodes(propAnno).First();
                newRoot = newRoot.ReplaceNode((SyntaxNode)oldProperty, newProperty.WithAdditionalAnnotations(Formatter.Annotation).WithLeadingTrivia(oldProperty.GetLeadingTrivia()));
                return document.WithSyntaxRoot(newRoot);
            }
        }
 private static ConstructorDeclarationSyntax GetConstructor(SyntaxNode newRoot, SyntaxAnnotation annotation)
 {
     return (ConstructorDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
 }