Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageVersion Include="System.IO.Abstractions" Version="22.1.1" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.0.16" />
<PackageVersion Include="System.IO.Hashing" Version="9.0.10" />
<PackageVersion Include="System.Management.Automation" Version="7.4.6" />
<PackageVersion Include="System.Text.Encoding.CodePages" Version="10.0.5" />
<PackageVersion Include="System.Text.Json" Version="10.0.5" />
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
Expand Down
6 changes: 4 additions & 2 deletions Src/CSharpier.Cli/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ void WriteLine(string? value = null)
}
}

private static ConsoleColor GetColorLevel(LogLevel logLevel) =>
logLevel switch
private static ConsoleColor GetColorLevel(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Critical => ConsoleColor.DarkRed,
LogLevel.Error => ConsoleColor.DarkRed,
LogLevel.Warning => ConsoleColor.DarkYellow,
_ => ConsoleColor.White,
};
}

public bool IsEnabled(LogLevel logLevel)
{
Expand Down
8 changes: 5 additions & 3 deletions Src/CSharpier.Cli/EditorConfig/GlobMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private struct MatchContext
this.myOptions.IgnoreCase
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;

private readonly char[] PathSeparatorChars =>
this.myOptions.AllowWindowsPaths ? ourWinPathSeparators : ourUnixPathSeparators;

Expand Down Expand Up @@ -634,11 +635,12 @@ private readonly bool CheckDot(int dotPos)
}
}

private static bool IsPathSeparator(GlobMatcherOptions options, char c) =>
private static bool IsPathSeparator(GlobMatcherOptions options, char c)
{
// windows: need to use /, not \
// On other platforms, \ is a valid (albeit bad) filename char.
c == '/'
|| options.AllowWindowsPaths && c == '\\';
return c == '/' || options.AllowWindowsPaths && c == '\\';
}

private class PatternCase : List<IPatternElement>
{
Expand Down
10 changes: 8 additions & 2 deletions Src/CSharpier.Core/CSharp/CSharpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,19 @@ public static Task<CodeFormatterResult> FormatAsync(
internal static Task<CodeFormatterResult> FormatAsync(
string code,
PrinterOptions printerOptions
) => FormatAsync(code, printerOptions, CancellationToken.None);
)
{
return FormatAsync(code, printerOptions, CancellationToken.None);
}

internal static Task<CodeFormatterResult> FormatAsync(
string code,
PrinterOptions printerOptions,
CancellationToken cancellationToken
) => FormatAsync(code, printerOptions, SourceCodeKind.Regular, cancellationToken);
)
{
return FormatAsync(code, printerOptions, SourceCodeKind.Regular, cancellationToken);
}

internal static Task<CodeFormatterResult> FormatAsync(
string code,
Expand Down
12 changes: 8 additions & 4 deletions Src/CSharpier.Core/CSharp/SyntaxPrinter/CSharpierIgnore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ internal static partial class CSharpierIgnore
);
#endif

public static bool HasIgnoreComment(SyntaxNode syntaxNode) =>
Token.HasLeadingCommentMatching(syntaxNode, IgnoreRegex);
public static bool HasIgnoreComment(SyntaxNode syntaxNode)
{
return Token.HasLeadingCommentMatching(syntaxNode, IgnoreRegex);
}

public static bool HasIgnoreComment(SyntaxToken syntaxToken) =>
Token.HasLeadingCommentMatching(syntaxToken, IgnoreRegex);
public static bool HasIgnoreComment(SyntaxToken syntaxToken)
{
return Token.HasLeadingCommentMatching(syntaxToken, IgnoreRegex);
}

public static bool IsNodeIgnored(SyntaxNode syntaxNode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ namespace CSharpier.Core.CSharp.SyntaxPrinter.SyntaxNodePrinters;

internal static class TupleExpression
{
public static Doc Print(TupleExpressionSyntax node, CSharpPrintingContext context) =>
Doc.Group(
public static Doc Print(TupleExpressionSyntax node, CSharpPrintingContext context)
{
return Doc.Group(
ArgumentListLike.Print(
node.OpenParenToken,
node.Arguments,
node.CloseParenToken,
context
)
);
}
}
20 changes: 14 additions & 6 deletions Src/CSharpier.Core/CSharp/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,24 @@ void AddLeadingComment(CommentType commentType)
return docs.Count > 0 ? Doc.Concat(docs) : Doc.Null;
}

private static bool IsSingleLineComment(SyntaxKind kind) =>
kind
private static bool IsSingleLineComment(SyntaxKind kind)
{
return kind
is SyntaxKind.SingleLineDocumentationCommentTrivia
or SyntaxKind.SingleLineCommentTrivia;
}

private static bool IsMultiLineComment(SyntaxKind kind) =>
kind is SyntaxKind.MultiLineCommentTrivia or SyntaxKind.MultiLineDocumentationCommentTrivia;
private static bool IsMultiLineComment(SyntaxKind kind)
{
return kind
is SyntaxKind.MultiLineCommentTrivia
or SyntaxKind.MultiLineDocumentationCommentTrivia;
}

private static bool IsRegion(SyntaxKind kind) =>
kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;
private static bool IsRegion(SyntaxKind kind)
{
return kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;
}

public static Doc PrintTrailingTrivia(SyntaxToken node)
{
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Core/CSharpier.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
</PackageReference>
<PackageReference Include="System.IO.Abstractions" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<PackageReference Include="System.Management.Automation" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Remove="PowerShell\**\*.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net10.0'">
<PackageReference Include="System.Text.Json" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Core/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ CancellationToken cancellationToken
cancellationToken
),
Formatter.XML => await XmlFormatter.FormatAsync(fileContents, options),
#if !NETSTANDARD2_0
Formatter.PowerShell => await PowerShell.PowerShellFormatter.FormatAsync(
fileContents,
options
),
#endif
_ => new CodeFormatterResult { FailureMessage = "Is an unsupported file type." },
};
}
Expand Down
6 changes: 4 additions & 2 deletions Src/CSharpier.Core/DocTypes/StringDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ internal sealed class StringDoc(string value, bool isDirective = false) : Doc
public string Value { get; } = value;
public bool IsDirective { get; } = isDirective;

public static StringDoc Create(string value) =>
value == " " ? SpaceStringDoc : new StringDoc(value);
public static StringDoc Create(string value)
{
return value == " " ? SpaceStringDoc : new StringDoc(value);
}

public static StringDoc Create(SyntaxToken token)
{
Expand Down
12 changes: 12 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/ExitStatement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Management.Automation.Language;
using CSharpier.Core.DocTypes;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal static class ExitStatement
{
public static Doc Print(ExitStatementAst node, PrintContext context)
{
return "TODO";
}
}
19 changes: 19 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/ForEachStatement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Management.Automation.Language;
using CSharpier.Core.DocTypes;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal static class ForEachStatement
{
internal static Doc Print(ForEachStatementAst node, PrintContext context)
{
return Doc.Concat(
"foreach (",
Verbatim.Print(node.Variable.Extent),
" in ",
Verbatim.Print(node.Condition.Extent),
") ",
StatementBlock.Print(node.Body, context)
);
}
}
62 changes: 62 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/FunctionDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Management.Automation.Language;
using CSharpier.Core.DocTypes;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal static class FunctionDefinition
{
internal static Doc Print(FunctionDefinitionAst node, PrintContext context)
{
var keyword = node.IsFilter ? "filter " : "function ";
var body = node.Body;

if (
body.BeginBlock is not null
|| body.ProcessBlock is not null
|| body.DynamicParamBlock is not null
|| body.EndBlock is null
|| !body.EndBlock.Unnamed
)
{
return Doc.Concat(keyword, node.Name, " ", Verbatim.Print(body.Extent));
}

var inner = new List<Doc>();
if (body.ParamBlock is not null)
{
inner.Add(Verbatim.Print(body.ParamBlock.Extent));
inner.Add(Doc.HardLine);
}

if (body.EndBlock.Statements.Count > 0)
{
if (inner.Count > 0)
{
inner.Add(Doc.HardLine);
}

inner.Add(
Statements.Print(
body.EndBlock.Statements,
context,
body.Extent.StartOffset,
body.Extent.EndOffset
)
);
}

if (inner.Count == 0)
{
return Doc.Concat(keyword, node.Name, " { }");
}

return Doc.Concat(
keyword,
node.Name,
" {",
Doc.Indent(Doc.HardLine, Doc.Concat(inner)),
Doc.HardLine,
"}"
);
}
}
29 changes: 29 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/IfStatement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Management.Automation.Language;
using CSharpier.Core.DocTypes;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal static class IfStatement
{
internal static Doc Print(IfStatementAst node, PrintContext context)
{
var docs = new List<Doc>();

for (var i = 0; i < node.Clauses.Count; i++)
{
var (condition, body) = (node.Clauses[i].Item1, node.Clauses[i].Item2);
docs.Add(i == 0 ? "if (" : " elseif (");
docs.Add(Verbatim.Print(condition.Extent));
docs.Add(") ");
docs.Add(StatementBlock.Print(body, context));
}

if (node.ElseClause is not null)
{
docs.Add(" else ");
docs.Add(StatementBlock.Print(node.ElseClause, context));
}

return Doc.Concat(docs);
}
}
22 changes: 22 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Management.Automation.Language;
using CSharpier.Core.DocTypes;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal static class Node
{
internal static Doc Print(Ast node, PrintContext context)
{
return node switch
{
ExitStatementAst exitStatement => ExitStatement.Print(exitStatement, context),
ForEachStatementAst forEach => ForEachStatement.Print(forEach, context),
FunctionDefinitionAst function => FunctionDefinition.Print(function, context),
IfStatementAst ifStatement => IfStatement.Print(ifStatement, context),
ScriptBlockAst scriptBlock => ScriptBlock.Print(scriptBlock, context),
TryStatementAst tryStatement => TryStatement.Print(tryStatement, context),
WhileStatementAst whileStatement => WhileStatement.Print(whileStatement, context),
_ => node.GetType().ToString(),
};
}
}
30 changes: 30 additions & 0 deletions Src/CSharpier.Core/PowerShell/AstPrinters/PrintContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Management.Automation.Language;

namespace CSharpier.Core.PowerShell.AstPrinters;

internal sealed class PrintContext(IReadOnlyList<IScriptExtent> comments)
{
internal bool HasCommentIn(IScriptExtent extent)
{
foreach (var comment in comments)
{
if (comment.StartOffset >= extent.StartOffset && comment.StartOffset < extent.EndOffset)
{
return true;
}
}

return false;
}

internal IEnumerable<IScriptExtent> CommentsBetween(int startOffset, int endOffset)
{
foreach (var comment in comments)
{
if (comment.StartOffset >= startOffset && comment.StartOffset < endOffset)
{
yield return comment;
}
}
}
}
Loading
Loading