Skip to content

Commit

Permalink
fix: bugs from #1066 (#1078)
Browse files Browse the repository at this point in the history
* fix: Bug with empty argument list

* fix: Incorrect formatting of lambdas with nested lambdas

Group id used in ArgumentListLikeSyntax was not unique
and the nested lambda overrode the state of the parent lambda

* style: Use switch expression instead of statement

* fix: Second bug in 1077

Closes #1077
  • Loading branch information
Rudomitori authored Dec 20, 2023
1 parent 76c2f73 commit 43ba8d7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
var someVariable = someObject.Property.CallMethod(
someValue => someValue.SomeProperty == someOtherValue___________________________
var someVariable =
someObject____________.Property_______________.CallMethod______________________();

var someVariable = someObject.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
);

var someVariable = someObject
.Property()
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

var someVariable = someObject
.Property.CallMethod(
someValue => someValue.SomeProperty == someOtherValue___________________________
.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
)
.CallMethod();

Expand All @@ -25,8 +28,8 @@ var someVariable = this.Property.CallMethod(someValue =>
var someVariable = this.Property()
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

var someVariable = this.Property.CallMethod(
someValue => someValue.SomeProperty == someOtherValue___________________________
var someVariable = this.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
)
.CallMethod();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,8 @@ class ClassName
someThing_______________________
.Property!.CallMethod__________________()
.CallMethod__________________();

IEnumerable<ValueProviderFactory> valueProviderFactories =
new ModelBinderAttribute_______().GetValueProviderFactories(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ public class ClassName
|| t.OtherLongColumn________________________________
);

this.Select_________________________________________(
superLongName________________________________ => true
Select______________________________________(superLongName_________________________ =>
true
);
this.Select_________________________________________(selector: static longName________ =>

this.Select_________________________________(selector: static longName_____________ =>
true
);

Expand Down Expand Up @@ -149,5 +150,12 @@ public class ClassName
return someOtherLongName__________________;
}
);

this.Where______________________________________(
selector: static longName_________________ =>
{
return Method(x => x.Prop);
}
);
}
}
66 changes: 27 additions & 39 deletions Src/CSharpier/SyntaxPrinter/ArgumentListLikeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,37 @@ public static Doc Print(
FormattingContext context
)
{
var docs = new List<Doc> { Token.Print(openParenToken, context) };
var lambdaId = Guid.NewGuid();

switch (arguments)
var args = arguments switch
{
case [{ Expression: SimpleLambdaExpressionSyntax lambda } arg]:
{
docs.Add(
[{ Expression: SimpleLambdaExpressionSyntax lambda } arg]
=> Doc.Concat(
Doc.GroupWithId(
"LambdaArguments",
$"LambdaArguments{lambdaId}",
Doc.Indent(
Doc.SoftLine,
Argument.PrintModifiers(arg, context),
SimpleLambdaExpression.PrintHead(lambda, context)
)
),
Doc.IndentIfBreak(
Doc.IfBreak(
Doc.Indent(Doc.Group(SimpleLambdaExpression.PrintBody(lambda, context))),
SimpleLambdaExpression.PrintBody(lambda, context),
"LambdaArguments"
$"LambdaArguments{lambdaId}"
),
lambda.Body
is BlockSyntax
or ObjectCreationExpressionSyntax
or AnonymousObjectCreationExpressionSyntax
? Doc.IfBreak(Doc.SoftLine, Doc.Null, "LambdaArguments")
? Doc.IfBreak(Doc.SoftLine, Doc.Null, $"LambdaArguments{lambdaId}")
: Doc.SoftLine
);
break;
}
case [
{
Expression: ParenthesizedLambdaExpressionSyntax
{
ParameterList.Parameters.Count: 0,
Block: { }
} lambda
} arg
]:
{
docs.Add(
),
[{ Expression: ParenthesizedLambdaExpressionSyntax lambda } arg]
when lambda is { ParameterList.Parameters: [], Block: { } }
=> Doc.Concat(
Doc.GroupWithId(
"LambdaArguments",
$"LambdaArguments{lambdaId}",
Doc.Indent(
Doc.SoftLine,
Argument.PrintModifiers(arg, context),
Expand All @@ -58,27 +48,25 @@ or AnonymousObjectCreationExpressionSyntax
),
Doc.IndentIfBreak(
ParenthesizedLambdaExpression.PrintBody(lambda, context),
"LambdaArguments"
$"LambdaArguments{lambdaId}"
),
Doc.IfBreak(Doc.SoftLine, Doc.Null, "LambdaArguments")
);
break;
}
default:
{
docs.Add(
Doc.IfBreak(Doc.SoftLine, Doc.Null, $"LambdaArguments{lambdaId}")
),
[_, ..]
=> Doc.Concat(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context)
),
Doc.SoftLine
);
break;
}
}

docs.Add(Token.Print(closeParenToken, context));
),
_ => Doc.Null
};

return Doc.Concat(docs);
return Doc.Concat(
Token.Print(openParenToken, context),
args,
Token.Print(closeParenToken, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal static class SimpleLambdaExpression
{
public static Doc Print(SimpleLambdaExpressionSyntax node, FormattingContext context)
{
return Doc.Concat(PrintHead(node, context), PrintBody(node, context));
return Doc.Group(PrintHead(node, context), PrintBody(node, context));
}

public static Doc PrintHead(SimpleLambdaExpressionSyntax node, FormattingContext context)
Expand All @@ -25,7 +25,7 @@ public static Doc PrintBody(SimpleLambdaExpressionSyntax node, FormattingContext
ObjectCreationExpressionSyntax
or AnonymousObjectCreationExpressionSyntax
=> Doc.Group(" ", Node.Print(node.Body, context)),
_ => Doc.Indent(Doc.Group(Doc.Line, Node.Print(node.Body, context)))
_ => Doc.Indent(Doc.Line, Node.Print(node.Body, context))
};
}
}

0 comments on commit 43ba8d7

Please sign in to comment.