Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions Src/CSharpier.Cli/IgnoreFile.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -31,7 +32,15 @@ private IgnoreFile(List<IgnoreWithBasePath> ignores)

public bool IsIgnored(string filePath)
{
filePath = filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
Span<char> pathRelativeToIgnoreFile =
filePath.Length <= 256 ? stackalloc char[256] : new char[filePath.Length];

pathRelativeToIgnoreFile = pathRelativeToIgnoreFile[..filePath.Length];
filePath.CopyTo(pathRelativeToIgnoreFile);
pathRelativeToIgnoreFile.Replace(
Path.AltDirectorySeparatorChar,
Path.DirectorySeparatorChar
);

foreach (var ignore in this.Ignores)
{
Expand Down Expand Up @@ -185,10 +194,19 @@ private class IgnoreWithBasePath(string basePath)
return (false, false);
}

var pathRelativeToIgnoreFile =
path.Length > basePath.Length
? path[basePath.Length..].Replace('\\', '/')
: string.Empty;
var relativePathLength = path.Length - basePath.Length;
Span<char> pathRelativeToIgnoreFile =
relativePathLength <= 256 ? stackalloc char[256] : new char[path.Length];
if (path.Length > basePath.Length)
{
path.AsSpan()[basePath.Length..].CopyTo(pathRelativeToIgnoreFile);
pathRelativeToIgnoreFile = pathRelativeToIgnoreFile[..relativePathLength];
pathRelativeToIgnoreFile.Replace('\\', '/');
}
else
{
pathRelativeToIgnoreFile = [];
}

var isIgnored = false;
var hasMatchingRule = false;
Expand Down
136 changes: 100 additions & 36 deletions Src/CSharpier.Cli/Options/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ILogger logger
this.logger = logger;
}

public static async Task<OptionsProvider> Create(
public static async ValueTask<OptionsProvider> Create(
string directoryName,
string? configPath,
string? ignorePath,
Expand Down Expand Up @@ -109,7 +109,7 @@ await EditorConfigLocator.FindForDirectoryNameAsync(
return optionsProvider;
}

public async Task<PrinterOptions?> GetPrinterOptionsForAsync(
public async ValueTask<PrinterOptions?> GetPrinterOptionsForAsync(
string filePath,
CancellationToken cancellationToken
)
Expand Down Expand Up @@ -147,105 +147,156 @@ CancellationToken cancellationToken
return formatter != Formatter.Unknown ? new PrinterOptions(formatter) : null;
}

private Task<CSharpierConfigData?> FindCSharpierConfigAsync(string directoryName)
private ValueTask<CSharpierConfigData?> FindCSharpierConfigAsync(string directoryName)
{
return this.FindFileAsync(
directoryName,
this.csharpierConfigsByDirectory,
searchingDirectory =>
this.fileSystem.Directory.EnumerateFiles(
(searchingDirectory, cancellationToken) =>
this
.fileSystem.Directory.EnumerateFiles(
searchingDirectory,
".csharpierrc*",
SearchOption.TopDirectoryOnly
)
.Any(),
searchingDirectory =>
(searchingDirectory, cancellationToken) =>
Task.FromResult(
CSharpierConfigParser.FindForDirectoryName(
searchingDirectory,
this.fileSystem,
this.logger
)
)
),
CancellationToken.None
);
}

private async Task<EditorConfigSections?> FindEditorConfigAsync(
private ValueTask<EditorConfigSections?> FindEditorConfigAsync(
string directoryName,
CancellationToken cancellationToken
)
{
return await this.FindFileAsync(
return this.FindFileAsync(
directoryName,
this.editorConfigByDirectory,
searchingDirectory =>
(searchingDirectory, cancellationToken) =>
this.fileSystem.File.Exists(Path.Combine(searchingDirectory, ".editorconfig")),
async searchingDirectory =>
async (searchingDirectory, cancellationToken) =>
await EditorConfigLocator.FindForDirectoryNameAsync(
searchingDirectory,
this.fileSystem,
await this.FindIgnoreFileAsync(searchingDirectory, cancellationToken),
cancellationToken
)
),
cancellationToken
);
}

private async Task<IgnoreFile> FindIgnoreFileAsync(
private ValueTask<IgnoreFile> FindIgnoreFileAsync(
string directoryName,
CancellationToken cancellationToken
)
{
var ignoreFile = await this.FindFileAsync(
var ignoreFileTask = this.FindFileAsync(
directoryName,
this.ignoreFilesByDirectory,
(searchingDirectory) =>
(searchingDirectory, _) =>
this.fileSystem.File.Exists(Path.Combine(searchingDirectory, ".gitignore"))
|| this.fileSystem.File.Exists(
Path.Combine(searchingDirectory, ".csharpierignore")
),
(searchingDirectory) =>
IgnoreFile.CreateAsync(searchingDirectory, this.fileSystem, null, cancellationToken)
(searchingDirectory, cancellationToken) =>
IgnoreFile.CreateAsync(
searchingDirectory,
this.fileSystem,
null,
cancellationToken
),
cancellationToken
);

#pragma warning disable IDE0270
if (ignoreFile is null)
if (ignoreFileTask.IsCompletedSuccessfully)
{
// should never happen
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
}
#pragma warning disable IDE0270
if (ignoreFileTask.Result is null)
{
// should never happen
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
}
#pragma warning restore IDE0270

return ignoreFile;
return ignoreFileTask!;
}

return new ValueTask<IgnoreFile>(FindIgnoreFileAsyncInner(ignoreFileTask, directoryName));

static async Task<IgnoreFile> FindIgnoreFileAsyncInner(
ValueTask<IgnoreFile?> ignoreFileTask,
string directoryName
)
{
var ignoreFile = await ignoreFileTask;

#pragma warning disable IDE0270
if (ignoreFile is null)
{
// should never happen
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
}
#pragma warning restore IDE0270
return ignoreFile;
}
}

/// <summary>
/// this is a type of lazy lookup. We preload file type for the initial directory of the format command
/// When trying to format a file in a given subdirectory if we've already found the appropriate file type then return it
/// otherwise track it down (parsing if we need to) and set the references for any parent directories
/// </summary>
private async Task<T?> FindFileAsync<T>(
private ValueTask<T?> FindFileAsync<T>(
string directoryName,
ConcurrentDictionary<string, T?> dictionary,
Func<string, bool> shouldConsiderDirectory,
Func<string, Task<T?>> createFileAsync
Func<string, CancellationToken, bool> shouldConsiderDirectory,
Func<string, CancellationToken, Task<T?>> createFileAsync,
CancellationToken cancellationToken
)
{
if (dictionary.TryGetValue(directoryName, out var result))
{
return result;
return new ValueTask<T?>(result);
}

return FindFileAsyncInner(
directoryName,
dictionary,
shouldConsiderDirectory,
createFileAsync,
cancellationToken
);
}

private async ValueTask<T?> FindFileAsyncInner<T>(
string directoryName,
ConcurrentDictionary<string, T?> dictionary,
Func<string, CancellationToken, bool> shouldConsiderDirectory,
Func<string, CancellationToken, Task<T?>> createFileAsync,
CancellationToken cancellationToken
)
{
T? result = default;
var directoriesToSet = new List<string>();
var searchingDirectory = this.fileSystem.DirectoryInfo.New(directoryName);
while (
searchingDirectory is not null
&& !dictionary.TryGetValue(searchingDirectory.FullName, out result)
)
{
if (shouldConsiderDirectory(searchingDirectory.FullName))
if (shouldConsiderDirectory(searchingDirectory.FullName, cancellationToken))
{
dictionary[searchingDirectory.FullName] = result = await createFileAsync(
searchingDirectory.FullName
searchingDirectory.FullName,
cancellationToken
);
break;
}
Expand All @@ -262,17 +313,30 @@ searchingDirectory is not null
return result;
}

public async Task<bool> IsIgnoredAsync(
public ValueTask<bool> IsIgnoredAsync(
string actualFilePath,
CancellationToken cancellationToken
)
{
return (
await this.FindIgnoreFileAsync(
Path.GetDirectoryName(actualFilePath)!,
cancellationToken
)
).IsIgnored(actualFilePath);
var ignoredTask = this.FindIgnoreFileAsync(
Path.GetDirectoryName(actualFilePath)!,
cancellationToken
);

if (ignoredTask.IsCompletedSuccessfully)
{
return new ValueTask<bool>(ignoredTask.Result.IsIgnored(actualFilePath));
}

return new ValueTask<bool>(IsIgnoredAsyncInner(ignoredTask, actualFilePath));

static async Task<bool> IsIgnoredAsyncInner(
ValueTask<IgnoreFile> task,
string actualFilePath
)
{
return (await task).IsIgnored(actualFilePath);
}
}

public string Serialize()
Expand Down