Skip to content

Commit

Permalink
Sync exposed CodeFormatterOptions with currently supported cli options
Browse files Browse the repository at this point in the history
Closes belav#1172
  • Loading branch information
Phault committed Feb 12, 2024
1 parent 84b5bc4 commit de502ca
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
27 changes: 27 additions & 0 deletions Src/CSharpier.Tests/CodeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ public void Format_Should_Use_Width()

result.Code.Should().Be("var someVariable =\n someValue;\n");
}

[Test]
public void Format_Should_Use_IndentStyle()
{
var code = "void someMethod() { var someVariable = someValue; }";
var result = CodeFormatter.Format(code, new CodeFormatterOptions { IndentStyle = IndentStyle.Tabs, IndentSize = 1});

result.Code.Should().Be("void someMethod()\n{\n\tvar someVariable = someValue;\n}\n");
}

[Test]
public void Format_Should_Use_IndentWidth()
{
var code = "void someMethod() { var someVariable = someValue; }";
var result = CodeFormatter.Format(code, new CodeFormatterOptions { IndentStyle = IndentStyle.Spaces, IndentSize = 1});

result.Code.Should().Be("void someMethod()\n{\n var someVariable = someValue;\n}\n");
}

[Test]
public void Format_Should_Use_EndOfLine()
{
var code = "var someVariable = someValue;";
var result = CodeFormatter.Format(code, new CodeFormatterOptions { EndOfLine = EndOfLine.CRLF });

result.Code.Should().Be("var someVariable = someValue;\r\n");
}

[TestCase("\n")]
[TestCase("\r\n")]
Expand Down
16 changes: 14 additions & 2 deletions Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public static Task<CodeFormatterResult> FormatAsync(

return CSharpFormatter.FormatAsync(
code,
new PrinterOptions { Width = options.Width },
new PrinterOptions
{
Width = options.Width,
UseTabs = options.IndentStyle == IndentStyle.Tabs,
TabWidth = options.IndentSize,
EndOfLine = options.EndOfLine
},
cancellationToken
);
}
Expand All @@ -44,7 +50,13 @@ public static Task<CodeFormatterResult> FormatAsync(

return CSharpFormatter.FormatAsync(
syntaxTree,
new PrinterOptions { Width = options.Width },
new PrinterOptions
{
Width = options.Width,
UseTabs = options.IndentStyle == IndentStyle.Tabs,
TabWidth = options.IndentSize,
EndOfLine = options.EndOfLine
},
SourceCodeKind.Regular,
cancellationToken
);
Expand Down
9 changes: 9 additions & 0 deletions Src/CSharpier/CodeFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ namespace CSharpier;
public class CodeFormatterOptions
{
public int Width { get; init; } = 100;
public IndentStyle IndentStyle { get; init; } = IndentStyle.Spaces;
public int IndentSize { get; init; } = 4;
public EndOfLine EndOfLine { get; init; } = EndOfLine.Auto;
}

public enum IndentStyle
{
Spaces,
Tabs
}
8 changes: 8 additions & 0 deletions Src/CSharpier/EndOfLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CSharpier;

public enum EndOfLine
{
Auto,
CRLF,
LF
}
7 changes: 0 additions & 7 deletions Src/CSharpier/PrinterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,3 @@ internal static string GetLineEnding(string code, PrinterOptions printerOptions)
return "\n";
}
}

internal enum EndOfLine
{
Auto,
CRLF,
LF
}
14 changes: 13 additions & 1 deletion Src/CSharpier/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@

CSharpier.CodeFormatterOptions.EndOfLine.get -> CSharpier.EndOfLine
CSharpier.CodeFormatterOptions.EndOfLine.init -> void
CSharpier.CodeFormatterOptions.IndentSize.get -> int
CSharpier.CodeFormatterOptions.IndentSize.init -> void
CSharpier.CodeFormatterOptions.IndentStyle.get -> CSharpier.IndentStyle
CSharpier.CodeFormatterOptions.IndentStyle.init -> void
CSharpier.EndOfLine
CSharpier.EndOfLine.Auto = 0 -> CSharpier.EndOfLine
CSharpier.EndOfLine.CRLF = 1 -> CSharpier.EndOfLine
CSharpier.EndOfLine.LF = 2 -> CSharpier.EndOfLine
CSharpier.IndentStyle
CSharpier.IndentStyle.Spaces = 0 -> CSharpier.IndentStyle
CSharpier.IndentStyle.Tabs = 1 -> CSharpier.IndentStyle

0 comments on commit de502ca

Please sign in to comment.