Skip to content

Commit

Permalink
Merge pull request #864 from JoeRobich/fix-file-matcher
Browse files Browse the repository at this point in the history
When matching all files don't rely on FileMatcher
  • Loading branch information
JoeRobich committed Nov 16, 2020
2 parents 248b292 + 7e10bb8 commit acb1b73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit acb1b73

Please sign in to comment.