public override SyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node) { if(this._targetConstructor == null || node.ParameterList.ToString() == this._targetConstructor.ParameterList.ToString()) { var fieldName = this._relatedField.Declaration.Variables .Select(p => p.Identifier.Text) .FirstOrDefault(); var paramName = fieldName.TrimStart('_'); if (this._rewriteParams) { var newParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier(paramName)) .WithType(this._relatedField.Declaration.Type); var newConstructorParams = node.ParameterList.AddParameters(newParam); node = node.WithParameterList(newConstructorParams); } var newStatement = SyntaxExtenders.AssignmentStatement("this." + fieldName, paramName); var newStatements = node.Body.Statements.Insert(0, newStatement); node = node.WithBody(node.Body.WithStatements(newStatements)); } return base.VisitConstructorDeclaration(node); }
public override SyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax constructorDeclaration) { if (constructor == constructorDeclaration) { var type = field.Declaration.Type; var variable = RoslynExtensions.GetFieldName(field); string parameter = variable; if(variable.StartsWith("_")) { parameter = variable.Substring(1); } else { variable = "this." + variable; } //var typeString = type.ToString(); //if (typeString.StartsWith("I")) //name = typeString.Substring(1); //name = Char.ToLowerInvariant(name[0]) + name.Substring(1); var p = SyntaxFactory.Parameter( new SyntaxList<AttributeListSyntax>(), new SyntaxTokenList(), type, SyntaxFactory.Identifier(parameter), null); var parameters = constructorDeclaration.ParameterList.AddParameters(p); var body = constructorDeclaration.Body; var statement = SyntaxFactory.ParseStatement(variable + " = " + parameter + ";" + Environment.NewLine);//.WithLeadingTrivia(SyntaxFactory.Tab, SyntaxFactory.Tab); body = body.AddStatements(statement); return constructorDeclaration.WithParameterList(parameters).WithBody(body); //(BlockSyntax)formatted); } return base.VisitConstructorDeclaration(constructorDeclaration); }
private static ConstructorDeclarationSyntax ReformatConstructorDeclaration(ConstructorDeclarationSyntax constructorDeclaration, IndentationOptions indentationOptions, SyntaxTrivia newLine) { var constructorInitializer = constructorDeclaration.Initializer; var newParameterList = constructorDeclaration.ParameterList .WithTrailingTrivia(constructorDeclaration.ParameterList.GetTrailingTrivia().WithoutTrailingWhitespace().Add(newLine)); var indentationSteps = IndentationHelper.GetIndentationSteps(indentationOptions, constructorDeclaration); var indentation = IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps + 1); var newColonTrailingTrivia = constructorInitializer.ColonToken.TrailingTrivia.WithoutTrailingWhitespace(); var newColonToken = constructorInitializer.ColonToken .WithLeadingTrivia(indentation) .WithTrailingTrivia(newColonTrailingTrivia); var newInitializer = constructorInitializer .WithColonToken(newColonToken) .WithThisOrBaseKeyword(constructorInitializer.ThisOrBaseKeyword.WithLeadingTrivia(SyntaxFactory.Space)); return constructorDeclaration .WithParameterList(newParameterList) .WithInitializer(newInitializer); }