提交 2d68180e 编写于 作者: P Petr Houska

Refactorings report applicableToSpan when registering (obvious cases).

上级 32f468f3
......@@ -14,7 +14,7 @@ internal class ExceptionInCodeAction : CodeRefactoringProvider
{
public override Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
context.RegisterRefactoring(new ExceptionCodeAction());
context.RegisterRefactoring(new ExceptionCodeAction(), context.Span);
return Task.CompletedTask;
}
......
......@@ -34,7 +34,7 @@ private class MyCodeRefactoringProvider : CodeRefactoringProvider
public sealed override Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var codeAction = new MyCodeAction(context.Document);
context.RegisterRefactoring(codeAction);
context.RegisterRefactoring(codeAction, context.Span);
return Task.CompletedTask;
}
......
......@@ -90,7 +90,7 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
.WithDocumentText(linkedDocument.Id, (await linkedDocument.GetTextAsync()).Replace(0, 6, "private"));
#pragma warning disable RS0005
context.RegisterRefactoring(CodeAction.Create("Description", (ct) => Task.FromResult(newSolution)));
context.RegisterRefactoring(CodeAction.Create("Description", (ct) => Task.FromResult(newSolution)), context.Span);
#pragma warning restore RS0005
}
}
......
......@@ -25,7 +25,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings
Private Class MyCodeRefactoringProvider : Inherits CodeRefactoringProvider
Public NotOverridable Overrides Function ComputeRefactoringsAsync(context As CodeRefactoringContext) As Task
Dim codeAction = New MyCodeAction(context.Document)
context.RegisterRefactoring(codeAction)
context.RegisterRefactoring(codeAction, context.Span)
Return Task.CompletedTask
End Function
......
......@@ -87,7 +87,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new MyCodeAction(
CSharpFeaturesResources.Inline_temporary_variable,
c => this.InlineTemporaryAsync(document, variableDeclarator, c)));
c => this.InlineTemporaryAsync(document, variableDeclarator, c)),
variableDeclarator.Span);
}
private async Task<IEnumerable<ReferenceLocation>> GetReferencesAsync(
......
......@@ -48,12 +48,14 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new MyCodeAction(
CSharpFeaturesResources.Simplify_lambda_expression,
c => SimplifyLambdaAsync(document, lambda, c)));
c => SimplifyLambdaAsync(document, lambda, c)),
lambda.Span);
context.RegisterRefactoring(
new MyCodeAction(
CSharpFeaturesResources.Simplify_all_occurrences,
c => SimplifyAllLambdasAsync(document, c)));
c => SimplifyAllLambdasAsync(document, c)),
lambda.Span);
}
private async Task<Document> SimplifyLambdaAsync(
......
......@@ -34,14 +34,8 @@ protected override async Task<SyntaxNode> GetSelectedNodeAsync(CodeRefactoringCo
// MemberDeclaration: member that can be declared in type (those are the ones we can pull up)
// VariableDeclaratorSyntax: for fields the MemberDeclaration can actually represent multiple declarations, e.g. `int a = 0, b = 1;`.
// ..Since the user might want to select & pull up only one of them (e.g. `int a = 0, [|b = 1|];` we also look for closest VariableDeclaratorSyntax.
var memberDecl = await context.TryGetRelevantNodeAsync<MemberDeclarationSyntax>().ConfigureAwait(false);
if (memberDecl != default)
{
return memberDecl;
}
var varDecl = await context.TryGetRelevantNodeAsync<VariableDeclaratorSyntax>().ConfigureAwait(false);
return varDecl;
return await context.TryGetRelevantNodeAsync<MemberDeclarationSyntax>().ConfigureAwait(false) as SyntaxNode ??
await context.TryGetRelevantNodeAsync<VariableDeclaratorSyntax>().ConfigureAwait(false);
}
}
}
......@@ -68,7 +68,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new MyCodeAction(
Title,
c => UpdateDocumentAsync(document, declaredType, c)));
c => UpdateDocumentAsync(document, declaredType, c)),
declaredType.Span);
}
private static async Task<SyntaxNode> GetDeclarationAsync(CodeRefactoringContext context)
......
......@@ -80,22 +80,26 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var succeeded = false;
if (helper.CanOfferUseExpressionBody(optionSet, declaration, forAnalyzer: false))
{
context.RegisterRefactoring(new MyCodeAction(
helper.UseExpressionBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, declaration, optionSet, helper,
useExpressionBody: true, cancellationToken: c)));
context.RegisterRefactoring(
new MyCodeAction(
helper.UseExpressionBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, declaration, optionSet, helper,
useExpressionBody: true, cancellationToken: c)),
declaration.Span);
succeeded = true;
}
var (canOffer, _) = helper.CanOfferUseBlockBody(optionSet, declaration, forAnalyzer: false);
if (canOffer)
{
context.RegisterRefactoring(new MyCodeAction(
helper.UseBlockBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, declaration, optionSet, helper,
useExpressionBody: false, cancellationToken: c)));
context.RegisterRefactoring(
new MyCodeAction(
helper.UseBlockBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, declaration, optionSet, helper,
useExpressionBody: false, cancellationToken: c)),
declaration.Span);
succeeded = true;
}
......
......@@ -45,7 +45,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
UseExpressionBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, lambdaNode,
useExpressionBody: true, c)));
useExpressionBody: true, c)), lambdaNode.Span);
}
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
......@@ -57,7 +57,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
UseBlockBodyTitle.ToString(),
c => UpdateDocumentAsync(
document, root, lambdaNode,
useExpressionBody: false, c)));
useExpressionBody: false, c)), lambdaNode.Span);
}
}
......
......@@ -69,7 +69,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
if (siblingBanner.Length > 0 && !siblingDocument.IsGeneratedCode(cancellationToken))
{
context.RegisterRefactoring(
new MyCodeAction(c => AddBannerAsync(document, root, siblingDocument, siblingBanner, c)));
new MyCodeAction(c => AddBannerAsync(document, root, siblingDocument, siblingBanner, c)),
new Text.TextSpan(position, length: 0));
return;
}
}
......
......@@ -46,13 +46,15 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
context.RegisterRefactoring(
new MyCodeAction(
GetTitle(),
c => AddAwaitAsync(document, awaitable, withConfigureAwait: false, c)));
c => AddAwaitAsync(document, awaitable, withConfigureAwait: false, c)),
awaitable.Span);
context.RegisterRefactoring(
new MyCodeAction(
GetTitleWithConfigureAwait(),
c => AddAwaitAsync(document, awaitable, withConfigureAwait: true, c)));
c => AddAwaitAsync(document, awaitable, withConfigureAwait: true, c)),
awaitable.Span);
}
private bool IsValidAwaitableExpression(SyntaxNode invocation, SemanticModel model, ISyntaxFactsService syntaxFacts)
......
......@@ -42,7 +42,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var addImportsCodeAction = new AddMissingImportsCodeAction(
CodeActionTitle,
cancellationToken => AddMissingImports(document, textSpan, cancellationToken));
context.RegisterRefactoring(addImportsCodeAction);
context.RegisterRefactoring(addImportsCodeAction, textSpan);
}
private async Task<Solution> AddMissingImports(Document document, TextSpan textSpan, CancellationToken cancellationToken)
......
......@@ -47,15 +47,15 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
var action = await GetCodeActionAsync(document, textSpan, cancellationToken: cancellationToken).ConfigureAwait(false);
if (action == null)
if (action == default)
{
return;
}
context.RegisterRefactoring(action.Item1);
context.RegisterRefactoring(action.action, textSpan);
}
private async Task<Tuple<CodeAction, string>> GetCodeActionAsync(
private async Task<(CodeAction action, string methodBlock)> GetCodeActionAsync(
Document document,
TextSpan textSpan,
CancellationToken cancellationToken)
......@@ -75,10 +75,10 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var codeAction = new MyCodeAction(description, c => AddRenameAnnotationAsync(result.Document, result.InvocationNameToken, c));
var methodBlock = result.MethodDeclarationNode;
return Tuple.Create<CodeAction, string>(codeAction, methodBlock.ToString());
return (codeAction, methodBlock.ToString());
}
return null;
return default;
}
private async Task<Document> AddRenameAnnotationAsync(Document document, SyntaxToken invocationNameToken, CancellationToken cancellationToken)
......
......@@ -73,7 +73,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
: string.Format(FeaturesResources.Change_namespace_to_0, state.TargetNamespace),
token => service.ChangeNamespaceAsync(document, state.Container, state.TargetNamespace, token));
context.RegisterRefactoring(solutionChangeAction);
context.RegisterRefactoring(solutionChangeAction, textSpan);
}
}
......
......@@ -61,7 +61,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
context.RegisterRefactoring(new MyCodeAction(
c => ConvertToClassAsync(document, textSpan, c)));
c => ConvertToClassAsync(document, textSpan, c)),
anonymousObject.Span);
}
private async Task<(TAnonymousObjectCreationExpressionSyntax, INamedTypeSymbol)> TryGetAnonymousObjectAsync(
......
......@@ -55,7 +55,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new ConvertAutoPropertyToFullPropertyCodeAction(
FeaturesResources.Convert_to_full_property,
c => ExpandToFullPropertyAsync(document, property, propertySymbol, root, c)));
c => ExpandToFullPropertyAsync(document, property, propertySymbol, root, c)),
property.Span);
}
internal bool IsValidAutoProperty(SyntaxNode property, IPropertySymbol propertySymbol)
......
......@@ -74,7 +74,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new ForEachToForCodeAction(
Title,
c => ConvertForeachToForAsync(document, foreachInfo, c)));
c => ConvertForeachToForAsync(document, foreachInfo, c)),
foreachStatement.Span);
}
protected SyntaxToken CreateUniqueName(
......
......@@ -144,10 +144,12 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
// Looks good. We can convert this.
context.RegisterRefactoring(new MyCodeAction(GetTitle(),
c => ConvertForToForEachAsync(
document, forStatement, iterationVariable, collectionExpression,
containingType, collectionType.Type, iterationType, c)));
context.RegisterRefactoring(
new MyCodeAction(GetTitle(),
c => ConvertForToForEachAsync(
document, forStatement, iterationVariable, collectionExpression,
containingType, collectionType.Type, iterationType, c)),
forStatement.Span);
return;
......
......@@ -93,8 +93,10 @@ public async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
return;
}
context.RegisterRefactoring(new MyCodeAction(Title, c =>
UpdateDocumentAsync(root, document, ifStatement, switchSections)));
context.RegisterRefactoring(
new MyCodeAction(Title,
c => UpdateDocumentAsync(root, document, ifStatement, switchSections)),
ifStatement.Span);
}
private IEnumerable<(IEnumerable<IPattern<TSwitchLabelSyntax>> patterns, TStatementSyntax statement)> GetSections(
......
......@@ -47,7 +47,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new MyCodeAction(
Title,
c => Task.FromResult(document.WithSyntaxRoot(documentUpdateInfo.UpdateRoot(root)))));
c => Task.FromResult(document.WithSyntaxRoot(documentUpdateInfo.UpdateRoot(root)))),
queryExpression.Span);
}
}
......
......@@ -110,7 +110,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new ForEachToLinqQueryCodeAction(
FeaturesResources.Convert_to_linq,
c => ApplyConversion(queryConverter, document, convertToQuery: true, c)));
c => ApplyConversion(queryConverter, document, convertToQuery: true, c)),
forEachStatement.Span);
// Offer refactoring to convert foreach to LINQ invocation expression. For example:
//
......@@ -131,7 +132,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
new ForEachToLinqQueryCodeAction(
FeaturesResources.Convert_to_linq_call_form,
c => ApplyConversion(linqConverter, document, convertToQuery: false, c)));
c => ApplyConversion(linqConverter, document, convertToQuery: false, c)),
forEachStatement.Span);
}
}
......
......@@ -103,14 +103,16 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
void RegisterRefactoringWithResult(string text, string title)
{
context.RegisterRefactoring(new MyCodeAction(title, c =>
{
var generator = SyntaxGenerator.GetGenerator(document);
var updatedToken = generator.NumericLiteralToken(text + suffix, (ulong)value)
.WithTriviaFrom(numericToken);
var updatedRoot = root.ReplaceToken(numericToken, updatedToken);
return Task.FromResult(document.WithSyntaxRoot(updatedRoot));
}));
context.RegisterRefactoring(
new MyCodeAction(title, c =>
{
var generator = SyntaxGenerator.GetGenerator(document);
var updatedToken = generator.NumericLiteralToken(text + suffix, (ulong)value)
.WithTriviaFrom(numericToken);
var updatedRoot = root.ReplaceToken(numericToken, updatedToken);
return Task.FromResult(document.WithSyntaxRoot(updatedRoot));
}),
numericToken.Span);
}
}
......
......@@ -121,7 +121,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(new CodeAction.CodeActionWithNestedActions(
FeaturesResources.Convert_to_struct,
scopes.ToImmutableAndFree(),
isInlinable: false));
isInlinable: false),
tupleExprOrTypeNode.Span);
}
private CodeAction CreateAction(CodeRefactoringContext context, Scope scope)
......
......@@ -128,7 +128,8 @@ private async Task HandleNonSelectionAsync(CodeRefactoringContext context)
context.RegisterRefactoring(
new GenerateConstructorWithDialogCodeAction(
this, document, textSpan, containingType, viableMembers,
pickMemberOptions.ToImmutableAndFree()));
pickMemberOptions.ToImmutableAndFree()),
textSpan);
}
public async Task<ImmutableArray<CodeAction>> GenerateConstructorFromMembersAsync(
......
......@@ -55,7 +55,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
context.RegisterRefactoring(new GenerateOverridesWithDialogCodeAction(
this, document, textSpan, containingType, overridableMembers));
this, document, textSpan, containingType, overridableMembers),
textSpan);
}
}
}
......@@ -50,7 +50,8 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
context.RegisterRefactoring(new MyCodeAction(
string.Format(FeaturesResources.Introduce_local_for_0, nodeString),
c => IntroduceLocalAsync(document, expressionStatement, c)));
c => IntroduceLocalAsync(document, expressionStatement, c)),
expressionStatement.Span);
}
protected async Task<TExpressionStatementSyntax> GetExpressionStatementAsync(CodeRefactoringContext context)
......
......@@ -52,7 +52,8 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
}
context.RegisterRefactoring(new MyCodeAction(GetTitle(),
c => InvertIfAsync(root, document, ifNode, c)));
c => InvertIfAsync(root, document, ifNode, c)),
ifNode.Span);
}
private InvertIfStyle GetInvertIfStyle(
......
......@@ -23,27 +23,28 @@ public AbstractMoveDeclarationNearReferenceCodeRefactoringProvider()
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var (document, textSpan, cancellationToken) = context;
var statement = await context.TryGetRelevantNodeAsync<TLocalDeclaration>().ConfigureAwait(false);
if (statement == null)
var declaration = await context.TryGetRelevantNodeAsync<TLocalDeclaration>().ConfigureAwait(false);
if (declaration == null)
{
return;
}
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var variables = syntaxFacts.GetVariablesOfLocalDeclarationStatement(statement);
var variables = syntaxFacts.GetVariablesOfLocalDeclarationStatement(declaration);
if (variables.Count != 1)
{
return;
}
var service = document.GetLanguageService<IMoveDeclarationNearReferenceService>();
if (!await service.CanMoveDeclarationNearReferenceAsync(document, statement, cancellationToken).ConfigureAwait(false))
if (!await service.CanMoveDeclarationNearReferenceAsync(document, declaration, cancellationToken).ConfigureAwait(false))
{
return;
}
context.RegisterRefactoring(
new MyCodeAction(c => MoveDeclarationNearReferenceAsync(document, statement, c)));
new MyCodeAction(c => MoveDeclarationNearReferenceAsync(document, declaration, c)),
declaration.Span);
}
private async Task<Document> MoveDeclarationNearReferenceAsync(
......
......@@ -5,10 +5,8 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings.PullMemberUp.Dialog;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PullMemberUp;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;
......@@ -70,7 +68,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var nestedCodeAction = new CodeActionWithNestedActions(
string.Format(FeaturesResources.Pull_0_up, selectedMember.ToNameDisplayString()),
allActions, isInlinable: true);
context.RegisterRefactoring(nestedCodeAction);
context.RegisterRefactoring(nestedCodeAction, selectedMemberNode.Span);
}
private ImmutableArray<INamedTypeSymbol> FindAllValidDestinations(
......
......@@ -153,7 +153,8 @@ private ISymbol GetEnclosingSymbol(SemanticModel semanticModel, int position, Ca
{
context.RegisterRefactoring(new MyCodeAction(
string.Format(FeaturesResources.Use_0, replacement),
c => ReplaceTextAsync(context.Document, expandedSpan, replacement, c)));
c => ReplaceTextAsync(context.Document, expandedSpan, replacement, c)),
expandedSpan);
}
private async Task<Document> ReplaceTextAsync(
......
......@@ -13,11 +13,7 @@ namespace Microsoft.CodeAnalysis.ReplaceMethodWithProperty
internal abstract class AbstractReplaceMethodWithPropertyService<TMethodDeclarationSyntax> where TMethodDeclarationSyntax : SyntaxNode
{
public async Task<SyntaxNode> GetMethodDeclarationAsync(CodeRefactoringContext context)
{
var property = await context.TryGetRelevantNodeAsync<TMethodDeclarationSyntax>().ConfigureAwait(false);
return property;
}
=> await context.TryGetRelevantNodeAsync<TMethodDeclarationSyntax>().ConfigureAwait(false);
protected static string GetWarning(GetAndSetMethods getAndSetMethods)
{
......
......@@ -64,7 +64,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(new ReplaceMethodWithPropertyCodeAction(
string.Format(FeaturesResources.Replace_0_with_property, methodName),
c => ReplaceMethodsWithProperty(document, propertyName, nameChanged, methodSymbol, setMethod: null, cancellationToken: c),
methodName));
methodName),
methodDeclaration.Span);
// If this method starts with 'Get' see if there's an associated 'Set' method we could
// replace as well.
......@@ -76,7 +77,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(new ReplaceMethodWithPropertyCodeAction(
string.Format(FeaturesResources.Replace_0_and_1_with_property, methodName, setMethod.Name),
c => ReplaceMethodsWithProperty(document, propertyName, nameChanged, methodSymbol, setMethod, cancellationToken: c),
methodName + "-get/set"));
methodName + "-get/set"),
methodDeclaration.Span);
}
}
}
......
......@@ -30,11 +30,7 @@ internal abstract class AbstractReplacePropertyWithMethodsService<TIdentifierNam
protected abstract TExpressionSyntax UnwrapCompoundAssignment(SyntaxNode compoundAssignment, TExpressionSyntax readExpression);
public async Task<SyntaxNode> GetPropertyDeclarationAsync(CodeRefactoringContext context)
{
var property = await context.TryGetRelevantNodeAsync<TPropertySyntax>().ConfigureAwait(false);
return property;
}
=> await context.TryGetRelevantNodeAsync<TPropertySyntax>().ConfigureAwait(false);
protected static SyntaxNode GetFieldReference(SyntaxGenerator generator, IFieldSymbol propertyBackingField)
{
......
......@@ -62,7 +62,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(new ReplacePropertyWithMethodsCodeAction(
string.Format(resourceString, propertyName),
c => ReplacePropertyWithMethodsAsync(document, propertySymbol, c),
propertyName));
propertyName),
propertyDeclaration.Span);
}
private async Task<Solution> ReplacePropertyWithMethodsAsync(
......
......@@ -102,12 +102,14 @@ protected abstract class Analyzer<TBaseArgumentSyntax, TSimpleArgumentSyntax, TA
context.RegisterRefactoring(
new MyCodeAction(
string.Format(FeaturesResources.Add_argument_name_0, argumentName),
c => AddNamedArgumentsAsync(root, document, argument, parameters, argumentIndex, includingTrailingArguments: false)));
c => AddNamedArgumentsAsync(root, document, argument, parameters, argumentIndex, includingTrailingArguments: false)),
argument.Span);
context.RegisterRefactoring(
new MyCodeAction(
string.Format(FeaturesResources.Add_argument_name_0_including_trailing_arguments, argumentName),
c => AddNamedArgumentsAsync(root, document, argument, parameters, argumentIndex, includingTrailingArguments: true)));
c => AddNamedArgumentsAsync(root, document, argument, parameters, argumentIndex, includingTrailingArguments: true)),
argument.Span);
}
else
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册