Skip to content

Commit 2da0658

Browse files
authored
Fix globbing (#1215)
1 parent e7d6105 commit 2da0658

File tree

7 files changed

+45
-10
lines changed

7 files changed

+45
-10
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Fix [RCS1164](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1164) ([#1196](https://github.com/JosefPihrt/Roslynator/pull/1196)).
2222
- Fix [RCS1241](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1241) ([#1197](https://github.com/JosefPihrt/Roslynator/pull/1197)).
2323
- Fix [RCS1250](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1250) ([#1205](https://github.com/JosefPihrt/Roslynator/pull/1205)).
24+
- [CLI] Fix globbing ([#1215](https://github.com/JosefPihrt/Roslynator/pull/1215)).
2425

2526
## [4.5.0] - 2023-08-27
2627

src/CommandLine/Commands/MSBuildWorkspaceCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ private async Task<TCommandResult> ExecuteAsync(string path, MSBuildWorkspace wo
158158
return null;
159159
}
160160

161+
if (FileSystemFilter is not null)
162+
FileSystemFilter.RootDirectoryPath = Path.GetDirectoryName(path);
163+
161164
return await ExecuteAsync(projectOrSolution, cancellationToken);
162165
}
163166

src/CommandLine/Rename/CliSymbolRenameState.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public CliSymbolRenameState(
4747

4848
public ConsoleDialog UserDialog { get; set; }
4949

50-
protected override async Task RenameSymbolsAsync(ImmutableArray<ProjectId> projects, CancellationToken cancellationToken = default)
50+
protected override async Task RenameSymbolsAsync(
51+
ImmutableArray<ProjectId> projects,
52+
bool isSolution,
53+
CancellationToken cancellationToken = default)
5154
{
5255
Stopwatch stopwatch = Stopwatch.StartNew();
5356
TimeSpan lastElapsed = TimeSpan.Zero;
@@ -65,6 +68,9 @@ protected override async Task RenameSymbolsAsync(ImmutableArray<ProjectId> proje
6568

6669
WriteLine($" Rename {GetScopePluralName(renameScopes[i])} in '{project.Name}' {$"{j + 1}/{projects.Length}"}", ConsoleColors.Cyan, Verbosity.Minimal);
6770

71+
if (!isSolution)
72+
CurrentDirectoryPath = Path.GetDirectoryName(project.FilePath);
73+
6874
await AnalyzeProjectAsync(project, renameScopes[i], cancellationToken);
6975

7076
WriteLine($" Done renaming {GetScopePluralName(renameScopes[i])} in '{project.Name}' in {stopwatch.Elapsed - lastElapsed:mm\\:ss\\.ff}", Verbosity.Normal);

src/Workspaces.Core/Extensions/Extensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ namespace Roslynator;
1818

1919
internal static class Extensions
2020
{
21-
public static bool IsMatch(this Matcher matcher, ISymbol symbol)
21+
public static bool IsMatch(this Matcher matcher, ISymbol symbol, string rootDirectoryPath)
2222
{
2323
foreach (Location location in symbol.Locations)
2424
{
2525
SyntaxTree tree = location.SourceTree;
2626

27-
if (tree is not null
28-
&& !matcher.Match(tree.FilePath).HasMatches)
27+
if (tree is not null)
2928
{
30-
return false;
29+
PatternMatchingResult result = (rootDirectoryPath is not null)
30+
? matcher.Match(tree.FilePath, rootDirectoryPath)
31+
: matcher.Match(tree.FilePath);
32+
33+
if (!result.HasMatches)
34+
return false;
3135
}
3236
}
3337

src/Workspaces.Core/FileSystemFilter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ private FileSystemFilter(Matcher matcher)
2222

2323
public Matcher Matcher { get; }
2424

25+
public string RootDirectoryPath { get; set; }
26+
2527
public static FileSystemFilter CreateOrDefault(
2628
IEnumerable<string> include,
2729
IEnumerable<string> exclude)
@@ -51,7 +53,7 @@ public static FileSystemFilter CreateOrDefault(
5153

5254
public bool IsMatch(ISymbol symbol)
5355
{
54-
bool isMatch = Matcher.IsMatch(symbol);
56+
bool isMatch = Matcher.IsMatch(symbol, RootDirectoryPath);
5557
#if DEBUG
5658
if (!isMatch
5759
&& Logger.ShouldWrite(Verbosity.Diagnostic))
@@ -65,7 +67,9 @@ public bool IsMatch(ISymbol symbol)
6567

6668
public bool IsMatch(string filePath)
6769
{
68-
PatternMatchingResult result = Matcher.Match(filePath);
70+
PatternMatchingResult result = (RootDirectoryPath is not null)
71+
? Matcher.Match(RootDirectoryPath, filePath)
72+
: Matcher.Match(filePath);
6973

7074
#if DEBUG
7175
Debug.Assert(result.Files.Count() <= 1, result.Files.Count().ToString());

src/Workspaces.Core/Rename/SymbolProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal class SymbolProvider
2222

2323
public Matcher FileSystemMatcher { get; init; }
2424

25+
public string RootDirectoryPath { get; init; }
26+
2527
public async Task<IEnumerable<ISymbol>> GetSymbolsAsync(
2628
Project project,
2729
RenameScope scope,
@@ -50,6 +52,7 @@ public async Task<IEnumerable<ISymbol>> GetSymbolsAsync(
5052
{
5153
IncludeGeneratedCode = IncludeGeneratedCode,
5254
FileSystemMatcher = FileSystemMatcher,
55+
RootDirectoryPath = RootDirectoryPath,
5356
};
5457

5558
analyzer.SymbolKinds.AddRange(symbolKinds);
@@ -90,6 +93,8 @@ private class Analyzer : DiagnosticAnalyzer
9093

9194
public Matcher FileSystemMatcher { get; init; }
9295

96+
public string RootDirectoryPath { get; init; }
97+
9398
public override void Initialize(AnalysisContext context)
9499
{
95100
context.EnableConcurrentExecution();
@@ -146,7 +151,7 @@ private void AnalyzeSymbol(SymbolAnalysisContext context)
146151

147152
private void AddSymbol(ISymbol symbol)
148153
{
149-
if (FileSystemMatcher?.IsMatch(symbol) != false)
154+
if (FileSystemMatcher?.IsMatch(symbol, RootDirectoryPath) != false)
150155
Symbols.Add(symbol);
151156
}
152157
}

src/Workspaces.Core/Rename/SymbolRenameState.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Diagnostics;
7+
using System.IO;
78
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -48,14 +49,18 @@ public SymbolRenameState(
4849

4950
private bool DryRun => Options.DryRun;
5051

52+
protected string CurrentDirectoryPath { get; set; }
53+
5154
public Task RenameSymbolsAsync(CancellationToken cancellationToken = default)
5255
{
5356
ImmutableArray<ProjectId> projectIds = CurrentSolution
5457
.GetProjectDependencyGraph()
5558
.GetTopologicallySortedProjects(cancellationToken)
5659
.ToImmutableArray();
5760

58-
return RenameSymbolsAsync(projectIds, cancellationToken);
61+
CurrentDirectoryPath = Path.GetDirectoryName(CurrentSolution.FilePath);
62+
63+
return RenameSymbolsAsync(projectIds, isSolution: true, cancellationToken);
5964
}
6065

6166
public Task RenameSymbolsAsync(
@@ -68,11 +73,12 @@ public Task RenameSymbolsAsync(
6873
.Join(projects, id => id, p => p.Id, (id, _) => id)
6974
.ToImmutableArray();
7075

71-
return RenameSymbolsAsync(projectIds, cancellationToken);
76+
return RenameSymbolsAsync(projectIds, isSolution: false, cancellationToken);
7277
}
7378

7479
protected virtual async Task RenameSymbolsAsync(
7580
ImmutableArray<ProjectId> projects,
81+
bool isSolution,
7682
CancellationToken cancellationToken = default)
7783
{
7884
foreach (RenameScope renameScope in GetRenameScopes())
@@ -83,6 +89,9 @@ protected virtual async Task RenameSymbolsAsync(
8389

8490
Project project = CurrentSolution.GetProject(projects[j]);
8591

92+
if (!isSolution)
93+
CurrentDirectoryPath = Path.GetDirectoryName(project.FilePath);
94+
8695
await AnalyzeProjectAsync(project, renameScope, cancellationToken).ConfigureAwait(false);
8796
}
8897
}
@@ -92,6 +101,8 @@ public virtual async Task RenameSymbolsAsync(
92101
Project project,
93102
CancellationToken cancellationToken = default)
94103
{
104+
CurrentDirectoryPath = Path.GetDirectoryName(project.FilePath);
105+
95106
foreach (RenameScope renameScope in GetRenameScopes())
96107
{
97108
cancellationToken.ThrowIfCancellationRequested();
@@ -135,6 +146,7 @@ protected async Task AnalyzeProjectAsync(
135146
{
136147
IncludeGeneratedCode = Options.IncludeGeneratedCode,
137148
FileSystemMatcher = Options.FileSystemMatcher,
149+
RootDirectoryPath = CurrentDirectoryPath,
138150
};
139151

140152
IEnumerable<ISymbol> symbols = await symbolProvider.GetSymbolsAsync(project, scope, cancellationToken).ConfigureAwait(false);

0 commit comments

Comments
 (0)