Skip to content

Commit

Permalink
Make SymbolRenamer public (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Aug 19, 2023
1 parent 870e04d commit 81ef8a8
Show file tree
Hide file tree
Showing 17 changed files with 1,476 additions and 1,153 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Enabled by default.
- Add analyzer "Unnecessary enum flag" [RCS1258](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1258.md) ([#886](https://github.com/JosefPihrt/Roslynator/pull/886)).
- Enabled by default.
- Make `Roslynator.Rename.SymbolRenamer` public ([#1161](https://github.com/josefpihrt/roslynator/pull/1161))

### Fixed

Expand Down
5 changes: 1 addition & 4 deletions src/CommandLine/CommandResults/RenameSymbolCommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ namespace Roslynator.CommandLine;

internal class RenameSymbolCommandResult : CommandResult
{
public RenameSymbolCommandResult(CommandStatus status, ImmutableArray<SymbolRenameResult> renameResults)
public RenameSymbolCommandResult(CommandStatus status)
: base(status)
{
RenameResults = renameResults;
}

public ImmutableArray<SymbolRenameResult> RenameResults { get; }
}
55 changes: 25 additions & 30 deletions src/CommandLine/Commands/RenameSymbolCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Roslynator.CommandLine.Rename;
using Roslynator.Rename;
using static Roslynator.Logger;

Expand All @@ -18,16 +19,14 @@ public RenameSymbolCommand(
RenameSymbolCommandLineOptions options,
in ProjectFilter projectFilter,
RenameScopeFilter scopeFilter,
Visibility visibility,
RenameErrorResolution errorResolution,
CliCompilationErrorResolution errorResolution,
IEnumerable<string> ignoredCompilerDiagnostics,
int codeContext,
Func<ISymbol, bool> predicate,
Func<ISymbol, string> getNewName) : base(projectFilter)
{
Options = options;
ScopeFilter = scopeFilter;
Visibility = visibility;
ErrorResolution = errorResolution;
IgnoredCompilerDiagnostics = ignoredCompilerDiagnostics;
CodeContext = codeContext;
Expand All @@ -39,9 +38,7 @@ public RenameSymbolCommand(

public RenameScopeFilter ScopeFilter { get; }

public Visibility Visibility { get; }

public RenameErrorResolution ErrorResolution { get; }
public CliCompilationErrorResolution ErrorResolution { get; }

public IEnumerable<string> IgnoredCompilerDiagnostics { get; }

Expand All @@ -57,8 +54,7 @@ public override async Task<RenameSymbolCommandResult> ExecuteAsync(ProjectOrSolu

var projectFilter = new ProjectFilter(Options.Projects, Options.IgnoredProjects, Language);

SymbolRenamer renamer = null;
ImmutableArray<SymbolRenameResult> results = default;
SymbolRenameState renamer = null;

if (projectOrSolution.IsProject)
{
Expand All @@ -72,7 +68,7 @@ public override async Task<RenameSymbolCommandResult> ExecuteAsync(ProjectOrSolu

Stopwatch stopwatch = Stopwatch.StartNew();

results = await renamer.AnalyzeProjectAsync(project, cancellationToken);
await renamer.RenameSymbolsAsync(project, cancellationToken);

stopwatch.Stop();

Expand All @@ -84,37 +80,36 @@ public override async Task<RenameSymbolCommandResult> ExecuteAsync(ProjectOrSolu

renamer = GetSymbolRenamer(solution);

results = await renamer.AnalyzeSolutionAsync(f => projectFilter.IsMatch(f), cancellationToken);
await renamer.RenameSymbolsAsync(solution.Projects.Where(p => projectFilter.IsMatch(p)), cancellationToken);
}

return new RenameSymbolCommandResult(CommandStatus.Success, results);
return new RenameSymbolCommandResult(CommandStatus.Success);

SymbolRenamer GetSymbolRenamer(Solution solution)
SymbolRenameState GetSymbolRenamer(Solution solution)
{
VisibilityFilter visibilityFilter = Visibility switch
var options = new SymbolRenamerOptions()
{
Visibility.Public => VisibilityFilter.All,
Visibility.Internal => VisibilityFilter.Internal | VisibilityFilter.Private,
Visibility.Private => VisibilityFilter.Private,
_ => throw new InvalidOperationException()
SkipTypes = (ScopeFilter & RenameScopeFilter.Type) != 0,
SkipMembers = (ScopeFilter & RenameScopeFilter.Member) != 0,
SkipLocals = (ScopeFilter & RenameScopeFilter.Local) != 0,
IncludeGeneratedCode = Options.IncludeGeneratedCode,
DryRun = Options.DryRun,
};

var options = new SymbolRenamerOptions(
scopeFilter: ScopeFilter,
visibilityFilter: visibilityFilter,
errorResolution: ErrorResolution,
ignoredCompilerDiagnosticIds: IgnoredCompilerDiagnostics,
codeContext: CodeContext,
includeGeneratedCode: Options.IncludeGeneratedCode,
ask: Options.Ask,
dryRun: Options.DryRun,
interactive: Options.Interactive);
if (IgnoredCompilerDiagnostics is not null)
{
foreach (string id in IgnoredCompilerDiagnostics)
options.IgnoredCompilerDiagnosticIds.Add(id);
}

return new SymbolRenamer(
return new CliSymbolRenameState(
solution,
predicate: Predicate,
getNewName: GetNewName,
userDialog: new ConsoleDialog(ConsoleDialogDefinition.Default, " "),
ask: Options.Ask,
interactive: Options.Interactive,
codeContext: -1,
errorResolution: ErrorResolution,
options: options);
}
}
Expand Down
23 changes: 4 additions & 19 deletions src/CommandLine/Options/RenameSymbolCommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,18 @@ public class RenameSymbolCommandLineOptions : MSBuildCommandLineOptions
longName: OptionNames.Ask,
HelpText = "Ask whether to rename a symbol.")]
public bool Ask { get; set; }
#if DEBUG
[Option(
longName: OptionNames.CodeContext,
HelpText = "Number of lines to display before and after a line with symbol definition.",
MetaValue = "<NUM>",
Default = -1)]
public int CodeContext { get; set; }
#endif

[Option(
shortName: OptionShortNames.DryRun,
longName: "dry-run",
HelpText = "List symbols to be renamed but do not save changes to a disk.")]
public bool DryRun { get; set; }
#if DEBUG

[Option(
longName: OptionNames.IgnoredCompilerDiagnostics,
HelpText = "A list of compiler diagnostics that should be ignored.")]
HelpText = "A space separated list of compiler diagnostics that should be ignored.")]
public IEnumerable<string> IgnoredCompilerDiagnostics { get; set; }
#endif

[Option(
shortName: OptionShortNames.IncludeGeneratedCode,
longName: "include-generated-code",
Expand Down Expand Up @@ -83,12 +76,4 @@ public class RenameSymbolCommandLineOptions : MSBuildCommandLineOptions
HelpText = "Symbol groups to be included. Allowed values are type, member and local.",
MetaValue = "<SCOPE>")]
public IEnumerable<string> Scope { get; set; }
#if DEBUG
[Option(
longName: OptionNames.Visibility,
Default = nameof(Roslynator.Visibility.Public),
HelpText = "Defines a maximal visibility of a symbol to be renamed. Allowed values are public (default), internal or private.",
MetaValue = "<VISIBILITY>")]
public string Visibility { get; set; }
#endif
}
19 changes: 3 additions & 16 deletions src/CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Roslynator.CodeFixes;
using Roslynator.CommandLine.Rename;
using Roslynator.Diagnostics;
using Roslynator.Documentation;
using Roslynator.FindSymbols;
Expand Down Expand Up @@ -430,18 +431,10 @@ private static async Task<int> RenameSymbolAsync(RenameSymbolCommandLineOptions
if (!TryParsePaths(options.Paths, out ImmutableArray<string> paths))
return ExitCodes.Error;

if (!TryParseOptionValueAsEnum(options.OnError, OptionNames.OnError, out RenameErrorResolution errorResolution, defaultValue: RenameErrorResolution.None))
if (!TryParseOptionValueAsEnum(options.OnError, OptionNames.OnError, out CliCompilationErrorResolution errorResolution, defaultValue: CliCompilationErrorResolution.None))
return ExitCodes.Error;

#pragma warning disable RCS1118
var visibility = Visibility.Public;
#pragma warning restore RCS1118
var scopeFilter = RenameScopeFilter.All;
#if DEBUG
if (!TryParseOptionValueAsEnum(options.Visibility, OptionNames.Visibility, out visibility))
return ExitCodes.Error;
#endif
if (!TryParseOptionValueAsEnumFlags(options.Scope, OptionNames.Scope, out scopeFilter, defaultValue: RenameScopeFilter.All))
if (!TryParseOptionValueAsEnumFlags(options.Scope, OptionNames.Scope, out RenameScopeFilter scopeFilter, defaultValue: RenameScopeFilter.All))
return ExitCodes.Error;

if (!TryParseCodeExpression(
Expand Down Expand Up @@ -486,15 +479,9 @@ private static async Task<int> RenameSymbolAsync(RenameSymbolCommandLineOptions
options: options,
projectFilter: projectFilter,
scopeFilter: scopeFilter,
visibility: visibility,
errorResolution: errorResolution,
#if DEBUG
ignoredCompilerDiagnostics: options.IgnoredCompilerDiagnostics,
codeContext: options.CodeContext,
#else
ignoredCompilerDiagnostics: null,
codeContext: -1,
#endif
predicate: predicate,
getNewName: getNewName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Roslynator.Rename;
namespace Roslynator.CommandLine.Rename;

internal enum RenameErrorResolution
internal enum CliCompilationErrorResolution
{
None = 0,
Abort = 1,
Expand Down
Loading

0 comments on commit 81ef8a8

Please sign in to comment.