diff --git a/Shell/Release.psm1 b/Shell/CreateRelease.psm1 similarity index 97% rename from Shell/Release.psm1 rename to Shell/CreateRelease.psm1 index a635d3b36..15ea76dc7 100644 --- a/Shell/Release.psm1 +++ b/Shell/CreateRelease.psm1 @@ -1,4 +1,4 @@ -function CSH-Release { +function CSH-CreateRelease { param ( [Parameter(Mandatory=$true)] [string]$versionNumber diff --git a/Shell/DeleteReviewBranches.psm1 b/Shell/DeleteReviewBranches.psm1 new file mode 100644 index 000000000..9651c8684 --- /dev/null +++ b/Shell/DeleteReviewBranches.psm1 @@ -0,0 +1,12 @@ +function CSH-DeleteReviewBranches { + + # TODO probably make this configurable + $pathToTestingRepo = "C:/Projects/csharpier-repos" + Set-Location $pathToTestingRepo + + git checkout main + git branch | Where-Object { $_ -notmatch "main" } | ForEach-Object { git branch -D $_ } + git branch -r | Where-Object { $_ -notmatch "origin/main" } | ForEach-Object { git push origin --delete $_.Replace("origin/", "").Trim() } +} + +Export-ModuleMember -Function CSH-* \ No newline at end of file diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives_CompilationUnit.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives_CompilationUnit.test deleted file mode 100644 index 378678c5a..000000000 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Directives_CompilationUnit.test +++ /dev/null @@ -1,19 +0,0 @@ -#if NO_EXTRA_LINES -extern alias Foo1; -#else -extern alias Foo2; -#endif - -#if NO_EXTRA_LINES -using System.Net.Http; -#else -using System.Web.Routing; -#endif - -using System.Web.Http; - -#if KEEP_LINE_ABOVE -using System.Web.Http; -#endif - -namespace Namespace { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives.test index c4d4e635c..d52e6a398 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives.test @@ -1,29 +1,13 @@ // leading -using First; // trailing - +using A; // trailing // leading with space using // another trailing -Second; - -using -// static leading -static // static trailing -Third; - -using M = System.Math; -using Point = (int x, int y); - -using static System.Math; - -global using System; - -using First; -using Second; +B; namespace Namespace { - using Third; using One.Two.Three; + using Third; public class ClassName { } } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_BasicIfDirective.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_BasicIfDirective.expected.test index c1dbd44db..ed4e3e503 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_BasicIfDirective.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_BasicIfDirective.expected.test @@ -1,4 +1,4 @@ +using System; #if DEBUG using Insite.Bad; #endif -using System; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test index ba7a50c1b..e7283389e 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_Basics.test @@ -2,4 +2,5 @@ global using Global; using System; using Custom; using static Expression; +using Point = (int x, int y); using Index = Microsoft.Framework.Index; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.expected.test deleted file mode 100644 index dfd91ca97..000000000 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.expected.test +++ /dev/null @@ -1,8 +0,0 @@ -#if BUILD_MSI_TASKS -using System; - -using Microsoft.Build.Framework; - -namespace RepoTasks; - -#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.test index 3b638181a..492c1c88a 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase1.test @@ -4,4 +4,6 @@ using Microsoft.Build.Framework; namespace RepoTasks; +class ClassName { } + #endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.expected.test new file mode 100644 index 000000000..dd004adfc --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase2.expected.test @@ -0,0 +1,6 @@ +using System.IO; +#if DEBUG +using System; +#else +using Microsoft; +#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.expected.test new file mode 100644 index 000000000..eb9e783fa --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase3.expected.test @@ -0,0 +1,6 @@ +using System.IO; +#if !DEBUG +using System; +#else +using Microsoft; +#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase4.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase4.test new file mode 100644 index 000000000..dc41fe9c5 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase4.test @@ -0,0 +1,8 @@ +#if DEBUG +using A; +#else +using B; +#endif +#if !DEBUG +using C; +#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.expected.test new file mode 100644 index 000000000..ee20b766f --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.expected.test @@ -0,0 +1,10 @@ +using C; +#if DEBUG +using A; +#else +using B; +#endif + +#if !DEBUG +using C; +#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.test new file mode 100644 index 000000000..86ee53c56 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_EdgeCase5.test @@ -0,0 +1,9 @@ +#if DEBUG +using A; +#else +using B; +#endif +using C; +#if !DEBUG +using C; +#endif diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.expected.test index ccb96c1d6..c9805adb3 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.expected.test @@ -1,6 +1,4 @@ using System; - -using System.Web; // keeping the blank line above this in the expected is odd, but there isn't a good way to know which spaces to remove besides the first one - -using AWord; // the blank line above here tests that we don't add two blank lines between system and non-system +using System.Web; +using AWord; using ZWord; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.test index f5c6fa852..c1389e511 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsSystemToTop.test @@ -1,6 +1,4 @@ using ZWord; - -using AWord; // the blank line above here tests that we don't add two blank lines between system and non-system - -using System.Web; // keeping the blank line above this in the expected is odd, but there isn't a good way to know which spaces to remove besides the first one +using AWord; +using System.Web; using System; diff --git a/Src/CSharpier/CSharpFormatter.cs b/Src/CSharpier/CSharpFormatter.cs index 408e3e7b7..a6cd52c1e 100644 --- a/Src/CSharpier/CSharpFormatter.cs +++ b/Src/CSharpier/CSharpFormatter.cs @@ -107,6 +107,7 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult) var formattingContext = new FormattingContext { LineEnding = lineEnding }; var document = Node.Print(rootNode, formattingContext); var formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding); + DebugLogger.Log(formattedCode); var reorderedModifiers = formattingContext.ReorderedModifiers; foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree)) diff --git a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs index 2e7619812..025fd5608 100644 --- a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs +++ b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs @@ -29,10 +29,31 @@ bool printExtraLines { var docs = new List(); - // what is this is an #if? only comments - docs.Add(Token.PrintLeadingTrivia(usings.First().GetLeadingTrivia(), context)); + var initialComments = new List(); + var otherStuff = new List(); + var foundOtherStuff = false; + foreach (var leadingTrivia in usings.First().GetLeadingTrivia()) + { + if (leadingTrivia.RawSyntaxKind() == SyntaxKind.IfDirectiveTrivia) + { + foundOtherStuff = true; + } + + if (foundOtherStuff) + { + otherStuff.Add(leadingTrivia); + } + else + { + initialComments.Add(leadingTrivia); + } + } + + docs.Add(Token.PrintLeadingTrivia(new SyntaxTriviaList(initialComments), context)); var isFirst = true; - foreach (var groupOfUsingData in GroupUsings(usings, context)) + foreach ( + var groupOfUsingData in GroupUsings(usings, new SyntaxTriviaList(otherStuff), context) + ) { foreach (var usingData in groupOfUsingData) { @@ -65,6 +86,7 @@ bool printExtraLines private static IEnumerable> GroupUsings( SyntaxList usings, + SyntaxTriviaList otherStuff, FormattingContext context ) { @@ -72,10 +94,7 @@ FormattingContext context 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) @@ -97,8 +116,8 @@ Doc PrintStuff(UsingDirectiveSyntax value) { // TODO what about something with comments and a close #endif? return isFirst - ? Doc.Null - : Doc.Concat(Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context)); + ? Token.PrintLeadingTrivia(otherStuff, context) + : Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context); } if (ifCount > 0) @@ -115,7 +134,9 @@ Doc PrintStuff(UsingDirectiveSyntax value) { if (openIf) { - leftOvers.Add(new UsingData { LeadingTrivia = PrintStuff(usingDirective) }); + directiveGroup.Add( + new UsingData { LeadingTrivia = PrintStuff(usingDirective) } + ); } var usingData = new UsingData @@ -146,35 +167,11 @@ Doc PrintStuff(UsingDirectiveSyntax value) isFirst = false; } - if (globalUsings.Any()) - { - yield return globalUsings.OrderBy(o => o.Using!, Comparer).ToList(); - } - - if (regularUsings.Any()) - { - yield return regularUsings.OrderBy(o => o.Using!, Comparer).ToList(); - } - - if (directiveGroup.Any()) - { - yield return directiveGroup.OrderBy(o => o.Using!, Comparer).ToList(); - } - - if (leftOvers.Any()) - { - yield return leftOvers; - } - - if (staticUsings.Any()) - { - yield return staticUsings.OrderBy(o => o.Using!, Comparer).ToList(); - } - - if (aliasUsings.Any()) - { - yield return aliasUsings.OrderBy(o => o.Using!, Comparer).ToList(); - } + yield return globalUsings.OrderBy(o => o.Using!, Comparer).ToList(); + yield return regularUsings.OrderBy(o => o.Using!, Comparer).ToList(); + yield return directiveGroup; + yield return staticUsings.OrderBy(o => o.Using!, Comparer).ToList(); + yield return aliasUsings.OrderBy(o => o.Using!, Comparer).ToList(); } private class UsingData @@ -195,11 +192,6 @@ private static bool IsSystemName(NameSyntax value) } } - private class UsingGroup - { - public required List Usings { get; init; } - } - private class DefaultOrder : IComparer { public int Compare(UsingDirectiveSyntax? x, UsingDirectiveSyntax? y) @@ -219,10 +211,6 @@ public int Compare(UsingDirectiveSyntax? x, UsingDirectiveSyntax? y) int Return(int value) { - DebugLogger.Log( - $"{x.ToFullString().Trim()} {xIsSystem} vs {y.ToFullString().Trim()} {yIsSystem} = {value}" - ); - return value; }