From 93baf5a6b003f0015785871fa539b0d73e18f488 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Wed, 8 Nov 2023 12:26:34 -0800 Subject: [PATCH] An existing csharpierrc file always takes priority over editorconfig (#991) closes #987 --- Docs/Configuration.md | 5 +++-- Src/CSharpier.Cli/Options/OptionsProvider.cs | 5 +---- Src/CSharpier.Tests/OptionsProviderTests.cs | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Docs/Configuration.md b/Docs/Configuration.md index 9c74c75e7..543c28f7c 100644 --- a/Docs/Configuration.md +++ b/Docs/Configuration.md @@ -7,8 +7,9 @@ CSharpier has support for a configuration file. You can use any of the following - A ```.csharpierrc.json``` or ```.csharpierrc.yaml``` file. - A ```.editorconfig``` file. See EditorConfig section below. -The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found. - +The configuration file will be resolved based on the location of the file being formatted. +- If a `.csharpierrc` file exists somewhere at or above the given file, that will be used. +- Otherwise if an `.editorconfig` file exists somewhere at or above the given file, that will be used. Respecting editorconfig inheritance. ### Configuration Options JSON ```json diff --git a/Src/CSharpier.Cli/Options/OptionsProvider.cs b/Src/CSharpier.Cli/Options/OptionsProvider.cs index 699ef24ba..4bdafde84 100644 --- a/Src/CSharpier.Cli/Options/OptionsProvider.cs +++ b/Src/CSharpier.Cli/Options/OptionsProvider.cs @@ -80,10 +80,7 @@ public PrinterOptions GetPrinterOptionsFor(string filePath) return new PrinterOptions(); } - if ( - (resolvedCSharpierConfig?.DirectoryName.Length ?? int.MinValue) - >= (resolvedEditorConfig?.DirectoryName.Length ?? int.MinValue) - ) + if (resolvedCSharpierConfig is not null) { return ConfigurationFileOptions.ConvertToPrinterOptions( resolvedCSharpierConfig!.CSharpierConfig diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index 62148270d..d05a255ea 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -446,7 +446,7 @@ public async Task Should_Prefer_CSharpierrc_In_SameFolder() } [Test] - public async Task Should_Prefer_Closer_EditorConfig() + public async Task Should_Not_Prefer_Closer_EditorConfig() { var context = new TestContext(); context.WhenAFileExists( @@ -462,7 +462,7 @@ public async Task Should_Prefer_Closer_EditorConfig() "c:/test", "c:/test/subfolder/test.cs" ); - result.TabWidth.Should().Be(2); + result.TabWidth.Should().Be(1); } [Test]