Skip to content

Commit 2e43384

Browse files
committed
POC for powershell formatting
1 parent 1ba4296 commit 2e43384

47 files changed

Lines changed: 1062 additions & 50 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageVersion Include="System.IO.Abstractions" Version="22.1.1" />
3030
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.0.16" />
3131
<PackageVersion Include="System.IO.Hashing" Version="9.0.10" />
32+
<PackageVersion Include="System.Management.Automation" Version="7.4.6" />
3233
<PackageVersion Include="System.Text.Encoding.CodePages" Version="10.0.5" />
3334
<PackageVersion Include="System.Text.Json" Version="10.0.5" />
3435
<PackageVersion Include="YamlDotNet" Version="16.3.0" />

Src/CSharpier.Cli/ConsoleLogger.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ void WriteLine(string? value = null)
8989
}
9090
}
9191

92-
private static ConsoleColor GetColorLevel(LogLevel logLevel) =>
93-
logLevel switch
92+
private static ConsoleColor GetColorLevel(LogLevel logLevel)
93+
{
94+
return logLevel switch
9495
{
9596
LogLevel.Critical => ConsoleColor.DarkRed,
9697
LogLevel.Error => ConsoleColor.DarkRed,
9798
LogLevel.Warning => ConsoleColor.DarkYellow,
9899
_ => ConsoleColor.White,
99100
};
101+
}
100102

101103
public bool IsEnabled(LogLevel logLevel)
102104
{

Src/CSharpier.Cli/EditorConfig/GlobMatcher.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,25 @@ private struct MatchContext
140140
private int myLastAsteriskItem;
141141
private int myNextPositionForAsterisk;
142142

143-
private readonly StringComparison ComparisonType =>
144-
this.myOptions.IgnoreCase
145-
? StringComparison.OrdinalIgnoreCase
146-
: StringComparison.Ordinal;
147-
private readonly char[] PathSeparatorChars =>
148-
this.myOptions.AllowWindowsPaths ? ourWinPathSeparators : ourUnixPathSeparators;
143+
private readonly StringComparison ComparisonType
144+
{
145+
get
146+
{
147+
return this.myOptions.IgnoreCase
148+
? StringComparison.OrdinalIgnoreCase
149+
: StringComparison.Ordinal;
150+
}
151+
}
152+
153+
private readonly char[] PathSeparatorChars
154+
{
155+
get
156+
{
157+
return this.myOptions.AllowWindowsPaths
158+
? ourWinPathSeparators
159+
: ourUnixPathSeparators;
160+
}
161+
}
149162

150163
public MatchContext(GlobMatcherOptions options, string str, PatternCase patternCase)
151164
{
@@ -634,11 +647,12 @@ private readonly bool CheckDot(int dotPos)
634647
}
635648
}
636649

637-
private static bool IsPathSeparator(GlobMatcherOptions options, char c) =>
650+
private static bool IsPathSeparator(GlobMatcherOptions options, char c)
651+
{
638652
// windows: need to use /, not \
639653
// On other platforms, \ is a valid (albeit bad) filename char.
640-
c == '/'
641-
|| options.AllowWindowsPaths && c == '\\';
654+
return c == '/' || options.AllowWindowsPaths && c == '\\';
655+
}
642656

643657
private class PatternCase : List<IPatternElement>
644658
{

Src/CSharpier.Cli/IConsole.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ public void WriteError(string value)
4242

4343
public ConsoleColor ForegroundColor
4444
{
45-
set => Console.ForegroundColor = value;
45+
set { Console.ForegroundColor = value; }
4646
}
4747

4848
public void ResetColor()
4949
{
5050
Console.ResetColor();
5151
}
5252

53-
public Encoding InputEncoding => Encoding.UTF8;
53+
public Encoding InputEncoding
54+
{
55+
get { return Encoding.UTF8; }
56+
}
5457
}

Src/CSharpier.Core/CSharp/CSharpFormatter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ public static Task<CodeFormatterResult> FormatAsync(
5151
internal static Task<CodeFormatterResult> FormatAsync(
5252
string code,
5353
PrinterOptions printerOptions
54-
) => FormatAsync(code, printerOptions, CancellationToken.None);
54+
)
55+
{
56+
return FormatAsync(code, printerOptions, CancellationToken.None);
57+
}
5558

5659
internal static Task<CodeFormatterResult> FormatAsync(
5760
string code,
5861
PrinterOptions printerOptions,
5962
CancellationToken cancellationToken
60-
) => FormatAsync(code, printerOptions, SourceCodeKind.Regular, cancellationToken);
63+
)
64+
{
65+
return FormatAsync(code, printerOptions, SourceCodeKind.Regular, cancellationToken);
66+
}
6167

6268
internal static Task<CodeFormatterResult> FormatAsync(
6369
string code,

Src/CSharpier.Core/CSharp/SyntaxPrinter/CSharpierIgnore.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ internal static partial class CSharpierIgnore
4747
);
4848
#endif
4949

50-
public static bool HasIgnoreComment(SyntaxNode syntaxNode) =>
51-
Token.HasLeadingCommentMatching(syntaxNode, IgnoreRegex);
50+
public static bool HasIgnoreComment(SyntaxNode syntaxNode)
51+
{
52+
return Token.HasLeadingCommentMatching(syntaxNode, IgnoreRegex);
53+
}
5254

53-
public static bool HasIgnoreComment(SyntaxToken syntaxToken) =>
54-
Token.HasLeadingCommentMatching(syntaxToken, IgnoreRegex);
55+
public static bool HasIgnoreComment(SyntaxToken syntaxToken)
56+
{
57+
return Token.HasLeadingCommentMatching(syntaxToken, IgnoreRegex);
58+
}
5559

5660
public static bool IsNodeIgnored(SyntaxNode syntaxNode)
5761
{

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/TupleExpression.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ namespace CSharpier.Core.CSharp.SyntaxPrinter.SyntaxNodePrinters;
55

66
internal static class TupleExpression
77
{
8-
public static Doc Print(TupleExpressionSyntax node, PrintingContext context) =>
9-
Doc.Group(
8+
public static Doc Print(TupleExpressionSyntax node, PrintingContext context)
9+
{
10+
return Doc.Group(
1011
ArgumentListLike.Print(
1112
node.OpenParenToken,
1213
node.Arguments,
1314
node.CloseParenToken,
1415
context
1516
)
1617
);
18+
}
1719
}

Src/CSharpier.Core/CSharp/SyntaxPrinter/Token.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,24 @@ void AddLeadingComment(CommentType commentType)
390390
return docs.Count > 0 ? Doc.Concat(docs) : Doc.Null;
391391
}
392392

393-
private static bool IsSingleLineComment(SyntaxKind kind) =>
394-
kind
393+
private static bool IsSingleLineComment(SyntaxKind kind)
394+
{
395+
return kind
395396
is SyntaxKind.SingleLineDocumentationCommentTrivia
396397
or SyntaxKind.SingleLineCommentTrivia;
398+
}
397399

398-
private static bool IsMultiLineComment(SyntaxKind kind) =>
399-
kind is SyntaxKind.MultiLineCommentTrivia or SyntaxKind.MultiLineDocumentationCommentTrivia;
400+
private static bool IsMultiLineComment(SyntaxKind kind)
401+
{
402+
return kind
403+
is SyntaxKind.MultiLineCommentTrivia
404+
or SyntaxKind.MultiLineDocumentationCommentTrivia;
405+
}
400406

401-
private static bool IsRegion(SyntaxKind kind) =>
402-
kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;
407+
private static bool IsRegion(SyntaxKind kind)
408+
{
409+
return kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;
410+
}
403411

404412
public static Doc PrintTrailingTrivia(SyntaxToken node)
405413
{

Src/CSharpier.Core/CSharpier.Core.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
</PackageReference>
2525
<PackageReference Include="System.IO.Abstractions" />
2626
</ItemGroup>
27+
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
28+
<PackageReference Include="System.Management.Automation" />
29+
</ItemGroup>
30+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
31+
<Compile Remove="PowerShell\**\*.cs" />
32+
</ItemGroup>
2733
<ItemGroup Condition="'$(TargetFramework)' != 'net10.0'">
2834
<PackageReference Include="System.Text.Json" />
2935
</ItemGroup>

Src/CSharpier.Core/CodeFormatter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ CancellationToken cancellationToken
2727
cancellationToken
2828
),
2929
Formatter.XML => await XmlFormatter.FormatAsync(fileContents, options),
30+
#if !NETSTANDARD2_0
31+
Formatter.PowerShell => await PowerShell.PowerShellFormatter.FormatAsync(
32+
fileContents,
33+
options
34+
),
35+
#endif
3036
_ => new CodeFormatterResult { FailureMessage = "Is an unsupported file type." },
3137
};
3238
}

0 commit comments

Comments
 (0)