From 482e02c8c625f72e84a00df750493498b3d8da31 Mon Sep 17 00:00:00 2001 From: pisolofin Date: Tue, 24 Sep 2024 16:21:35 +0200 Subject: [PATCH] Support custom file extensions with editor config (#1346) closes #1273 --- .../EditorConfig/EditorConfigSections.cs | 17 +++++++++++----- Src/CSharpier.Cli/EditorConfig/Section.cs | 2 ++ Src/CSharpier.Tests/OptionsProviderTests.cs | 20 ++++++++++++++++++- docs/Configuration.md | 16 +++++++++++++-- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs b/Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs index a500142d0..4902608a2 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) { @@ -77,6 +81,7 @@ private class ResolvedConfiguration public int? TabWidth { get; } public int? MaxLineLength { get; } public EndOfLine? EndOfLine { get; } + public string? Formatter { get; } public BraceNewLine? NewLineBeforeOpenBrace { get; } public bool? NewLineBeforeElse { get; } @@ -130,6 +135,8 @@ public ResolvedConfiguration(List
sections) this.EndOfLine = result; } + this.Formatter = sections.LastOrDefault(o => o.Formatter is not null)?.Formatter; + var newLineBeforeOpenBrace = sections .LastOrDefault(o => o.NewLineBeforeOpenBrace != null) ?.NewLineBeforeOpenBrace; diff --git a/Src/CSharpier.Cli/EditorConfig/Section.cs b/Src/CSharpier.Cli/EditorConfig/Section.cs index 544ef6c92..1fbc30068 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 string? NewLineBeforeOpenBrace { get; } public string? NewLineBeforeElse { get; } public string? NewLineBeforeCatch { get; } @@ -27,6 +28,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"]; this.NewLineBeforeOpenBrace = section.Keys["csharp_new_line_before_open_brace"]; this.NewLineBeforeElse = section.Keys["csharp_new_line_before_else"]; this.NewLineBeforeCatch = section.Keys["csharp_new_line_before_catch"]; diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index 4e2e630b1..28d11f955 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -511,7 +511,7 @@ public async Task Should_Return_UsePrettierStyleTrailingCommas_With_Yaml(bool va } [Test] - public async Task Should_Return_TabWidth_For_Overrid() + public async Task Should_Return_TabWidth_For_Override() { var context = new TestContext(); context.WhenAFileExists( @@ -836,6 +836,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 673b61e07..1b1d6a5d1 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -251,6 +251,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 @@ -284,7 +285,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 @@ -293,9 +294,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 ``` +