-
Notifications
You must be signed in to change notification settings - Fork 5k
Ordered evaluation in FileSystemGlobbing Matcher #115940
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
Merged
PranavSenthilnathan
merged 11 commits into
dotnet:main
from
PranavSenthilnathan:ordered-globbing
May 28, 2025
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68a3681
Ordered evaluation in FileSystemGlobbing Matcher
kasperk81 9028698
Merge branch 'main' into main
kasperk81 8410fbc
Merge branch 'main' into main
kasperk81 ba8ca5d
Apply suggestions from code review
kasperk81 3bef432
Merge branch 'main' into main
kasperk81 d1191af
Update InMemoryDirectoryInfo.cs
kasperk81 8a46440
Merge branch 'main' into main
kasperk81 0c80bc9
Clean up PR
PranavSenthilnathan 8087ab3
fix BOM
PranavSenthilnathan 4b37b10
fix property and naming
PranavSenthilnathan f873a75
pr feedback
PranavSenthilnathan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/IncludeOrExcludeValue.cs
This file contains hidden or 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,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.FileSystemGlobbing.Internal | ||
{ | ||
internal struct IncludeOrExcludeValue<TValue> | ||
{ | ||
internal TValue Value; | ||
internal bool IsInclude; | ||
} | ||
} |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
...oft.Extensions.FileSystemGlobbing/src/Internal/PatternContexts/CompositePatternContext.cs
This file contains hidden or 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,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions; | ||
|
||
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PatternContexts | ||
{ | ||
internal abstract class CompositePatternContext : IPatternContext | ||
{ | ||
public abstract void Declare(Action<IPathSegment, bool> onDeclare); | ||
public abstract void PopDirectory(); | ||
public abstract void PushDirectory(DirectoryInfoBase directory); | ||
|
||
protected internal abstract PatternTestResult MatchPatternContexts<TFileInfoBase>( | ||
TFileInfoBase fileInfo, | ||
Func<IPatternContext, TFileInfoBase, PatternTestResult> test); | ||
|
||
public bool Test(DirectoryInfoBase directory) => | ||
MatchPatternContexts(directory, | ||
static (context, dir) => | ||
context.Test(dir) ? PatternTestResult.Success(stem: string.Empty) : PatternTestResult.Failed).IsSuccessful; | ||
|
||
public PatternTestResult Test(FileInfoBase file) => | ||
MatchPatternContexts(file, static (context, fileInfo) => context.Test(fileInfo)); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...s.FileSystemGlobbing/src/Internal/PatternContexts/IncludesFirstCompositePatternContext.cs
This file contains hidden or 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,87 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions; | ||
|
||
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PatternContexts | ||
{ | ||
internal sealed class IncludesFirstCompositePatternContext : CompositePatternContext | ||
{ | ||
private readonly IPatternContext[] _includePatternContexts; | ||
private readonly IPatternContext[] _excludePatternContexts; | ||
|
||
internal IncludesFirstCompositePatternContext(IPatternContext[] includePatternContexts, IPatternContext[] excludePatternContexts) | ||
{ | ||
_includePatternContexts = includePatternContexts; | ||
_excludePatternContexts = excludePatternContexts; | ||
} | ||
|
||
public override void Declare(Action<IPathSegment, bool> onDeclare) | ||
{ | ||
foreach (IPatternContext include in _includePatternContexts) | ||
{ | ||
include.Declare(onDeclare); | ||
} | ||
} | ||
|
||
protected internal override PatternTestResult MatchPatternContexts<TFileInfoBase>(TFileInfoBase fileInfo, Func<IPatternContext, TFileInfoBase, PatternTestResult> test) | ||
{ | ||
PatternTestResult result = PatternTestResult.Failed; | ||
|
||
// If the given file/directory matches any including pattern, continues to next step. | ||
foreach (IPatternContext context in _includePatternContexts) | ||
{ | ||
PatternTestResult localResult = test(context, fileInfo); | ||
if (localResult.IsSuccessful) | ||
{ | ||
result = localResult; | ||
break; | ||
} | ||
} | ||
|
||
// If the given file/directory doesn't match any of the including pattern, returns false. | ||
if (!result.IsSuccessful) | ||
{ | ||
return PatternTestResult.Failed; | ||
} | ||
|
||
// If the given file/directory matches any excluding pattern, returns false. | ||
foreach (IPatternContext context in _excludePatternContexts) | ||
{ | ||
if (test(context, fileInfo).IsSuccessful) | ||
{ | ||
return PatternTestResult.Failed; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public override void PopDirectory() | ||
{ | ||
foreach (IPatternContext context in _excludePatternContexts) | ||
{ | ||
context.PopDirectory(); | ||
} | ||
|
||
foreach (IPatternContext context in _includePatternContexts) | ||
{ | ||
context.PopDirectory(); | ||
} | ||
} | ||
|
||
public override void PushDirectory(DirectoryInfoBase directory) | ||
{ | ||
foreach (IPatternContext context in _includePatternContexts) | ||
{ | ||
context.PushDirectory(directory); | ||
} | ||
|
||
foreach (IPatternContext context in _excludePatternContexts) | ||
{ | ||
context.PushDirectory(directory); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...s.FileSystemGlobbing/src/Internal/PatternContexts/PreserveOrderCompositePatternContext.cs
This file contains hidden or 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,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions; | ||
|
||
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PatternContexts | ||
{ | ||
internal sealed class PreserveOrderCompositePatternContext : CompositePatternContext | ||
{ | ||
private readonly IncludeOrExcludeValue<IPatternContext>[] _includeOrExcludePatternContexts; | ||
|
||
internal PreserveOrderCompositePatternContext(IncludeOrExcludeValue<IPatternContext>[] includeOrExcludePatternContexts) => | ||
_includeOrExcludePatternContexts = includeOrExcludePatternContexts; | ||
|
||
public override void Declare(Action<IPathSegment, bool> onDeclare) | ||
{ | ||
foreach (IncludeOrExcludeValue<IPatternContext> context in _includeOrExcludePatternContexts) | ||
{ | ||
if (context.IsInclude) | ||
{ | ||
context.Value.Declare(onDeclare); | ||
} | ||
} | ||
} | ||
|
||
protected internal override PatternTestResult MatchPatternContexts<TFileInfoBase>(TFileInfoBase fileInfo, Func<IPatternContext, TFileInfoBase, PatternTestResult> test) | ||
{ | ||
PatternTestResult result = PatternTestResult.Failed; | ||
|
||
foreach (IncludeOrExcludeValue<IPatternContext> context in _includeOrExcludePatternContexts) | ||
{ | ||
// If the file is currently a match and the pattern is exclude, then test it to determine | ||
// if we should unmatch. And if the file is currently not a match and the pattern is include, | ||
// then test it to determine if we should match. | ||
if (result.IsSuccessful != context.IsInclude) | ||
{ | ||
PatternTestResult localResult = test(context.Value, fileInfo); | ||
if (localResult.IsSuccessful) | ||
{ | ||
result = context.IsInclude ? localResult : PatternTestResult.Failed; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public override void PopDirectory() | ||
{ | ||
foreach (IncludeOrExcludeValue<IPatternContext> context in _includeOrExcludePatternContexts) | ||
{ | ||
context.Value.PopDirectory(); | ||
} | ||
} | ||
|
||
public override void PushDirectory(DirectoryInfoBase directory) | ||
{ | ||
foreach (IncludeOrExcludeValue<IPatternContext> context in _includeOrExcludePatternContexts) | ||
{ | ||
context.Value.PushDirectory(directory); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not need to be generic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type argument's naming is confusing since we don't actually care that it's a file info. This is instead using a
TState
pattern, so the implementation will just call thetest
with theirIPatternContext
and pass in this opaque state for the ride. It could also just be:which would force us to have a closure. I don't know if that matters so much here, but this method is from the existing code and I decided to just leave it as is.