diff --git a/Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs b/Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs index d1462a40a..126acc9d8 100644 --- a/Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs +++ b/Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs @@ -1,13 +1,25 @@ using System.IO.Abstractions; +using System.Text.RegularExpressions; using IniParser; +using IniParser.Model.Configuration; +using IniParser.Parser; namespace CSharpier.Cli.EditorConfig; internal static class ConfigFileParser { + // According to https://spec.editorconfig.org/#file-format + // "Comment: starts with a ; or a #." + private static readonly Regex CommentRegex = new("^[;#].*$"); + + private static readonly IniParserConfiguration Configuration = new() + { + CommentRegex = CommentRegex, + }; + public static ConfigFile Parse(string filePath, IFileSystem fileSystem) { - var parser = new FileIniDataParser(); + var parser = new FileIniDataParser(new IniDataParser(Configuration)); using var stream = fileSystem.File.OpenRead(filePath); using var streamReader = new StreamReader(stream); diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index f870f8d61..76e8abf99 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -215,6 +215,35 @@ public async Task Should_Support_EditorConfig_Basic() result.EndOfLine.Should().Be(EndOfLine.CRLF); } + [Test] + public async Task Should_Support_EditorConfig_With_Comments() + { + var context = new TestContext(); + context.WhenAFileExists( + "c:/test/.editorconfig", + @" +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 2 +max_line_length = 10 +; Windows-style line endings +end_of_line = crlf +" + ); + + var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs"); + + result.UseTabs.Should().BeFalse(); + result.TabWidth.Should().Be(2); + result.Width.Should().Be(10); + result.EndOfLine.Should().Be(EndOfLine.CRLF); + } + [TestCase("tab_width")] [TestCase("indent_size")] public async Task Should_Support_EditorConfig_Tabs(string propertyName)