Skip to content

Commit

Permalink
GH-7 some progress cleaning up invocation expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Mar 8, 2021
1 parent 72e0a6e commit fffb7b1
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 69 deletions.
13 changes: 6 additions & 7 deletions Src/CSharpier.Tests/EncodingTests/UTF8BOM.actual.cst
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ namespace Insite.Admin
public class AdminContext
{
public static bool DisableAdminAccess
=> (ConfigurationManager.AppSettings["DisableAdminAccess"] ?? "false")
.EqualsIgnoreCase("true");
=> (ConfigurationManager.AppSettings["DisableAdminAccess"] ?? "false").EqualsIgnoreCase(
"true");

private static IPerRequestCacheManager perRequestCacheManager;
private static IPerRequestCacheManager PerRequestCacheManager
=> perRequestCacheManager ?? (perRequestCacheManager = DependencyLocator
.Current
.GetInstance<IPerRequestCacheManager>());
=> perRequestCacheManager ?? (perRequestCacheManager = DependencyLocator.Current.GetInstance<IPerRequestCacheManager>());

private static IAdminContext current;

public static IAdminContext Current
{
get
{
return current ?? PerRequestCacheManager
.Get("AdminContext_Current", LoadAdminContext);
return current ?? PerRequestCacheManager.Get(
"AdminContext_Current",
LoadAdminContext);
}
set { current = value; }
}
Expand Down
14 changes: 6 additions & 8 deletions Src/CSharpier.Tests/Samples/AllInOne.Formatted.cst
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,12 @@ namespace My
}
void AsyncAnonymous() // C # 5 feature
{
var task = Task
.Factory
.StartNew(
async () =>
{
return await new WebClient()
.DownloadStringTaskAsync("http://example.com");
});
var task = Task.Factory.StartNew(
async () =>
{
return await new WebClient().DownloadStringTaskAsync(
"http://example.com");
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/TestFiles/Directives/Directives.cst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ClassName
namespace Namespace
{
#pragma
using System.Linq;
using System.Linq;

#pragma
class ExtraLineChecker { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@
{
this.DoSomething().DoSomething();

this
.DoSomething()
this.DoSomething()
.DoSomething()
.DoSomething()
.DoSomething()
.DoSomething();

this
.Method(
"ljkasdfkljasdlkjfklajsdfkjlasdfkjlasdkljfajklsdfkjasdf",
"kljasdfkljaslkjdfljaksdfkjlaksljfaksjldf");
this.Method(
"ljkasdfkljasdlkjfklajsdfkjlasdfkjlasdkljfajklsdfkjasdf",
"kljasdfkljaslkjdfljaksdfkjlaksljfaksjldf");

var superLongMethodNameForceLine = someFactoryName
.SuperLongMethodNameForceLine();
var superLongMethodNameForceLine = someFactoryName.SuperLongMethodNameForceLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class ClassName
{
this.DoSomething().DoSomething();

this
.DoSomething()
this.DoSomething()
.DoSomething()
.DoSomething()
.DoSomething()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
{
void AsyncAnonymous()
{
var task = Task
.Factory
.StartNew(
async () =>
{
return await new WebClient()
.DownloadStringTaskAsync("http://example.com");
});
var task = Task.Factory.StartNew(
async () =>
{
return await new WebClient().DownloadStringTaskAsync(
"http://example.com");
});
}
}
139 changes: 104 additions & 35 deletions Src/CSharpier/Printer/InvocationExpressionSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace CSharpier
{
public class NodeOrToken
public class PrintedNode
{
public CSharpSyntaxNode Node { get; set; }
public SyntaxToken? Token { get; set; }
public Doc Doc { get; set; }
}

Expand All @@ -19,64 +18,134 @@ public partial class Printer
private Doc PrintInvocationExpressionSyntax(
InvocationExpressionSyntax node)
{
var nodeOrTokens = new List<NodeOrToken>();

void PushToken(SyntaxToken syntaxToken)
{
nodeOrTokens.Add(new NodeOrToken { Token = syntaxToken });
}

void PushNode(CSharpSyntaxNode syntaxNode)
{
nodeOrTokens.Add(new NodeOrToken { Node = syntaxNode });
}

void PushDoc(Doc doc)
{
nodeOrTokens.Add(new NodeOrToken { Doc = doc });
}
var printedNodes = new List<PrintedNode>();

void Traverse(ExpressionSyntax expression)
{
/*InvocationExpression
[this.DoSomething().DoSomething][()]
SimpleMemberAccessExpression
[this.DoSomething()][.][DoSomething]
InvocationExpression
[this.DoSomething][()]
SimpleMemberAccessExpression
[this][.][DoSomething]
*/
if (expression is InvocationExpressionSyntax invocationExpressionSyntax)
{
Traverse(invocationExpressionSyntax.Expression);
PushNode(invocationExpressionSyntax.ArgumentList);
printedNodes.Add(new PrintedNode
{
Doc = this.PrintArgumentListSyntax(invocationExpressionSyntax.ArgumentList),
Node = invocationExpressionSyntax
});
}
else if (expression is MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
Traverse(memberAccessExpressionSyntax.Expression);
PushDoc(SoftLine);
PushToken(memberAccessExpressionSyntax.OperatorToken);
PushNode(memberAccessExpressionSyntax.Name);
printedNodes.Add(new PrintedNode
{
Doc = Concat(this.PrintSyntaxToken(memberAccessExpressionSyntax.OperatorToken), this.Print(memberAccessExpressionSyntax.Name)),
Node = memberAccessExpressionSyntax
});
}
else
{
PushNode(expression);
printedNodes.Add(new PrintedNode
{
Doc = this.Print(expression),
Node = expression
});
}
}

Traverse(node);

var parts = new Parts();
foreach (var nodeOrToken in nodeOrTokens)
var groups = new List<List<Doc>>();
var currentGroup = new List<Doc>();
currentGroup.Add(printedNodes[0].Doc);
var index = 1;
for (; index < printedNodes.Count; index++)
{
if (nodeOrToken.Token != null)
{
parts.Push(this.PrintSyntaxToken(nodeOrToken.Token.Value));
}
else if (nodeOrToken.Doc != null)
if (printedNodes[index].Node is MemberAccessExpressionSyntax)
{
parts.Push(nodeOrToken.Doc);
currentGroup.Add(printedNodes[index].Doc);
}
else
{
parts.Push(this.Print(nodeOrToken.Node));
break;
}
}
// TODO GH-7 prettier has pretty complex logic for how to print this, which I think is what we need.
// the logic for where to put groups to get line breaks needs to happen here, it can't happen in the nodes below this.
return Group(parts.First(), Indent(parts.Skip(1).ToArray()));

// TODO GH-7 there is a lot more code in prettier for how to get this all working, also include some documents
if (printedNodes[index].Node is MemberAccessExpressionSyntax)
{
currentGroup.Add(printedNodes[index].Doc);
index++;
}

groups.Add(currentGroup);
currentGroup = new List<Doc>();

var hasSeenInvocationExpression = false;
for (; index < printedNodes.Count; index++)
{
if (hasSeenInvocationExpression && IsMemberish(printedNodes[index].Node)) {
// [0] should be appended at the end of the group instead of the
// beginning of the next one
// if (printedNodes[i].node.computed && isNumericLiteral(printedNodes[i].node.property)) {
// currentGroup.push(printedNodes[i]);
// continue;
// }

groups.Add(currentGroup);
currentGroup = new List<Doc>();
hasSeenInvocationExpression = false;
}

if (printedNodes[index].Node is InvocationExpressionSyntax) {
hasSeenInvocationExpression = true;
}
currentGroup.Add(printedNodes[index].Doc);

// if (printedNodes[i].node.comments && printedNodes[i].node.comments.some(comment => comment.trailing)) {
// groups.push(currentGroup);
// currentGroup = [];
// hasSeenCallExpression = false;
// }
}

if (currentGroup.Any())
{
groups.Add(currentGroup);
}

var cutoff = 3;
if (groups.Count < cutoff)
{
return Group(groups.SelectMany(o => o).ToArray());
}

return Concat(Group(groups[0].ToArray()), PrintIndentedGroup(groups.Skip(1)));
}

private bool IsMemberish(CSharpSyntaxNode node)
{
return node is MemberAccessExpressionSyntax;
}

private Doc PrintIndentedGroup(IEnumerable<List<Doc>> groups)
{
if (!groups.Any())
{
return null;
}

// TODO GH-7 softline here?
return Indent(Group(Join(SoftLine, groups.Select(o => Group(o.ToArray())))));
}
}
}
1 change: 1 addition & 0 deletions Src/CSharpier/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static async Task<int> Main(string[] args)
return await rootCommand.InvokeAsync(args);
}

// TODO if someone kills this process while running, I think it can leave files half written
public static async Task<int> Run(string directory, bool validate)
{
var fullStopwatch = Stopwatch.StartNew();
Expand Down

0 comments on commit fffb7b1

Please sign in to comment.