public static async Task <Solution> FixProperty(Document document, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var classParent = (ClassDeclarationSyntax)property.Parent; var field = AutoPropertyAnalyzer.PreviosField(classParent, property); var fieldVariable = field.Declaration.Variables.Single(); var semanticModel = await document.GetSemanticModelAsync(); var symbol = semanticModel.GetDeclaredSymbol(fieldVariable); var solution = document.Project.Solution; solution = await Renamer.RenameSymbolAsync(solution, symbol, property.Identifier.ToString(), solution.Workspace.Options, cancellationToken); document = solution.GetDocument(document.Id); var root = await document.GetSyntaxRootAsync(); classParent = root.DescendantNodes().OfType <ClassDeclarationSyntax>().SingleOrDefault(c => c.Identifier.IsEquivalentTo(classParent.Identifier)); field = classParent.Members.OfType <FieldDeclarationSyntax>().SingleOrDefault(f => f.Declaration.Variables.Any(v => v.Identifier.ToString() == property.Identifier.ToString())); fieldVariable = field.Declaration.Variables.Single(); var oldProperty = classParent.Members.OfType <PropertyDeclarationSyntax>().SingleOrDefault(a => a.Identifier.ToString() == property.Identifier.ToString()); var newProperty = SyntaxFactory.PropertyDeclaration( new SyntaxList <AttributeListSyntax>().AddRange(field.AttributeLists).AddRange(oldProperty.AttributeLists), oldProperty.Modifiers, oldProperty.Type, null, oldProperty.Identifier, SyntaxFactory.AccessorList(SyntaxFactory.List( property.AccessorList.Accessors.Select(a => a.WithBody(null).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))) ))); var leading = field.DescendantTokens().First().LeadingTrivia; var first = newProperty.DescendantTokens().First(); newProperty = newProperty.ReplaceToken(first, first.WithLeadingTrivia(leading)); if (fieldVariable.Initializer != null) { newProperty = newProperty.WithInitializer(fieldVariable.Initializer).WithSemicolonToken(field.SemicolonToken); } var members = classParent.Members.Replace(oldProperty, newProperty); members = members.Remove(members.Single(m => m.IsEquivalentTo(field))); var newClass = classParent.WithMembers(members); var docNode = await document.GetSyntaxRootAsync(cancellationToken); docNode = docNode.ReplaceNode(classParent, newClass); docNode = Formatter.Format(docNode, solution.Workspace); var resultSolution = solution.WithDocumentSyntaxRoot(document.Id, docNode); return(resultSolution); }
public static async Task <Solution> FixAllProperties(Document document, ClassDeclarationSyntax parentClass, CancellationToken cancellationToken) { var currentSolution = document.Project.Solution; var currentDocument = document; var currentClass = parentClass; while (true) { var property = currentClass.Members.OfType <PropertyDeclarationSyntax>().FirstOrDefault(p => AutoPropertyAnalyzer.IsSimpleProperty(p, currentClass)); if (property == null) { return(currentSolution); } try { currentSolution = await FixProperty(currentDocument, property, cancellationToken); currentDocument = currentSolution.GetDocument(currentDocument.Id); currentClass = (await currentDocument.GetSyntaxRootAsync()).DescendantNodes().OfType <ClassDeclarationSyntax>() .FirstOrDefault(a => a.Identifier.ToString() == currentClass.Identifier.ToString()); } catch (Exception) { return(currentSolution); } } }