Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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