-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put back refactoring RR0194 (fix #881)
- Loading branch information
1 parent
a61fb2a
commit 9ed14ad
Showing
10 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
104 changes: 104 additions & 0 deletions
104
src/Refactorings/CSharp/Refactorings/SplitLocalDeclarationAndAssignmentRefactoring.cs
This file contains 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,104 @@ | ||
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Roslynator.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static Roslynator.CSharp.CSharpFactory; | ||
|
||
namespace Roslynator.CSharp.Refactorings | ||
{ | ||
internal static class SplitLocalDeclarationAndAssignmentRefactoring | ||
{ | ||
public static async Task ComputeRefactoringAsync( | ||
RefactoringContext context, | ||
LocalDeclarationStatementSyntax localDeclaration) | ||
{ | ||
StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(localDeclaration); | ||
|
||
if (!statementsInfo.Success) | ||
return; | ||
|
||
SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(localDeclaration); | ||
|
||
if (!localInfo.Success) | ||
return; | ||
|
||
if (!context.Span.IsEmpty | ||
&& context.Span.Start == localInfo.EqualsToken.SpanStart) | ||
{ | ||
return; | ||
} | ||
|
||
ExpressionSyntax value = localInfo.Value; | ||
|
||
if (value == null) | ||
return; | ||
|
||
SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); | ||
|
||
TypeSyntax type = localInfo.Type; | ||
|
||
if (type.IsVar) | ||
{ | ||
ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(value, context.CancellationToken); | ||
|
||
if (typeSymbol?.SupportsExplicitDeclaration() != true) | ||
return; | ||
|
||
type = typeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart); | ||
} | ||
else | ||
{ | ||
ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); | ||
|
||
if (typeSymbol?.IsErrorType() != false) | ||
return; | ||
} | ||
|
||
context.RegisterRefactoring( | ||
"Split declaration and assignment", | ||
ct => RefactorAsync(context.Document, localInfo, type, statementsInfo, ct), | ||
RefactoringDescriptors.SplitLocalDeclarationAndAssignment); | ||
} | ||
|
||
private static Task<Document> RefactorAsync( | ||
Document document, | ||
in SingleLocalDeclarationStatementInfo localInfo, | ||
TypeSyntax type, | ||
in StatementListInfo statementsInfo, | ||
CancellationToken cancellationToken) | ||
{ | ||
LocalDeclarationStatementSyntax localStatement = localInfo.Statement; | ||
|
||
int index = statementsInfo.IndexOf(localStatement); | ||
|
||
VariableDeclaratorSyntax declarator = localInfo.Declarator; | ||
|
||
VariableDeclaratorSyntax newDeclarator = declarator.WithInitializer(null); | ||
|
||
VariableDeclarationSyntax newDeclaration = localInfo.Declaration.ReplaceNode(declarator, newDeclarator); | ||
|
||
if (type != null) | ||
newDeclaration = newDeclaration.WithType(type.WithTriviaFrom(newDeclaration.Type)); | ||
|
||
LocalDeclarationStatementSyntax newLocalStatement = localStatement | ||
.WithDeclaration(newDeclaration) | ||
.WithSemicolonToken(localStatement.SemicolonToken.WithNavigationAnnotation()) | ||
.WithTrailingTrivia(NewLine()) | ||
.WithFormatterAnnotation(); | ||
|
||
ExpressionStatementSyntax assignmentStatement = SimpleAssignmentStatement(IdentifierName(localInfo.Identifier), localInfo.Initializer.Value) | ||
.WithTrailingTrivia(localStatement.GetTrailingTrivia()) | ||
.WithFormatterAnnotation(); | ||
|
||
StatementListInfo newStatementsInfo = statementsInfo | ||
.Insert(index + 1, assignmentStatement) | ||
.ReplaceAt(index, newLocalStatement); | ||
|
||
return document.ReplaceStatementsAsync(statementsInfo, newStatementsInfo, cancellationToken); | ||
} | ||
} | ||
} |
This file contains 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
36 changes: 36 additions & 0 deletions
36
src/Tests/Refactorings.Tests/RR0194SplitLocalDeclarationAndAssignmentTests.cs
This file contains 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,36 @@ | ||
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Roslynator.Testing.CSharp; | ||
using Xunit; | ||
|
||
namespace Roslynator.CSharp.Refactorings.Tests | ||
{ | ||
public class RR0194SplitLocalDeclarationAndAssignmentTests : AbstractCSharpRefactoringVerifier | ||
{ | ||
public override string RefactoringId { get; } = RefactoringIdentifiers.SplitLocalDeclarationAndAssignment; | ||
|
||
[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.SplitLocalDeclarationAndAssignment)] | ||
public async Task TestRefactoring() | ||
{ | ||
await VerifyRefactoringAsync(@" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
string s [||]= new string(' ', 1); | ||
} | ||
} | ||
", @" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
string s; | ||
s = new string(' ', 1); | ||
} | ||
} | ||
", equivalenceKey: EquivalenceKey.Create(RefactoringId)); | ||
} | ||
} | ||
} |
This file contains 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