public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First(); var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression); var returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol; var variableDeclarator = (VariableDeclaratorSyntax)returnIdentifierSymbol.DeclaringSyntaxReferences.First().GetSyntax(); var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>(); root = root.TrackNodes(returnIdentifier, fieldDeclaration, property); document = document.WithSyntaxRoot(root); root = await document.GetSyntaxRootAsync(cancellationToken); semanticModel = await document.GetSemanticModelAsync(cancellationToken); returnIdentifier = root.GetCurrentNode(returnIdentifier); returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol; var newProperty = GetSimpleProperty(property, variableDeclarator) .WithTriviaFrom(property) .WithAdditionalAnnotations(Formatter.Annotation); var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, returnIdentifierSymbol, property.Identifier.ValueText, document.Project.Solution.Workspace.Options, cancellationToken); document = newSolution.GetDocument(document.Id); root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); root = root.InsertNodesAfter(root.GetCurrentNode(property), new[] { newProperty }); var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1; if (multipleVariableDeclaration) { var newfieldDeclaration = fieldDeclaration.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia)); root = root.RemoveNode(root.GetCurrentNode<SyntaxNode>(property), SyntaxRemoveOptions.KeepNoTrivia); root = root.ReplaceNode(root.GetCurrentNode(fieldDeclaration), newfieldDeclaration); } else { root = root.RemoveNodes(root.GetCurrentNodes<SyntaxNode>(new SyntaxNode[] { fieldDeclaration, property }), SyntaxRemoveOptions.KeepNoTrivia); } document = document.WithSyntaxRoot(root); return document.Project.Solution; }
internal static SyntaxNode AddStatementToConstructorBody(SyntaxNode root, ConstructorDeclarationSyntax constructor, StatementSyntax statement) { var body = constructor.Body ?? SyntaxFactory.Block(); return root.ReplaceNode(root.GetCurrentNode(constructor), constructor.WithBody( body.WithStatements(SyntaxFactory.List(new[] { statement }.Concat(body.Statements))) )); }
private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyntax member, MemberDeclarationSyntax targetMember, IndentationOptions indentationOptions) { var firstToken = syntaxRoot.GetFirstToken(); var fileHeader = GetFileHeader(firstToken.LeadingTrivia); syntaxRoot = syntaxRoot.TrackNodes(member, targetMember, firstToken.Parent); var memberToMove = syntaxRoot.GetCurrentNode(member); var targetMemberTracked = syntaxRoot.GetCurrentNode(targetMember); if (!memberToMove.HasLeadingTrivia) { var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, targetMember); var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, targetIndentationLevel); memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString)); } if (!HasLeadingBlankLines(targetMember) && HasLeadingBlankLines(member)) { memberToMove = memberToMove.WithTrailingTrivia(memberToMove.GetTrailingTrivia().Add(SyntaxFactory.CarriageReturnLineFeed)); memberToMove = memberToMove.WithLeadingTrivia(GetLeadingTriviaWithoutLeadingBlankLines(memberToMove)); } syntaxRoot = syntaxRoot.InsertNodesBefore(targetMemberTracked, new[] { memberToMove }); var fieldToMoveTracked = syntaxRoot.GetCurrentNodes(member).Last(); syntaxRoot = syntaxRoot.RemoveNode(fieldToMoveTracked, SyntaxRemoveOptions.KeepNoTrivia); if (fileHeader.Any()) { var oldFirstToken = syntaxRoot.GetCurrentNode(firstToken.Parent).ChildTokens().First(); syntaxRoot = syntaxRoot.ReplaceToken(oldFirstToken, oldFirstToken.WithLeadingTrivia(StripFileHeader(oldFirstToken.LeadingTrivia))); var newFirstToken = syntaxRoot.GetFirstToken(); syntaxRoot = syntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader.AddRange(newFirstToken.LeadingTrivia))); } return syntaxRoot; }
private static SyntaxNode RemoveField(SyntaxNode root, VariableDeclaratorSyntax variableDeclarator, FieldDeclarationSyntax fieldDeclaration) { var currentField = root.GetCurrentNode(fieldDeclaration); var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1; root = multipleVariableDeclaration ? root.ReplaceNode(currentField, fieldDeclaration .WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia))) : root.RemoveNode(currentField, SyntaxRemoveOptions.KeepNoTrivia); return root; }
internal static SyntaxNode AddBefore(SyntaxNode root, SyntaxNode loationToAddBefore, SyntaxNode nodeToAdd) { return root.InsertNodesBefore( root.GetCurrentNode(loationToAddBefore) , new List<SyntaxNode> { nodeToAdd }); }
private static decimal ProcessObjectInitializer(ObjectCreationExpressionSyntax target, decimal lastTemporaryIndex, ref SyntaxNode newRoot, ref SyntaxNode[] originalObjectExpressionSyntax, ref IEnumerable<KeyValuePair<SyntaxNode, SyntaxNode>> replacedNodes, int depth = 0) { depth++; var objectCreation = target; if (objectCreation.Initializer.IsKind(SyntaxKind.ObjectInitializerExpression)) { var initializerInfo = GetModel(target).GetTypeInfo(target); var tempName = initializerInfo.Type.Name + "_helper_" + lastTemporaryIndex++; var assignments = new List<SyntaxNode>(); foreach (var expression in objectCreation.Initializer.Expressions) { var localExpression = (expression as AssignmentExpressionSyntax); if (localExpression == null) continue; var right = localExpression.Right; var targets = expression.DescendantNodes() .OfType<ObjectCreationExpressionSyntax>() .Where(k => k.Ancestors().OfType<ObjectCreationExpressionSyntax>().First() == target); var objectCreationExpressionSyntaxs = targets as ObjectCreationExpressionSyntax[] ?? targets.ToArray(); var noriginalObjectExpressionSyntax = objectCreationExpressionSyntaxs.ToArray<SyntaxNode>().ToArray(); foreach (var ntarget in noriginalObjectExpressionSyntax) { lastTemporaryIndex = ProcessObjectInitializer((ObjectCreationExpressionSyntax)ntarget, lastTemporaryIndex, ref newRoot, ref originalObjectExpressionSyntax, ref replacedNodes, depth); } if (noriginalObjectExpressionSyntax.Any()) { bool contained; try { newRoot.GetCurrentNode(right); contained = true; } catch (Exception) { contained = false; } if (contained) { if (right is MemberAccessExpressionSyntax) // Since Roslyn 1.0.0, old method doesnt work ... { var mar = right as MemberAccessExpressionSyntax; contained = !(mar.Expression.ToString() == replacedNodes.Last().Key.ToString()); } else if (right is ObjectCreationExpressionSyntax) { var mar = right as ObjectCreationExpressionSyntax; contained = !mar.Initializer.DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Any(); //!(mar.ToString () == replacedNodes.Last ().Key.ToString ()); } } if (!contained || replacedNodes.Count() == 1) // We been replaced { try { var str = replacedNodes.Last().Value.ToFullString(); if (right is MemberAccessExpressionSyntax) { var mar = right as MemberAccessExpressionSyntax; right = SyntaxFactory.ParseExpression(str + "." + mar.Name.ToFullString()); } else { // for // replacedNodes.Any(k => k.Key == right); right = SyntaxFactory.ParseExpression(str); // (ExpressionSyntax)replacedNodes.FirstOrDefault(o => o.Key.ToFullString() == right.ToFullString()).Value; } replacedNodes = replacedNodes.Except(replacedNodes.Last()); } catch (Exception) { // throw; } } } var newAssignment = SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName( tempName), SyntaxFactory.IdentifierName( localExpression.Left.As<IdentifierNameSyntax>() .ToFullString())), right) ).NormalizeWhitespace(); assignments.Add(newAssignment); // var varName = declarator.Identifier.Text + "_helper_"+ e + lastTemporaryIndex++; } var statements = new List<StatementSyntax>(); ObjectCreationExpressionSyntax newObjectCreation; if (objectCreation.ArgumentList == null || (!objectCreation.ArgumentList.Arguments.Any())) { var text = objectCreation.WithInitializer(null).ToFullString().Trim(); if (!text.EndsWith("()")) text += "()"; var exp = SyntaxFactory.ParseExpression(text); newObjectCreation = (ObjectCreationExpressionSyntax)exp; //SyntaxFactory.ObjectCreationExpression(objectCreation.Type); } else { newObjectCreation = objectCreation.WithInitializer(null).WithArgumentList(objectCreation.ArgumentList); } var local = SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( target.Type, SyntaxFactory.SeparatedList(new[] { SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(tempName), null, SyntaxFactory.EqualsValueClause(newObjectCreation)) }) ) ).NormalizeWhitespace(); statements.Add(local); statements.AddRange(assignments.Cast<StatementSyntax>()); var listS = SyntaxFactory.List(statements); var newList2 = new List<ObjectCreationExpressionSyntax>(); var union2 = originalObjectExpressionSyntax.Union(new SyntaxNode[] { target }); foreach (var statementSyntax in union2) { try { newList2.Add((ObjectCreationExpressionSyntax)newRoot.GetCurrentNode(statementSyntax)); } catch (Exception ex) { } } if (!newList2.Contains(target)) { // originalObjectExpressionSyntax = originalObjectExpressionSyntax.Union(new SyntaxNode[] { target }).ToArray(); } originalObjectExpressionSyntax = originalObjectExpressionSyntax.Where(l => l != null).ToArray(); newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax); try { var currentNode = newRoot.GetCurrentNode(target); var lastStatement = currentNode.Ancestors().OfType<StatementSyntax>().First(); newRoot = newRoot.InsertNodesBefore(lastStatement, listS); // var currentNodes = newRoot.GetCurrentNodes<SyntaxNode>(originalObjectExpressionSyntax); newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax); currentNode = newRoot.GetCurrentNode(target); var newName = SyntaxFactory.ParseExpression(tempName); var nDict = new Dictionary<SyntaxNode, SyntaxNode>(); foreach (var replacedNode in replacedNodes) nDict[replacedNode.Key] = replacedNode.Value; nDict[currentNode] = newName; replacedNodes = nDict; // newRoot = newRoot; newRoot = newRoot.ReplaceNode(currentNode, newName); var newList = new List<ObjectCreationExpressionSyntax>(); var union = originalObjectExpressionSyntax.Union(nDict.Values); //.Union(new SyntaxNode[]{newName}); foreach (var statementSyntax in union) { try { // if(statementSyntax==newName) // { // newList.Add((ObjectCreationExpressionSyntax) (newName)); // continue; // } var syntaxNode = newRoot.GetCurrentNode(statementSyntax); newList.Add((ObjectCreationExpressionSyntax)syntaxNode); } catch (Exception ex) { } } originalObjectExpressionSyntax = newList.ToArray(); newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax); } catch (Exception ex) { } } return lastTemporaryIndex; }