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 print generic methods and constructors #284

Merged
merged 8 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,8 +14,7 @@ public class ThisIsSomeLongNameAndItShouldFormatWell1
AndYetAnotherLongClassName,
AndStillOneMore { }

public class SimpleGeneric<T>
where T : new() { }
public class SimpleGeneric<T> where T : new() { }

public class LongTypeConstraints<T>
where T : SomeLongNameThatJustKeepsGoing,
Expand All @@ -33,8 +32,7 @@ public class LongerClassNameWithLotsOfGenerics<
TThirdLongName__________________
> : SomeBaseClass<TLongName> { }

public class SimpleGeneric<T> : BaseClass<T>
where T : new() { }
public class SimpleGeneric<T> : BaseClass<T> where T : new() { }

public class ThisIsSomeLongNameAndItShouldFormatWell2<T, T2, T3>
: AnotherLongClassName<T>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ public class BasicClass

public class Initializers : BasicClass
{
public Initializers()
: this(true) { }
public Initializers() : this(true) { }

public Initializers(string value)
: base(value) { }
public Initializers(string value) : base(value) { }

public Initializers(
string parameter1,
string parameter2,
string parameter3,
string parameter4
) : base(false)
{
_parameter1 = parameter1;
_parameter2 = parameter2;
}
}

public class Exactly80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ class ClassName
{
delegate void Delegate();

delegate void Delegate<[System.Obsolete()] out T>()
where T : struct;
delegate void Delegate<[System.Obsolete()] out T>() where T : struct;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class ClassName
DynamicallyAccessedMemberTypes.PublicConstructors)]
TAccountClaimsPrincipalFactory>(
this IRemoteAuthenticationBuilder<RemoteAuthenticationState, RemoteUserAccount> builder
)
where TAccountClaimsPrincipalFactory : AccountClaimsPrincipalFactory<RemoteUserAccount> =>
) where TAccountClaimsPrincipalFactory : AccountClaimsPrincipalFactory<RemoteUserAccount> =>
builder.AddAccountClaimsPrincipalFactory<
RemoteAuthenticationState,
RemoteUserAccount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
delegate void Delegate<T>()
where T : struct;
delegate void Delegate<T>() where T : struct;

class ClassName<T>
where T : class
class ClassName<T> where T : class
{
void MethodName<T>()
where T : class
void MethodName<T>() where T : class
{
void LocalFunction<T>()
where T : class
void LocalFunction<T>() where T : class
{
return;
}
}

public static ReturnType<T> MethodName<T, U>(
string parameter1,
string parameter2
) where T : class
where U : class
{
return;
}
}

interface InterfaceName<T>
where T : class { }
interface InterfaceName<T> where T : class { }

struct Struct<T>
where T : class { }
struct Struct<T> where T : class { }

class ClassName<N, C, T, TT, L>
where N : new()
Expand Down
39 changes: 30 additions & 9 deletions Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,40 @@ public static Doc Print(IEnumerable<TypeParameterConstraintClauseSyntax> constra
{
return Doc.Null;
}
else if (constraintClausesList.Count == 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

So, this whole function can be replaced by C# 8/9's new pattern matching syntax 🚀

return constraintClauses.ToList() switch
{
    { Count: 0 } => Doc.Null,
    { Count: 1 } => /* stuff */,
    var list when list[0].Parent is MethodDeclarationSyntax => /* stuff */,
    var list => Doc.Indent(
        Doc.HardLine,
        Doc.Join(Doc.HardLine, list.Select(TypeParameterConstraintClause.Print))
    )                                               
};

{
return Doc.Group(
Doc.Indent(
Doc.Line,
Doc.Join(
Doc.Line,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
)
)
);
}

var docs = new List<Doc>
if (constraintClausesList[0].Parent is MethodDeclarationSyntax)
{
Doc.Indent(
Doc.HardLine,
Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
return Doc.Concat(
" ",
Doc.Align(
2,
Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
)
)
)
};
);
}

return Doc.Concat(docs);
return Doc.Indent(
Doc.HardLine,
Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public static Doc Print(CSharpSyntaxNode node)
{
docs.Add(
groupId != null
&& constraintClauses.Count() < 2
&& constructorInitializer == null
? Block.PrintWithConditionalSpace(body, groupId)
: Block.Print(body)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public static class ConstructorInitializer
{
public static Doc Print(ConstructorInitializerSyntax node)
{
return Doc.Indent(
Doc.HardLine,
return Doc.Concat(
" ",
Token.PrintWithSuffix(node.ColonToken, " "),
Token.Print(node.ThisOrBaseKeyword),
ArgumentList.Print(node.ArgumentList)
Expand Down