Skip to content

Commit

Permalink
Group refactorings 'Wrap in ...' (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 3, 2023
1 parent 4f816f1 commit eb5e546
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 47 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rename analyzers ([PR](https://github.com/dotnet/roslynator/pull/1314))
- "Add new line before embedded statement" -> "Put embedded statement on its own line" ([RCS0030](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0030))
- "Add new line before statement" -> "Put statement on its own line" ([RCS0033](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0033))
- Group refactoring "Wrap in ..." ([PR](https://github.com/dotnet/roslynator/pull/1317))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static void ComputeRefactoring(RefactoringContext context, StatementSynta

if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapStatementsInCondition))
{
context.RegisterRefactoring(
context.AddNestedCodeAction(
WrapInIfStatementRefactoring.Title,
ct => WrapInIfStatementRefactoring.Instance.RefactorAsync(context.Document, statement, ct),
RefactoringDescriptors.WrapStatementsInCondition);
}

if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapLinesInTryCatch))
{
context.RegisterRefactoring(
context.AddNestedCodeAction(
WrapLinesInTryCatchRefactoring.Title,
ct => WrapLinesInTryCatchRefactoring.Instance.RefactorAsync(context.Document, statement, ct),
RefactoringDescriptors.WrapLinesInTryCatch);
Expand Down
59 changes: 22 additions & 37 deletions src/Refactorings/CSharp/Refactorings/RefactoringContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public RefactoringContext(CodeRefactoringContext underlyingContext, SyntaxNode r

public SyntaxNode Root { get; }

public ImmutableArray<CodeAction>.Builder NestedCodeActions { get; private set; }

public CancellationToken CancellationToken
{
get { return UnderlyingContext.CancellationToken; }
Expand Down Expand Up @@ -77,50 +79,33 @@ public bool SupportsCSharp6
{
get
{
if (Project.Language == LanguageNames.CSharp)
{
switch (((CSharpParseOptions)Project.ParseOptions).LanguageVersion)
{
case LanguageVersion.CSharp1:
case LanguageVersion.CSharp2:
case LanguageVersion.CSharp3:
case LanguageVersion.CSharp4:
case LanguageVersion.CSharp5:
return false;
default:
return true;
}
}

return false;
return Project.Language == LanguageNames.CSharp
&& ((CSharpParseOptions)Project.ParseOptions).LanguageVersion >= LanguageVersion.CSharp6;
}
}

public bool SupportsCSharp7
public bool PrefixFieldIdentifierWithUnderscore => (_configOptions ??= Document.GetConfigOptions(Root.SyntaxTree)).GetPrefixFieldIdentifierWithUnderscore();

public void AddNestedCodeAction(
string title,
Func<CancellationToken, Task<Document>> createChangedDocument,
RefactoringDescriptor descriptor,
string additionalEquivalenceKey1 = null,
string additionalEquivalenceKey2 = null)
{
get
{
if (Project.Language == LanguageNames.CSharp)
{
switch (((CSharpParseOptions)Project.ParseOptions).LanguageVersion)
{
case LanguageVersion.CSharp1:
case LanguageVersion.CSharp2:
case LanguageVersion.CSharp3:
case LanguageVersion.CSharp4:
case LanguageVersion.CSharp5:
case LanguageVersion.CSharp6:
return false;
default:
return true;
}
}
NestedCodeActions ??= ImmutableArray.CreateBuilder<CodeAction>();

return false;
}
NestedCodeActions.Add(CodeAction.Create(
title,
createChangedDocument,
EquivalenceKey.Create(descriptor, additionalEquivalenceKey1, additionalEquivalenceKey2)));
}

public bool PrefixFieldIdentifierWithUnderscore => (_configOptions ??= Document.GetConfigOptions(Root.SyntaxTree)).GetPrefixFieldIdentifierWithUnderscore();
public void RegisterNestedCodeActions(string title)
{
if (NestedCodeActions is not null)
UnderlyingContext.RegisterRefactoring(CodeAction.Create(title, NestedCodeActions.ToImmutable(), isInlinable: false));
}

public void ThrowIfCancellationRequested()
{
Expand Down
10 changes: 6 additions & 4 deletions src/Refactorings/CSharp/Refactorings/SelectedLinesRefactoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
{
if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapLinesInRegion))
{
context.RegisterRefactoring(
"Wrap in #region",
context.AddNestedCodeAction(
"#region",
ct => WrapLinesInRegionRefactoring.Instance.RefactorAsync(document, selectedLines, ct),
RefactoringDescriptors.WrapLinesInRegion);
}

if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapLinesInPreprocessorDirective))
{
context.RegisterRefactoring(
"Wrap in #if",
context.AddNestedCodeAction(
"#if",
ct => WrapLinesInPreprocessorDirectiveRefactoring.Instance.RefactorAsync(document, selectedLines, ct),
RefactoringDescriptors.WrapLinesInPreprocessorDirective);
}

context.RegisterNestedCodeActions("Wrap in");
}

if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveEmptyLines))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public static async Task ComputeRefactoringAsync(RefactoringContext context, Sta

if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapStatementsInCondition))
{
context.RegisterRefactoring(
context.AddNestedCodeAction(
WrapInIfStatementRefactoring.Title,
ct => WrapInIfStatementRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct),
RefactoringDescriptors.WrapStatementsInCondition);
}

if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapLinesInTryCatch))
{
context.RegisterRefactoring(
context.AddNestedCodeAction(
WrapLinesInTryCatchRefactoring.Title,
ct => WrapLinesInTryCatchRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct),
RefactoringDescriptors.WrapLinesInTryCatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private WrapInIfStatementRefactoring()

public static WrapInIfStatementRefactoring Instance { get; } = new();

public const string Title = "Wrap in condition";
public const string Title = "'if' statement";

public override IfStatementSyntax CreateStatement(ImmutableArray<StatementSyntax> statements)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Roslynator.CSharp.Refactorings.WrapStatements;

internal sealed class WrapLinesInTryCatchRefactoring : WrapStatementsRefactoring<TryStatementSyntax>
{
public const string Title = "Wrap in try-catch";
public const string Title = "try-catch";

private WrapLinesInTryCatchRefactoring()
{
Expand Down

0 comments on commit eb5e546

Please sign in to comment.