diff --git a/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs b/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs index e7d14ccc2..ddb82d51d 100644 --- a/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs +++ b/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs @@ -11,15 +11,19 @@ internal class EditorConfigSections public PrinterOptions? ConvertToPrinterOptions(string filePath) { - if (!(filePath.EndsWith(".cs") || filePath.EndsWith(".csx"))) + var sections = this.SectionsIncludingParentFiles.Where(o => o.IsMatch(filePath)).ToList(); + var resolvedConfiguration = new ResolvedConfiguration(sections); + + var formatter = + resolvedConfiguration.Formatter + ?? (filePath.EndsWith(".cs") || filePath.EndsWith(".csx") ? "csharp" : null); + + if (formatter == null) { return null; } - var sections = this.SectionsIncludingParentFiles.Where(o => o.IsMatch(filePath)).ToList(); - var resolvedConfiguration = new ResolvedConfiguration(sections); - - var printerOptions = new PrinterOptions { Formatter = "csharp" }; + var printerOptions = new PrinterOptions { Formatter = formatter }; if (resolvedConfiguration.MaxLineLength is { } maxLineLength) { @@ -56,6 +60,7 @@ private class ResolvedConfiguration public int? TabWidth { get; } public int? MaxLineLength { get; } public EndOfLine? EndOfLine { get; } + public string? Formatter { get; } public ResolvedConfiguration(List
sections) { @@ -100,6 +105,8 @@ public ResolvedConfiguration(List
sections) { this.EndOfLine = result; } + + this.Formatter = sections.LastOrDefault(o => o.Formatter is not null)?.Formatter; } } } diff --git a/Src/CSharpier.Cli/EditorConfig/Section.cs b/Src/CSharpier.Cli/EditorConfig/Section.cs index 50f98ed7f..f50f312d2 100644 --- a/Src/CSharpier.Cli/EditorConfig/Section.cs +++ b/Src/CSharpier.Cli/EditorConfig/Section.cs @@ -11,6 +11,7 @@ internal class Section public string? TabWidth { get; } public string? MaxLineLength { get; } public string? EndOfLine { get; } + public string? Formatter { get; } public Section(SectionData section, string directory) { @@ -20,6 +21,7 @@ public Section(SectionData section, string directory) this.TabWidth = section.Keys["tab_width"]; this.MaxLineLength = section.Keys["max_line_length"]; this.EndOfLine = section.Keys["end_of_line"]; + this.Formatter = section.Keys["csharpier_formatter"]; } public bool IsMatch(string fileName) diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index 00dfe6354..be40d21ba 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -203,7 +203,7 @@ public async Task Should_Return_UseTabs_With_Yaml() } [Test] - public async Task Should_Return_TabWidth_For_Overrid() + public async Task Should_Return_TabWidth_For_Override() { var context = new TestContext(); context.WhenAFileExists( @@ -528,6 +528,24 @@ public async Task Should_Support_EditorConfig_Tabs_With_Glob_Braces_Multiples() result.IndentSize.Should().Be(2); } + [Test] + public async Task Should_Return_IndentSize_For_Formatter_In_Editorconfig() + { + var context = new TestContext(); + context.WhenAFileExists( + "c:/test/.editorconfig", + """ + [*.cst] + indent_size = 2 + csharpier_formatter = csharp + """ + ); + + var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cst"); + + result.IndentSize.Should().Be(2); + } + [Test] public async Task Should_Find_EditorConfig_In_Parent_Directory() { diff --git a/docs/Configuration.md b/docs/Configuration.md index ec56fbe7f..58a712990 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -81,6 +81,7 @@ The long term plan is to improve Csharpier's ability to determine the symbol set ### Configuration Overrides ### _First available in 0.29.0_ + Overrides allows you to specify different configuration options based on glob patterns. This can be used to format non-standard extensions, or to change options based on file path. Top level options will apply to `**/*.{cs,csx}` ```json @@ -114,7 +115,7 @@ _First available in 0.26.0_ CSharpier supports configuration via an `.editorconfig` file. A `.csharpierrc*` file in the same directory will take priority. ```ini -[*] +[*.{cs|csx}] # Non-configurable behaviors charset = utf-8 insert_final_newline = true @@ -123,9 +124,20 @@ dotnet_sort_system_directives_first = true dotnet_separate_import_directive_groups = false # Configurable behaviors -# end_of_line = lf - there is no 'auto' with a .editorconfig +# end_of_line = lf - there is no 'auto' with an .editorconfig indent_style = space indent_size = 4 max_line_length = 100 +``` + +_First available in 0.29.2_ +Formatting non-standard file extensions using csharpier can be accomplished with the `csharpier_formatter` option +```ini +[*.cst] +csharpier_formatter = csharp +indent_style = space +indent_size = 2 +max_line_length = 80 ``` +