From 3383f7a12a9f745b136f7bfb00ecfd4adca5b37a Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 3 Jul 2023 14:24:56 -0500 Subject: [PATCH] handling more cases --- Shell/ReviewBranch.psm1 | 16 ++-- .../TestFiles/cs/UsingDirectives_Basics.test | 1 + .../cs/UsingDirectives_EdgeCase2.test | 6 ++ .../cs/UsingDirectives_EdgeCase3.test | 6 ++ .../SyntaxPrinter/UsingDirectives.cs | 76 +++++++++++++------ 5 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.test diff --git a/Shell/ReviewBranch.psm1 b/Shell/ReviewBranch.psm1 index 15a3b0789..691290076 100644 --- a/Shell/ReviewBranch.psm1 +++ b/Shell/ReviewBranch.psm1 @@ -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 @@ -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 diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test index 1dcd2a4a1..ba7a50c1b 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test @@ -2,3 +2,4 @@ global using Global; using System; using Custom; using static Expression; +using Index = Microsoft.Framework.Index; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.test new file mode 100644 index 000000000..267ff491d --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.test @@ -0,0 +1,6 @@ +#if DEBUG +using System; +#else +using Microsoft; +#endif +using System.IO; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.test new file mode 100644 index 000000000..f79019003 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.test @@ -0,0 +1,6 @@ +#if !DEBUG +using System; +#else +using Microsoft; +#endif +using System.IO; diff --git a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs index c81f5a403..2e7619812 100644 --- a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs +++ b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs @@ -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( @@ -27,29 +29,34 @@ bool printExtraLines { var docs = new List(); - 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; } } @@ -61,13 +68,16 @@ private static IEnumerable> GroupUsings( FormattingContext context ) { + var globalUsings = new List(); var regularUsings = new List(); var staticUsings = new List(); + var aliasUsings = new List(); // TODO what about multiple ifs? var directiveGroup = new List(); // TODO this is leftovers for the first group var leftOvers = new List(); var ifCount = 0; + var isFirst = true; foreach (var usingDirective in usings) { var openIf = ifCount > 0; @@ -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) @@ -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()) @@ -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