Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve formatting of lambda expressions #1066

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,46 @@ public class ClassName
this.Method(true);

this.NamedArguments(b: true, c: "false");

this.ToDictionary(o => o.Key, o => new { o.Prop });

this.ToDictionary(o => o.Key, o => new Entity { o.Prop });

this.ToDictionary_____________(
o => o.VeryLongKey_________________________,
o => new { o.Prop }
);

this.ToDictionary_____________(
o => o.VeryLongKey_________________________,
o => new Entity { o.Prop }
);

this.ToDictionary_____________(
keySelector: static o => o.Key,
elementSelector: static o => new Entity { o.Prop }
);

this.ToDictionary_____________(
keySelector: static o => o.Key,
elementSelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
}
);

this.GroupBy_____________(
keySelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
},
resultSelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ class TestClass
"SecondParameter"
);

var y = someList.Where(
o =>
someLongValue_______________________
&& theseShouldNotIndent_________________
&& theseShouldNotIndent_________________
> butThisOneShould_________________________________________
var y = someList.Where(o =>
someLongValue_______________________
&& theseShouldNotIndent_________________
&& theseShouldNotIndent_________________
> butThisOneShould_________________________________________
);

var someVariable =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var someVariable = someObject
.CallMethod();

// TODO too hard to change this for now, will do it in https://github.com/belav/csharpier/issues/451
shocklateboy92 marked this conversation as resolved.
Show resolved Hide resolved
var someVariable = this.Property.CallMethod(
someValue => someValue.SomeProperty == someOtherValue___________________________
var someVariable = this.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
);

var someVariable = this.Property()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ class ClassName

roleNames
.ToList()
.ForEach(
role =>
this.SomeProperty.Setup(o => longThing_______________________________________)
.ForEach(role =>
this.SomeProperty.Setup(o => longThing_______________________________________)
);

roleNames
Expand Down Expand Up @@ -115,9 +114,8 @@ class ClassName

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

CallMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,146 @@ public class ClassName

this.WhereAsync(static o => true);

this.Where___________________(
longName__________________ => someOtherLongName__________________
this.Select(o => new { o.Prop });

this.Select(o => new Entity { o.Prop });

this.Select(static o => new { o.Prop });

this.Select(static o => new Entity { o.Prop });

this.Select(selector: o => new { o.Prop });

this.Select(selector: o => new Entity { o.Prop });

this.Select(selector: static o => new { o.Prop });

this.Select(selector: static o => new Entity { o.Prop });

this.Where(x => x.IsSomething).Select(static o => new Entity { o.Prop });

this.Where(x => x.IsLongSomething______________________)
.Select(static o => new { o.Prop, });

this.Where(x => x.IsLongSomething______________________)
.Select(selector: static o => new { o.Prop, });

this.Where(x => x.IsLongSomething________________)
.Select(static o => new Entity { o.Prop, });

this.Where(x => x.IsSomeThing)
.Select(selector: static o => new
{
o.LongName_______________________,
o.LongName_______________________
});

this.Where(x => x.IsSomeThing)
.Select(selector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
});

this.Select_________________________________(
superLongName________________________________ => new
{
Prop = superLongName________________________________,
}
);

this.Where___________________(
superLongName________________________________ =>
someOtherLongName_______________________________
this.Select_________________________________(
superLongName_________________________________ => new Entity
{
Prop = superLongName________________________________,
}
);

this.Select_________________________________(
selector: static longName__________________ => new
{
Prop__________ = longName__________________
}
);

this.Select_________________________________(
selector: static longName__________________ => new Entity(
LongArgument_______________________,
LongArgument_______________________
)
{
Prop__________ = longName__________________
}
);

this.Select_________________________________(o => new Entity
{
o.LongName________________________________,
});

this.Select_________________________________(static o => new
{
o.LongName________________________________,
});

this.Select_________________________________(static o => new Entity
{
o.LongName________________________________,
});

this.Where(x => x.IsSomeThing)
.Select(selector: static o => new Entity(
LongArgument_______________________,
LongArgument_______________________
)
{
o.LongName_______________________,
o.LongName_______________________
});

this.Where(t =>
t.SomeLongColumn________________________ || t.OtherLongColumn_______________________
);

this.Where(t =>
t.SomeVeryLongColumn________________________________
|| t.OtherLongColumn________________________________
);

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

this.Select_________________________________________(
selector: static longName______________ =>
someOtherLongName________________________________
);

this.Where___________________(x =>
{
return x;
});

this.Where___________________(selector: static x =>
{
return someOtherLongName__________________;
return x;
});

this.Where______________________________________(
superLongName________________________________ =>
{
return someOtherLongName__________________;
}
);

this.Where______________________________________(
selector: static longName_________________ =>
{
return someOtherLongName__________________;
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class ClassName
: null
) { }

var variableDeclarator = conditionalAccessExpression?.invocationExpression(
o => someLongValue___________________________________
var variableDeclarator = conditionalAccessExpression?.invocationExpression(o =>
someLongValue___________________________________
shocklateboy92 marked this conversation as resolved.
Show resolved Hide resolved
);

var variableDeclarator = (CastExpression)
Expand Down
80 changes: 61 additions & 19 deletions Src/CSharpier/SyntaxPrinter/ArgumentListLikeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,70 @@ FormattingContext context
{
var docs = new List<Doc> { Token.Print(openParenToken, context) };

if (
arguments.Count == 1
&& arguments[0].Expression
is ParenthesizedLambdaExpressionSyntax
switch (arguments)
{
case [{ Expression: SimpleLambdaExpressionSyntax lambda } arg]:
{
docs.Add(
Doc.GroupWithId(
"LambdaArguments",
Doc.Indent(
Doc.SoftLine,
Argument.PrintModifiers(arg, context),
SimpleLambdaExpression.PrintHead(lambda, context)
)
),
Doc.IndentIfBreak(
SimpleLambdaExpression.PrintBody(lambda, context),
"LambdaArguments"
),
lambda.Body
is BlockSyntax
or ObjectCreationExpressionSyntax
or AnonymousObjectCreationExpressionSyntax
? Doc.IfBreak(Doc.SoftLine, Doc.Null, "LambdaArguments")
: Doc.SoftLine
);
break;
}
case [
{
Expression: ParenthesizedLambdaExpressionSyntax
{
ParameterList.Parameters.Count: 0,
Block: { }
}
or SimpleLambdaExpressionSyntax { Block: { } }
)
{
docs.Add(Argument.Print(arguments[0], context));
}
else if (arguments.Any())
{
docs.Add(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context)
),
Doc.SoftLine
);
} lambda
} arg
]:
{
docs.Add(
Doc.GroupWithId(
"LambdaArguments",
Doc.Indent(
Doc.SoftLine,
Argument.PrintModifiers(arg, context),
ParenthesizedLambdaExpression.PrintHead(lambda, context)
)
),
Doc.IndentIfBreak(
ParenthesizedLambdaExpression.PrintBody(lambda, context),
"LambdaArguments"
),
Doc.IfBreak(Doc.SoftLine, Doc.Null, "LambdaArguments")
);
break;
}
default:
{
docs.Add(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context)
),
Doc.SoftLine
);
break;
}
}

docs.Add(Token.Print(closeParenToken, context));
Expand Down
11 changes: 9 additions & 2 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ internal static class Argument
{
public static Doc Print(ArgumentSyntax node, FormattingContext context)
{
var docs = new List<Doc>();
return Doc.Concat(
Argument.PrintModifiers(node, context),
Node.Print(node.Expression, context)
);
}

public static Doc PrintModifiers(ArgumentSyntax node, FormattingContext context)
{
var docs = new List<Doc>(2);
if (node.NameColon != null)
{
docs.Add(BaseExpressionColon.Print(node.NameColon, context));
Expand All @@ -15,7 +23,6 @@ public static Doc Print(ArgumentSyntax node, FormattingContext context)
docs.Add(Token.PrintWithSuffix(node.RefKindKeyword, " ", context));
}

docs.Add(Node.Print(node.Expression, context));
return Doc.Concat(docs);
}
}
Loading
Loading