Skip to content

Commit

Permalink
Better print generic methods and constructors (#284)
Browse files Browse the repository at this point in the history
* Better print generic methods

* Update tests

* Update rules

* Update more rules

* Update formatting

* Update rules again

* Simplify logic

* Fix comment handling
  • Loading branch information
Divyansh Shukla authored Jun 12, 2021
1 parent a4ba778 commit 0fec511
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 43 deletions.
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,35 @@ 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 reallyLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongParameter
) : base(
reallyLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongParameterIMeanIt
) { }

public Initializers(
string reallyLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongParameter
) : base(false) { }

public ReallyLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogMethodName()
: base(false) { }

public ReallyLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogMethodName()
: base(
reallyLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongParameterIMeanIt
) { }

public Initializers(
string value
) /*Comment*/
: base(value) { }

public ReallyLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogMethodName()
/*Comment*/: base(false) { }
}

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,32 @@
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>()
where T : class
where U : class { }

public static ReturnType<T> MethodName<T, U>(string parameter)
where T : class
where U : class { }

public static ReturnType<T> MethodName<T, U>(
string reallyLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongParameterIMeanIt
) where T : class
where U : class { }
}

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
37 changes: 25 additions & 12 deletions Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,40 @@ namespace CSharpier.SyntaxPrinter
{
public static class ConstraintClauses
{
public static Doc PrintWithConditionalSpace(
IEnumerable<TypeParameterConstraintClauseSyntax> constraintClauses,
string groupId
) {
return Print(constraintClauses, groupId);
}

public static Doc Print(IEnumerable<TypeParameterConstraintClauseSyntax> constraintClauses)
{
return Print(constraintClauses, null);
}

private static Doc Print(
IEnumerable<TypeParameterConstraintClauseSyntax> constraintClauses,
string? groupId
) {
var constraintClausesList = constraintClauses.ToList();

if (constraintClausesList.Count == 0)
{
return Doc.Null;
}
var prefix = constraintClausesList.Count >= 2 ? Doc.HardLine : Doc.Line;
var body = Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
);

var docs = new List<Doc>
{
Doc.Indent(
Doc.HardLine,
Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
)
)
};

return Doc.Concat(docs);
return Doc.Group(
Doc.Indent(groupId != null ? Doc.IfBreak(" ", prefix, groupId) : prefix),
groupId != null
? Doc.IfBreak(Doc.Align(2, body), Doc.Indent(body), groupId)
: Doc.Indent(body)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ public static Doc Print(CSharpSyntaxNode node)

if (constructorInitializer != null)
{
declarationGroup.Add(ConstructorInitializer.Print(constructorInitializer));
declarationGroup.Add(
groupId != null
? ConstructorInitializer.PrintWithConditionalSpace(
constructorInitializer,
groupId
)
: ConstructorInitializer.Print(constructorInitializer)
);
}

if (modifiers.HasValue && modifiers.Value.Count > 0)
Expand All @@ -163,7 +170,11 @@ public static Doc Print(CSharpSyntaxNode node)

docs.Add(Doc.Group(declarationGroup));

docs.Add(ConstraintClauses.Print(constraintClauses));
docs.Add(
groupId != null
? ConstraintClauses.PrintWithConditionalSpace(constraintClauses, groupId)
: ConstraintClauses.Print(constraintClauses)
);
if (body != null)
{
docs.Add(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters
{
public static class ConstructorInitializer
{
public static Doc PrintWithConditionalSpace(
ConstructorInitializerSyntax node,
string groupId
) {
return Print(node, groupId);
}

public static Doc Print(ConstructorInitializerSyntax node)
{
return Doc.Indent(
Doc.HardLine,
Token.PrintWithSuffix(node.ColonToken, " "),
return Print(node, null);
}

private static Doc Print(ConstructorInitializerSyntax node, string? groupId)
{
var colonToken = Token.PrintWithSuffix(node.ColonToken, " ");
return Doc.Group(
Doc.Indent(groupId != null ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.Line),
groupId != null
? Doc.IfBreak(Doc.Align(2, colonToken), Doc.Indent(colonToken), groupId)
: Doc.Indent(colonToken),
Token.Print(node.ThisOrBaseKeyword),
ArgumentList.Print(node.ArgumentList)
groupId != null
? ArgumentList.Print(node.ArgumentList)
: Doc.Indent(ArgumentList.Print(node.ArgumentList))
);
}
}
Expand Down

0 comments on commit 0fec511

Please sign in to comment.