-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basics of new override ability by extension, which will also allow ot…
…her file extensions to be formatted. (#1251) closes #1220 I mostly went with how prettier works for overrides. Using a file glob, a user can specify options to be used for a file. One of those options is which formatter to use. Right now this means non-standard files can be formatted with the csharp formatter. Some day the xml formatting PR will finally get some attention. --------- Co-authored-by: Lasath Fernando <devel@lasath.org>
- Loading branch information
1 parent
0df263d
commit 28c9bc4
Showing
22 changed files
with
516 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace CSharpier.Cli.EditorConfig; | ||
|
||
internal static class Globber | ||
{ | ||
private static readonly GlobMatcherOptions globOptions = | ||
new() | ||
{ | ||
MatchBase = true, | ||
Dot = true, | ||
AllowWindowsPaths = true, | ||
AllowSingleBraceSets = true, | ||
}; | ||
|
||
public static GlobMatcher Create(string files, string directory) | ||
{ | ||
var pattern = FixGlob(files, directory); | ||
return GlobMatcher.Create(pattern, globOptions); | ||
} | ||
|
||
private static string FixGlob(string glob, string directory) | ||
{ | ||
glob = glob.IndexOf('/') switch | ||
{ | ||
-1 => "**/" + glob, | ||
0 => glob[1..], | ||
_ => glob | ||
}; | ||
directory = directory.Replace(@"\", "/"); | ||
if (!directory.EndsWith("/")) | ||
{ | ||
directory += "/"; | ||
} | ||
|
||
return directory + glob; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
namespace CSharpier.Cli.Options; | ||
|
||
using System.IO.Abstractions; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Logging; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
internal static class ConfigFileParser | ||
{ | ||
private static readonly string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" }; | ||
|
||
/// <summary>Finds all configs above the given directory as well as within the subtree of this directory</summary> | ||
internal static List<CSharpierConfigData> FindForDirectoryName( | ||
string directoryName, | ||
IFileSystem fileSystem, | ||
ILogger logger, | ||
bool limitEditorConfigSearch | ||
) | ||
{ | ||
var results = new List<CSharpierConfigData>(); | ||
var directoryInfo = fileSystem.DirectoryInfo.New(directoryName); | ||
|
||
var filesByDirectory = directoryInfo | ||
.EnumerateFiles( | ||
".csharpierrc*", | ||
limitEditorConfigSearch | ||
? SearchOption.TopDirectoryOnly | ||
: SearchOption.AllDirectories | ||
) | ||
.GroupBy(o => o.DirectoryName); | ||
|
||
foreach (var group in filesByDirectory) | ||
{ | ||
var firstFile = group | ||
.Where(o => validExtensions.Contains(o.Extension, StringComparer.OrdinalIgnoreCase)) | ||
.MinBy(o => o.Extension); | ||
|
||
if (firstFile != null) | ||
{ | ||
results.Add( | ||
new CSharpierConfigData( | ||
firstFile.DirectoryName!, | ||
Create(firstFile.FullName, fileSystem, logger) | ||
) | ||
); | ||
} | ||
} | ||
|
||
// already found any in this directory above | ||
directoryInfo = directoryInfo.Parent; | ||
|
||
while (directoryInfo is not null) | ||
{ | ||
var file = directoryInfo | ||
.EnumerateFiles(".csharpierrc*", SearchOption.TopDirectoryOnly) | ||
.Where(o => validExtensions.Contains(o.Extension, StringComparer.OrdinalIgnoreCase)) | ||
.MinBy(o => o.Extension); | ||
|
||
if (file != null) | ||
{ | ||
results.Add( | ||
new CSharpierConfigData( | ||
file.DirectoryName!, | ||
Create(file.FullName, fileSystem, logger) | ||
) | ||
); | ||
} | ||
|
||
directoryInfo = directoryInfo.Parent; | ||
} | ||
|
||
return results.OrderByDescending(o => o.DirectoryName.Length).ToList(); | ||
} | ||
|
||
internal static ConfigurationFileOptions Create( | ||
string configPath, | ||
IFileSystem fileSystem, | ||
ILogger? logger = null | ||
) | ||
{ | ||
var directoryName = fileSystem.Path.GetDirectoryName(configPath)!; | ||
var content = fileSystem.File.ReadAllText(configPath); | ||
|
||
if (!string.IsNullOrWhiteSpace(content)) | ||
{ | ||
var configFile = CreateFromContent(content); | ||
configFile.Init(directoryName); | ||
return configFile; | ||
} | ||
|
||
logger?.LogWarning("The configuration file at " + configPath + " was empty."); | ||
|
||
return new(); | ||
} | ||
|
||
internal static ConfigurationFileOptions CreateFromContent(string content) | ||
{ | ||
return content.TrimStart().StartsWith("{") ? ReadJson(content) : ReadYaml(content); | ||
} | ||
|
||
private static ConfigurationFileOptions ReadJson(string contents) | ||
{ | ||
return JsonSerializer.Deserialize<ConfigurationFileOptions>( | ||
contents, | ||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true } | ||
) ?? new(); | ||
} | ||
|
||
private static ConfigurationFileOptions ReadYaml(string contents) | ||
{ | ||
var deserializer = new DeserializerBuilder() | ||
.WithNamingConvention(CamelCaseNamingConvention.Instance) | ||
.IgnoreUnmatchedProperties() | ||
.Build(); | ||
|
||
return deserializer.Deserialize<ConfigurationFileOptions>(contents); | ||
} | ||
} |
Oops, something went wrong.