提交 6c8431d9 编写于 作者: P Petr Houska

Move moveDeclNearReferenceRefactoring to helpers.

上级 a623e787
......@@ -2,8 +2,8 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.MoveDeclarationNearReference;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
using Microsoft.CodeAnalysis.MoveDeclarationNearReference;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
......@@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.MoveDeclarationNearRefe
public class MoveDeclarationNearReferenceTests : AbstractCSharpCodeActionTest
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace, TestParameters parameters)
=> new MoveDeclarationNearReferenceCodeRefactoringProvider();
=> new CSharpMoveDeclarationNearReferenceCodeRefactoringProvider();
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
public async Task TestMove1()
......
......@@ -2,14 +2,14 @@
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings
Imports Microsoft.CodeAnalysis.MoveDeclarationNearReference
Imports Microsoft.CodeAnalysis.VisualBasic.MoveDeclarationNearReference
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.MoveDeclarationNearReference
Public Class MoveDeclarationNearReferenceTests
Inherits AbstractVisualBasicCodeActionTest
Protected Overrides Function CreateCodeRefactoringProvider(workspace As Workspace, parameters As TestParameters) As CodeRefactoringProvider
Return New MoveDeclarationNearReferenceCodeRefactoringProvider()
Return New VisualBasicMoveDeclarationNearReferenceCodeRefactoringProvider()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)>
......
using System.Composition;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MoveDeclarationNearReference;
namespace Microsoft.CodeAnalysis.CSharp.MoveDeclarationNearReference
{
[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.MoveDeclarationNearReference), Shared]
[ExtensionOrder(After = PredefinedCodeRefactoringProviderNames.InlineTemporary)]
class CSharpMoveDeclarationNearReferenceCodeRefactoringProvider : AbstractMoveDeclarationNearReferenceCodeRefactoringProvider<LocalDeclarationStatementSyntax>
{
}
}
......@@ -13,12 +13,10 @@
namespace Microsoft.CodeAnalysis.MoveDeclarationNearReference
{
[ExportCodeRefactoringProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeRefactoringProviderNames.MoveDeclarationNearReference), Shared]
[ExtensionOrder(After = PredefinedCodeRefactoringProviderNames.InlineTemporary)]
internal sealed class MoveDeclarationNearReferenceCodeRefactoringProvider : CodeRefactoringProvider
internal abstract class AbstractMoveDeclarationNearReferenceCodeRefactoringProvider<TLocalDeclaration> : CodeRefactoringProvider where TLocalDeclaration : SyntaxNode
{
[ImportingConstructor]
public MoveDeclarationNearReferenceCodeRefactoringProvider()
public AbstractMoveDeclarationNearReferenceCodeRefactoringProvider()
{
}
......@@ -28,12 +26,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var textSpan = context.Span;
var cancellationToken = context.CancellationToken;
if (!textSpan.IsEmpty)
{
return;
}
var statement = await GetLocalDeclarationStatementAsync(document, textSpan, cancellationToken).ConfigureAwait(false);
var helperService = document.GetLanguageService<IRefactoringHelpersService>();
var statement = await helperService.TryGetSelectedNodeAsync<TLocalDeclaration>(context).ConfigureAwait(false);
if (statement == null)
{
return;
......@@ -46,17 +40,6 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return;
}
// Don't offer the refactoring inside the initializer for the variable.
var initializer = syntaxFacts.GetInitializerOfVariableDeclarator(variables[0]);
var applicableSpan = initializer == null
? statement.Span
: TextSpan.FromBounds(statement.SpanStart, initializer.SpanStart);
if (!applicableSpan.IntersectsWith(textSpan.Start))
{
return;
}
var service = document.GetLanguageService<IMoveDeclarationNearReferenceService>();
if (!await service.CanMoveDeclarationNearReferenceAsync(document, statement, cancellationToken).ConfigureAwait(false))
{
......@@ -64,26 +47,13 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}
context.RegisterRefactoring(
new MyCodeAction(c => MoveDeclarationNearReferenceAsync(document, textSpan, c)));
}
private async Task<SyntaxNode> GetLocalDeclarationStatementAsync(
Document document, TextSpan textSpan, CancellationToken cancellationToken)
{
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var position = textSpan.Start;
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var statement = root.FindToken(position).Parent.Ancestors().FirstOrDefault(n => syntaxFacts.IsLocalDeclarationStatement(n));
return statement;
new MyCodeAction(c => MoveDeclarationNearReferenceAsync(document, statement, c)));
}
private async Task<Document> MoveDeclarationNearReferenceAsync(
Document document, TextSpan span, CancellationToken cancellationToken)
Document document, SyntaxNode statement, CancellationToken cancellationToken)
{
var statement = await GetLocalDeclarationStatementAsync(document, span, cancellationToken).ConfigureAwait(false);
var service = document.GetLanguageService<IMoveDeclarationNearReferenceService>();
return await service.MoveDeclarationNearReferenceAsync(document, statement, cancellationToken).ConfigureAwait(false);
}
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Composition
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.MoveDeclarationNearReference
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.MoveDeclarationNearReference
<ExportCodeRefactoringProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeRefactoringProviderNames.MoveDeclarationNearReference), [Shared]>
<ExtensionOrder(After:=PredefinedCodeRefactoringProviderNames.InlineTemporary)>
Class VisualBasicMoveDeclarationNearReferenceCodeRefactoringProvider
Inherits AbstractMoveDeclarationNearReferenceCodeRefactoringProvider(Of LocalDeclarationStatementSyntax)
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册