Skip to content

Commit d2cd509

Browse files
Phaultbelav
andauthored
Sync exposed CodeFormatterOptions with currently supported cli options (#1176)
* Sync exposed CodeFormatterOptions with currently supported cli options Closes #1172 * Formatting files --------- Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 84b5bc4 commit d2cd509

6 files changed

Lines changed: 80 additions & 10 deletions

File tree

Src/CSharpier.Tests/CodeFormatterTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ public void Format_Should_Use_Width()
6262
result.Code.Should().Be("var someVariable =\n someValue;\n");
6363
}
6464

65+
[Test]
66+
public void Format_Should_Use_IndentStyle()
67+
{
68+
var code = "void someMethod() { var someVariable = someValue; }";
69+
var result = CodeFormatter.Format(
70+
code,
71+
new CodeFormatterOptions { IndentStyle = IndentStyle.Tabs, IndentSize = 1 }
72+
);
73+
74+
result.Code.Should().Be("void someMethod()\n{\n\tvar someVariable = someValue;\n}\n");
75+
}
76+
77+
[Test]
78+
public void Format_Should_Use_IndentWidth()
79+
{
80+
var code = "void someMethod() { var someVariable = someValue; }";
81+
var result = CodeFormatter.Format(
82+
code,
83+
new CodeFormatterOptions { IndentStyle = IndentStyle.Spaces, IndentSize = 1 }
84+
);
85+
86+
result.Code.Should().Be("void someMethod()\n{\n var someVariable = someValue;\n}\n");
87+
}
88+
89+
[Test]
90+
public void Format_Should_Use_EndOfLine()
91+
{
92+
var code = "var someVariable = someValue;";
93+
var result = CodeFormatter.Format(
94+
code,
95+
new CodeFormatterOptions { EndOfLine = EndOfLine.CRLF }
96+
);
97+
98+
result.Code.Should().Be("var someVariable = someValue;\r\n");
99+
}
100+
65101
[TestCase("\n")]
66102
[TestCase("\r\n")]
67103
public void Format_Should_Get_Line_Endings_With_SyntaxTree(string lineEnding)

Src/CSharpier/CodeFormatter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public static Task<CodeFormatterResult> FormatAsync(
2121

2222
return CSharpFormatter.FormatAsync(
2323
code,
24-
new PrinterOptions { Width = options.Width },
24+
new PrinterOptions
25+
{
26+
Width = options.Width,
27+
UseTabs = options.IndentStyle == IndentStyle.Tabs,
28+
TabWidth = options.IndentSize,
29+
EndOfLine = options.EndOfLine
30+
},
2531
cancellationToken
2632
);
2733
}
@@ -44,7 +50,13 @@ public static Task<CodeFormatterResult> FormatAsync(
4450

4551
return CSharpFormatter.FormatAsync(
4652
syntaxTree,
47-
new PrinterOptions { Width = options.Width },
53+
new PrinterOptions
54+
{
55+
Width = options.Width,
56+
UseTabs = options.IndentStyle == IndentStyle.Tabs,
57+
TabWidth = options.IndentSize,
58+
EndOfLine = options.EndOfLine
59+
},
4860
SourceCodeKind.Regular,
4961
cancellationToken
5062
);

Src/CSharpier/CodeFormatterOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@ namespace CSharpier;
33
public class CodeFormatterOptions
44
{
55
public int Width { get; init; } = 100;
6+
public IndentStyle IndentStyle { get; init; } = IndentStyle.Spaces;
7+
public int IndentSize { get; init; } = 4;
8+
public EndOfLine EndOfLine { get; init; } = EndOfLine.Auto;
9+
}
10+
11+
public enum IndentStyle
12+
{
13+
Spaces,
14+
Tabs
615
}

Src/CSharpier/EndOfLine.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CSharpier;
2+
3+
public enum EndOfLine
4+
{
5+
Auto,
6+
CRLF,
7+
LF
8+
}

Src/CSharpier/PrinterOptions.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,3 @@ internal static string GetLineEnding(string code, PrinterOptions printerOptions)
3333
return "\n";
3434
}
3535
}
36-
37-
internal enum EndOfLine
38-
{
39-
Auto,
40-
CRLF,
41-
LF
42-
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-

1+
CSharpier.CodeFormatterOptions.EndOfLine.get -> CSharpier.EndOfLine
2+
CSharpier.CodeFormatterOptions.EndOfLine.init -> void
3+
CSharpier.CodeFormatterOptions.IndentSize.get -> int
4+
CSharpier.CodeFormatterOptions.IndentSize.init -> void
5+
CSharpier.CodeFormatterOptions.IndentStyle.get -> CSharpier.IndentStyle
6+
CSharpier.CodeFormatterOptions.IndentStyle.init -> void
7+
CSharpier.EndOfLine
8+
CSharpier.EndOfLine.Auto = 0 -> CSharpier.EndOfLine
9+
CSharpier.EndOfLine.CRLF = 1 -> CSharpier.EndOfLine
10+
CSharpier.EndOfLine.LF = 2 -> CSharpier.EndOfLine
11+
CSharpier.IndentStyle
12+
CSharpier.IndentStyle.Spaces = 0 -> CSharpier.IndentStyle
13+
CSharpier.IndentStyle.Tabs = 1 -> CSharpier.IndentStyle

0 commit comments

Comments
 (0)