コード例 #1
0
        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));
            }
        }