From 3993b9f9ffcbd0ecb7a335c859a4636892cc6478 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 7 Nov 2023 17:39:14 -0800 Subject: [PATCH] Allow editor config without base directory --- src/EditorConfig.Core/ConfigSection.cs | 2 ++ src/EditorConfig.Core/EditorConfigFile.cs | 6 ++-- src/EditorConfig.Core/EditorConfigParser.cs | 4 +-- .../InMemory/InMemoryConfigTests.cs | 30 ++++++++++++++++--- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/EditorConfig.Core/ConfigSection.cs b/src/EditorConfig.Core/ConfigSection.cs index 8d3b753..474ab3d 100644 --- a/src/EditorConfig.Core/ConfigSection.cs +++ b/src/EditorConfig.Core/ConfigSection.cs @@ -70,6 +70,8 @@ public ConfigSection(string name, IEditorConfigFile origin, Dictionary Parses EditorConfig file from the text reader. /// Text reader. - /// EditorConfig directory files to be matched to. - /// EditorConfig file name. + /// EditorConfig base directory to match file sections to. Default is null. + /// EditorConfig file name. Default is '.editorconfig'. /// Parsed EditorConfig file. - public static EditorConfigFile Parse(TextReader reader, string directory, string fileName = ".editorconfig") => + public static EditorConfigFile Parse(TextReader reader, string directory = null, string fileName = ".editorconfig") => new(fileName, directory, reader); internal static EditorConfigFile Parse(string path, string cacheKey) diff --git a/src/EditorConfig.Core/EditorConfigParser.cs b/src/EditorConfig.Core/EditorConfigParser.cs index d323328..ff137fc 100644 --- a/src/EditorConfig.Core/EditorConfigParser.cs +++ b/src/EditorConfig.Core/EditorConfigParser.cs @@ -91,13 +91,13 @@ public FileConfiguration Parse(string fileName, IEnumerable ed var sections = from configFile in editorConfigFiles from section in configFile.Sections - where IsMatch(section.Glob, fullPath, configFile.Directory) + where IsMatch(section.Glob, fullPath) select section; return new FileConfiguration(ParseVersion, file, sections.ToList()); } - private bool IsMatch(string glob, string fileName, string directory) + private bool IsMatch(string glob, string fileName) { var matcher = GlobMatcher.Create(glob, _globOptions); var isMatch = matcher.IsMatch(fileName); diff --git a/src/EditorConfig.Tests/InMemory/InMemoryConfigTests.cs b/src/EditorConfig.Tests/InMemory/InMemoryConfigTests.cs index 6716fd3..44ba4eb 100644 --- a/src/EditorConfig.Tests/InMemory/InMemoryConfigTests.cs +++ b/src/EditorConfig.Tests/InMemory/InMemoryConfigTests.cs @@ -14,17 +14,39 @@ public void InMemoryConfigIsUsable() var configContent = @""" root = true - [*] + [*.cs] end_of_line = lf """; - var configDirectory = "C://VirtualPath/"; var stringReader = new StringReader(configContent); - var editorConfigFile = EditorConfigFile.Parse(stringReader, configDirectory); + var editorConfigFile = EditorConfigFile.Parse(stringReader); var parser = new EditorConfigParser(); - var config = parser.Parse("C://VirtualPath/myfile.cs", new[] { editorConfigFile }); + var config = parser.Parse("myfile.cs", new[] { editorConfigFile }); config.EditorConfigFiles.Should().ContainSingle(f => f.IsRoot); + config.EndOfLine.Should().Be(EndOfLine.LF); + } + + [Test] + public void InMemoryConfigIsUsableWithVirtualPath() + { + var configContent = @""" + root = true + + [*.cs] + end_of_line = lf + """; + var stringReader = new StringReader(configContent); + var editorConfigFile = EditorConfigFile.Parse(stringReader, "C://VirtualPath"); + + var parser = new EditorConfigParser(); + + var config1 = parser.Parse("C://VirtualPath/myfile.cs", new[] { editorConfigFile }); + config1.EditorConfigFiles.Should().ContainSingle(f => f.IsRoot); + config1.EndOfLine.Should().Be(EndOfLine.LF); + + var config2 = parser.Parse("C://DifferentFolder/myfile.cs", new[] { editorConfigFile }); + config2.EditorConfigFiles.Should().BeEmpty(); } } }