From 7e10bb8eb340e6e4e2430e00190680780e567943 Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Mon, 16 Nov 2020 13:48:26 -0800 Subject: [PATCH] When matching all files don't rely on FileMatcher --- src/CodeFormatter.cs | 2 +- src/Utilities/SourceFileMatcher.cs | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index be79fd21e2..86bace4ce8 100644 --- a/src/CodeFormatter.cs +++ b/src/CodeFormatter.cs @@ -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; diff --git a/src/Utilities/SourceFileMatcher.cs b/src/Utilities/SourceFileMatcher.cs index b9fbd81208..d0779ff018 100644 --- a/src/Utilities/SourceFileMatcher.cs +++ b/src/Utilities/SourceFileMatcher.cs @@ -12,16 +12,21 @@ 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 Include { get; } public ImmutableArray 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); @@ -29,8 +34,8 @@ private SourceFileMatcher(string[] include, string[] exclude) _matcher.AddExcludePatterns(Exclude); } - public PatternMatchingResult Match(string filePath) - => _matcher.Match(filePath); + public bool HasMatches(string filePath) + => _shouldMatchAll || _matcher.Match(filePath).HasMatches; public IEnumerable GetResultsInFullPath(string directoryPath) => _matcher.GetResultsInFullPath(directoryPath);