Gargoyle
Gargoyle

Reputation: 10375

Roslyn ParameterListSyntax puts comma in wrong spot

In my CodeFixProvider I'm adding a new parameter to a constructor, but the formatting isn't working. When the person has each parameter on a line of its own, the new parameter is added but at the start of the line with the comma on the new line. So instead of getting this:

class Foo(
    string something,
    Context context
);

I end up generating this:

class Foo(
    string something
, Context context
);

What must I change in the code below to make it work properly?

var newParameter = SyntaxFactory
    .Parameter(SyntaxFactory.Identifier(parameterName))
    .WithType(typeSyntax);

var count = parameterList.Parameters.Count;
if (count > 0) {
    var previous = parameterList.Parameters[count - 1];
    newParameter = newParameter.WithTriviaFrom(previous);
}

var newParameterList = parameterList.AddParameters(newParameter);

SyntaxNode? newItem;

if (data.Item is ConstructorDeclarationSyntax constructor)
    newItem = constructor.WithParameterList(newParameterList);
else if (data.Item is ClassDeclarationSyntax @class)
    newItem = @class.WithParameterList(newParameterList);
else
    return data.Document;

editor.ReplaceNode(data.Item, newItem);

Upvotes: 0

Views: 20

Answers (0)

Related Questions