Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When matching all files don't rely on FileMatcher #864

Merged
merged 1 commit into from Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CodeFormatter.cs
Expand Up @@ -194,7 +194,7 @@ private static Workspace OpenFolderWorkspace(string workspacePath, SourceFileMat
addedFilePaths.Add(document.FilePath);

var isFileIncluded = formatOptions.WorkspaceType == WorkspaceType.Folder ||
(formatOptions.FileMatcher.Match(document.FilePath).HasMatches && File.Exists(document.FilePath));
(formatOptions.FileMatcher.HasMatches(document.FilePath) && File.Exists(document.FilePath));
if (!isFileIncluded || !document.SupportsSyntaxTree)
{
continue;
Expand Down
13 changes: 9 additions & 4 deletions src/Utilities/SourceFileMatcher.cs
Expand Up @@ -12,25 +12,30 @@ internal sealed class SourceFileMatcher
private static string[] AllFilesList => new[] { @"**/*.*" };

public static SourceFileMatcher CreateMatcher(string[] include, string[] exclude)
=> new SourceFileMatcher(include.Length > 0 ? include : AllFilesList, exclude);
=> new SourceFileMatcher(include, exclude);

private readonly Matcher _matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
private readonly bool _shouldMatchAll;

public ImmutableArray<string> Include { get; }
public ImmutableArray<string> Exclude { get; }

private SourceFileMatcher(string[] include, string[] exclude)
{
Include = include.ToImmutableArray();
_shouldMatchAll = include.Length == 0 && exclude.Length == 0;

Include = include.Length > 0
? include.ToImmutableArray()
: AllFilesList.ToImmutableArray();
Exclude = exclude.ToImmutableArray();

_matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
_matcher.AddIncludePatterns(Include);
_matcher.AddExcludePatterns(Exclude);
}

public PatternMatchingResult Match(string filePath)
=> _matcher.Match(filePath);
public bool HasMatches(string filePath)
=> _shouldMatchAll || _matcher.Match(filePath).HasMatches;

public IEnumerable<string> GetResultsInFullPath(string directoryPath)
=> _matcher.GetResultsInFullPath(directoryPath);
Expand Down