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

Better lambda indent #673

Merged
merged 6 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -197,21 +197,17 @@ class ClassName
// comment shouldn't do weird stuff
SomeObject.CallMethod();

CallMethod(
lambdaWithBlock =>
{
return;
}
)
CallMethod(lambdaWithBlock =>
{
return;
})
.CallMethod()
.CallMethod();

someObject.CallMethod(
lambdaWithBlock =>
{
return;
}
);
someObject.CallMethod(lambdaWithBlock =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really "better"? IMHO the previous version was more readable for both these cases.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is mostly better, with those examples being the ones that fall into "maybe not better" category.

These examples I think are definitely better. It saves vertical space and I don't think it is any less readable there.
belav/csharpier-repos@831f201#diff-bc2e3864cdf7b47541cca4aa46b6d378cb2c11cb0091dca0647fed8a565a7dbf

It indents less too, which helps in other cases like this
belav/csharpier-repos@831f201#diff-668431d269a33a846504a3895822785838e4d92a10e9a3ce025b37de65724ed2

fwiw, prettier formats it like this. I don't really like that they force ( ) on it

someObject.CallMethod((lambdaWithBlock) => {
    return;
});

{
return;
});

o.CallMethod(parameter_____________, parameter_____________)
.CallMethod(parameter_____________, parameter_____________);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,52 +61,44 @@ class ClassName
>();

private static readonly Action<IApplicationBuilder> ActionNotImplemented =
new Action<IApplicationBuilder>(
_ =>
{
throw new NotImplementedException();
}
);
new Action<IApplicationBuilder>(_ =>
{
throw new NotImplementedException();
});

public void MapFrom<TResult>(Func<TSource, TDestination, TMember, TResult> mappingFunction)
{
if (true)
{
PropertyMapActions.Add(
pm =>
{
Expression<
Func<TSource, TDestination, TMember, ResolutionContext, TResult>
> expr = (src, dest, destMember, ctxt) =>
mappingFunction(src, dest, destMember);
PropertyMapActions.Add(pm =>
{
Expression<Func<TSource, TDestination, TMember, ResolutionContext, TResult>> expr =
(src, dest, destMember, ctxt) => mappingFunction(src, dest, destMember);

pm.CustomMapFunction = expr;
}
);
pm.CustomMapFunction = expr;
});
}
}

public void Condition(
Func<ConditionParameters<TSource, TDestination, TMember>, bool> condition
) =>
PathMapActions.Add(
pm =>
{
Expression<
Func<TSource, TDestination, TMember, TMember, ResolutionContext, bool>
> expr = (src, dest, srcMember, destMember, ctxt) =>
condition(
new ConditionParameters<TSource, TDestination, TMember>(
src,
dest,
srcMember,
destMember,
ctxt
)
);
pm.Condition = expr;
}
);
PathMapActions.Add(pm =>
{
Expression<
Func<TSource, TDestination, TMember, TMember, ResolutionContext, bool>
> expr = (src, dest, srcMember, destMember, ctxt) =>
condition(
new ConditionParameters<TSource, TDestination, TMember>(
src,
dest,
srcMember,
destMember,
ctxt
)
);
pm.Condition = expr;
});

public override Type SourceType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ public class ClassName
) => evenLongerParameter
);

var task = Task.Factory.StartNew(
async () =>
{
return await new WebClient().DownloadStringTaskAsync____________________(
"http://example.com"
);
}
CallMethod(() =>
{
CallOtherMethod();
});

CallMethod(
() => CallOtherMethod___________________________________________________________()
);

var task = Task.Factory.StartNew(async () =>
{
return await new WebClient().DownloadStringTaskAsync____________________(
"http://example.com"
);
});

Action find = () =>
EntryPointDiscoverer.FindStaticEntryMethod(typeof(IEnumerable<>).Assembly);

Expand All @@ -62,12 +69,10 @@ public class ClassName

var reusedCommand = new Command("reused")
{
Handler = CommandHandler.Create(
() =>
{
doSomething();
}
)
Handler = CommandHandler.Create(() =>
{
doSomething();
})
};

CallSomeMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public class ClassName
someOtherLongName_______________________________
);

this.Where___________________(
superLongName________________________________ =>
{
return someOtherLongName__________________;
}
);
this.Where___________________(superLongName________________________________ =>
{
return someOtherLongName__________________;
});
}
}
15 changes: 14 additions & 1 deletion Src/CSharpier/SyntaxPrinter/ArgumentListLikeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ FormattingContext context
{
var docs = new List<Doc> { Token.Print(openParenToken, context) };

if (arguments.Any())
if (
arguments.Count == 1
&& arguments[0].Expression
is ParenthesizedLambdaExpressionSyntax
{
ParameterList.Parameters.Count: 0,
Block: { }
}
or SimpleLambdaExpressionSyntax { Block: { } }
)
{
docs.Add(Argument.Print(arguments[0], context));
}
else if (arguments.Any())
{
docs.Add(
Doc.Indent(
Expand Down