-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da88ce6
commit 9eae730
Showing
9 changed files
with
371 additions
and
1 deletion.
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
145 changes: 145 additions & 0 deletions
145
src/CodeFixes/CSharp/CodeFixes/ObjectCreationExpressionCodeFixProvider.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,145 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Roslynator.CodeFixes; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Roslynator.CSharp.CodeFixes | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ObjectCreationExpressionCodeFixProvider))] | ||
[Shared] | ||
public class ObjectCreationExpressionCodeFixProvider : BaseCodeFixProvider | ||
{ | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds | ||
{ | ||
get { return ImmutableArray.Create(CompilerDiagnosticIdentifiers.ThereIsNoArgumentGivenThatCorrespondsToRequiredFormalParameter); } | ||
} | ||
|
||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
Diagnostic diagnostic = context.Diagnostics[0]; | ||
|
||
if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.MoveInitializerExpressionsToConstructor)) | ||
return; | ||
|
||
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); | ||
|
||
if (!TryFindFirstAncestorOrSelf(root, context.Span, out ObjectCreationExpressionSyntax objectCreationExpression)) | ||
return; | ||
|
||
switch (diagnostic.Id) | ||
{ | ||
case CompilerDiagnosticIdentifiers.ThereIsNoArgumentGivenThatCorrespondsToRequiredFormalParameter: | ||
{ | ||
if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.MoveInitializerExpressionsToConstructor)) | ||
break; | ||
|
||
if (!objectCreationExpression.Type.Span.Contains(diagnostic.Location.SourceSpan)) | ||
return; | ||
|
||
ArgumentListSyntax argumentList = objectCreationExpression.ArgumentList; | ||
|
||
if (argumentList?.Arguments.Any() == true) | ||
return; | ||
|
||
InitializerExpressionSyntax initializer = objectCreationExpression.Initializer; | ||
|
||
if (initializer == null) | ||
return; | ||
|
||
SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); | ||
|
||
List<ExpressionSyntax> expressions = null; | ||
|
||
foreach (ExpressionSyntax expression in initializer.Expressions) | ||
{ | ||
if (expression is AssignmentExpressionSyntax assignment | ||
&& semanticModel.GetDiagnostic( | ||
CompilerDiagnosticIdentifiers.PropertyOrIndexerCannotBeUsedInThisContextBecauseSetAccessorIsAccessible, | ||
assignment.Left.Span, | ||
context.CancellationToken) != null) | ||
{ | ||
(expressions ??= new List<ExpressionSyntax>()).Add(expression); | ||
} | ||
} | ||
|
||
if (expressions == null) | ||
return; | ||
|
||
TypeSyntax type = objectCreationExpression.Type; | ||
|
||
if (argumentList == null) | ||
{ | ||
argumentList = ArgumentList().WithTrailingTrivia(type.GetTrailingTrivia()); | ||
type = type.WithoutTrailingTrivia(); | ||
} | ||
|
||
SeparatedSyntaxList<ArgumentSyntax> arguments = expressions | ||
.Select(f => Argument(((AssignmentExpressionSyntax)f).Right)) | ||
.ToSeparatedSyntaxList(); | ||
|
||
argumentList = argumentList.WithArguments(arguments); | ||
|
||
ObjectCreationExpressionSyntax newObjectCreationExpression = objectCreationExpression.Update( | ||
objectCreationExpression.NewKeyword, | ||
type, | ||
argumentList, | ||
initializer); | ||
|
||
SymbolInfo symbolInfo = semanticModel.GetSpeculativeSymbolInfo( | ||
objectCreationExpression.SpanStart, | ||
newObjectCreationExpression, | ||
SpeculativeBindingOption.BindAsExpression); | ||
|
||
if (symbolInfo.Symbol is IMethodSymbol methodSymbol | ||
&& methodSymbol.MethodKind == MethodKind.Constructor) | ||
{ | ||
CodeAction codeAction = CodeAction.Create( | ||
"Move initializer expressions to constructor", | ||
ct => | ||
{ | ||
InitializerExpressionSyntax newInitializer = initializer.RemoveNodes(expressions, SyntaxRefactorings.DefaultRemoveOptions); | ||
|
||
if (newInitializer.Expressions.Count == 0 | ||
&& newInitializer | ||
.DescendantTrivia(TextSpan.FromBounds(newInitializer.OpenBraceToken.SpanStart, newInitializer.CloseBraceToken.SpanStart)) | ||
.All(f => f.IsWhitespaceOrEndOfLineTrivia())) | ||
{ | ||
newInitializer = null; | ||
|
||
ArgumentListSyntax newArgumentList = newObjectCreationExpression | ||
.ArgumentList | ||
.TrimTrailingTrivia() | ||
.AppendToTrailingTrivia(initializer.GetTrailingTrivia()); | ||
|
||
newObjectCreationExpression = newObjectCreationExpression | ||
.WithArgumentList(newArgumentList); | ||
} | ||
|
||
newObjectCreationExpression = newObjectCreationExpression | ||
.WithInitializer(newInitializer) | ||
.WithFormatterAnnotation(); | ||
|
||
return context.Document.ReplaceNodeAsync(objectCreationExpression, newObjectCreationExpression, ct); | ||
}, | ||
GetEquivalenceKey(diagnostic)); | ||
|
||
context.RegisterCodeFix(codeAction, diagnostic); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.