Skip to content

Commit

Permalink
handling more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jul 3, 2023
1 parent de96c75 commit 3383f7a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
16 changes: 8 additions & 8 deletions Shell/ReviewBranch.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function CSH-ReviewBranch {

if ($branch -eq "main") {
Write-Output "You must be on the branch you want to test. You are currently on main"
exit 1
return
}

$preBranch = "pre-" + $branch
Expand All @@ -59,13 +59,13 @@ function CSH-ReviewBranch {

if ($firstRun) {
Set-Location $repositoryRoot
try {
& git checkout main | Out-String
}
catch {
Write-Host "Could not checkout main on csharpier, working directory is probably not clean"
return
}
# try {
& git checkout main #2>&1 | Out-String
# }
# catch {
# Write-Host "Could not checkout main on csharpier, working directory is probably not clean"
# return
# }

CSH-BuildProject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ global using Global;
using System;
using Custom;
using static Expression;
using Index = Microsoft.Framework.Index;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if DEBUG
using System;
#else
using Microsoft;
#endif
using System.IO;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if !DEBUG
using System;
#else
using Microsoft;
#endif
using System.IO;
76 changes: 51 additions & 25 deletions Src/CSharpier/SyntaxPrinter/UsingDirectives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ then static
what about alias of any type?
*/

// TODO alias!!
// TODO https://github.com/belav/csharpier-repos/pull/80/files has one weird #else thingie

// TODO what does the analyzer do with some of these sorts?
// TODO what about validation?
// TODO get rid of lines and keep them in blocks? - this does https://google.github.io/styleguide/javaguide.html#s3.3-import-statements
// TODO what about alias any type with c# 12?

public static Doc PrintWithSorting(
Expand All @@ -27,29 +29,34 @@ bool printExtraLines
{
var docs = new List<Doc>();

foreach (var usingGroup in GroupUsings(usings, context))
// what is this is an #if? only comments
docs.Add(Token.PrintLeadingTrivia(usings.First().GetLeadingTrivia(), context));
var isFirst = true;
foreach (var groupOfUsingData in GroupUsings(usings, context))
{
for (var i = 0; i < usingGroup.Count; i++)
foreach (var usingData in groupOfUsingData)
{
if (i != 0)
if (!isFirst)
{
docs.Add(Doc.HardLine);
}

if (usingGroup[i].LeadingTrivia != Doc.Null)
if (usingData.LeadingTrivia != Doc.Null)
{
docs.Add(usingGroup[i].LeadingTrivia);
docs.Add(usingData.LeadingTrivia);
}
if (usingGroup[i].Using is UsingDirectiveSyntax usingDirective)
if (usingData.Using is UsingDirectiveSyntax usingDirective)
{
docs.Add(
UsingDirective.Print(
usingDirective,
context,
printExtraLines: (i != 0 || printExtraLines)
printExtraLines // TODO keeping lines is hard, maybe don't? : printExtraLines || !isFirst
)
);
}

isFirst = false;
}
}

Expand All @@ -61,13 +68,16 @@ private static IEnumerable<List<UsingData>> GroupUsings(
FormattingContext context
)
{
var globalUsings = new List<UsingData>();
var regularUsings = new List<UsingData>();
var staticUsings = new List<UsingData>();
var aliasUsings = new List<UsingData>();
// TODO what about multiple ifs?
var directiveGroup = new List<UsingData>();
// TODO this is leftovers for the first group
var leftOvers = new List<UsingData>();
var ifCount = 0;
var isFirst = true;
foreach (var usingDirective in usings)
{
var openIf = ifCount > 0;
Expand All @@ -86,7 +96,9 @@ FormattingContext context
Doc PrintStuff(UsingDirectiveSyntax value)
{
// TODO what about something with comments and a close #endif?
return Doc.Concat(Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context));
return isFirst
? Doc.Null
: Doc.Concat(Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context));
}

if (ifCount > 0)
Expand All @@ -106,28 +118,37 @@ Doc PrintStuff(UsingDirectiveSyntax value)
leftOvers.Add(new UsingData { LeadingTrivia = PrintStuff(usingDirective) });
}

if (usingDirective.StaticKeyword.RawSyntaxKind() == SyntaxKind.None)
var usingData = new UsingData
{
regularUsings.Add(
new UsingData
{
Using = usingDirective,
LeadingTrivia = !openIf ? PrintStuff(usingDirective) : Doc.Null
}
);
Using = usingDirective,
LeadingTrivia = !openIf ? PrintStuff(usingDirective) : Doc.Null
};

// TODO what about IF on these?
if (usingDirective.GlobalKeyword.RawSyntaxKind() != SyntaxKind.None)
{
globalUsings.Add(usingData);
}
else if (usingDirective.StaticKeyword.RawSyntaxKind() != SyntaxKind.None)
{
staticUsings.Add(usingData);
}
else if (usingDirective.Alias is not null)
{
aliasUsings.Add(usingData);
}
else
{
// TODO what about IF on these?
staticUsings.Add(
new UsingData
{
Using = usingDirective,
LeadingTrivia = !openIf ? PrintStuff(usingDirective) : Doc.Null
}
);
regularUsings.Add(usingData);
}
}

isFirst = false;
}

if (globalUsings.Any())
{
yield return globalUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

if (regularUsings.Any())
Expand All @@ -149,6 +170,11 @@ Doc PrintStuff(UsingDirectiveSyntax value)
{
yield return staticUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

if (aliasUsings.Any())
{
yield return aliasUsings.OrderBy(o => o.Using!, Comparer).ToList();
}
}

private class UsingData
Expand Down

0 comments on commit 3383f7a

Please sign in to comment.