Skip to content

Commit

Permalink
Allow editor config without base directory
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Nov 8, 2023
1 parent d7a1f27 commit 3993b9f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/EditorConfig.Core/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public ConfigSection(string name, IEditorConfigFile origin, Dictionary<string, s

private static string FixGlob(string glob, string directory)
{
if (string.IsNullOrEmpty(directory)) return glob;

switch (glob.IndexOf('/'))
{
case -1: glob = "**/" + glob; break;
Expand Down
6 changes: 3 additions & 3 deletions src/EditorConfig.Core/EditorConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ private EditorConfigFile(

/// <summary> Parses EditorConfig file from the text reader. </summary>
/// <param name="reader"> Text reader. </param>
/// <param name="directory"> EditorConfig directory files to be matched to. </param>
/// <param name="fileName"> EditorConfig file name. </param>
/// <param name="directory"> EditorConfig base directory to match file sections to. Default is null. </param>
/// <param name="fileName"> EditorConfig file name. Default is '.editorconfig'. </param>
/// <returns> Parsed EditorConfig file. </returns>
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)
Expand Down
4 changes: 2 additions & 2 deletions src/EditorConfig.Core/EditorConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public FileConfiguration Parse(string fileName, IEnumerable<EditorConfigFile> 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);
Expand Down
30 changes: 26 additions & 4 deletions src/EditorConfig.Tests/InMemory/InMemoryConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 3993b9f

Please sign in to comment.