提交 e7832a6a 编写于 作者: A Allison Chou

Refactoring

上级 02758aaf
......@@ -1910,9 +1910,6 @@ static void Main(string[] args)
System.Console.WriteLine([|^1|]);
}
}",
@"
using System;" +
TestSources.Index +
@"
class Program
......@@ -1921,7 +1918,7 @@ static void Main(string[] args)
{
System.Console.WriteLine({|Rename:NewMethod|}());
static Index NewMethod()
static System.Index NewMethod()
{
return ^1;
}
......@@ -1940,9 +1937,6 @@ static void Main(string[] args)
System.Console.WriteLine([|..|]);
}
}",
@"
using System;" +
TestSources.Index +
TestSources.Range + @"
class Program
......@@ -1951,7 +1945,7 @@ static void Main(string[] args)
{
System.Console.WriteLine({|Rename:NewMethod|}());
static Range NewMethod()
static System.Range NewMethod()
{
return ..;
}
......@@ -1970,9 +1964,6 @@ static void Main(string[] args)
System.Console.WriteLine([|..1|]);
}
}",
@"
using System;" +
TestSources.Index +
TestSources.Range + @"
class Program
......@@ -1981,7 +1972,7 @@ static void Main(string[] args)
{
System.Console.WriteLine({|Rename:NewMethod|}());
static Range NewMethod()
static System.Range NewMethod()
{
return ..1;
}
......@@ -2000,9 +1991,6 @@ static void Main(string[] args)
System.Console.WriteLine([|1..|]);
}
}",
@"
using System;" +
TestSources.Index +
TestSources.Range + @"
class Program
......@@ -2011,7 +1999,7 @@ static void Main(string[] args)
{
System.Console.WriteLine({|Rename:NewMethod|}());
static Range NewMethod()
static System.Range NewMethod()
{
return 1..;
}
......@@ -2030,9 +2018,6 @@ static void Main(string[] args)
System.Console.WriteLine([|1..2|]);
}
}",
@"
using System;" +
TestSources.Index +
TestSources.Range + @"
class Program
......@@ -2041,7 +2026,7 @@ static void Main(string[] args)
{
System.Console.WriteLine({|Rename:NewMethod|}());
static Range NewMethod()
static System.Range NewMethod()
{
return 1..2;
}
......@@ -3129,39 +3114,6 @@ static void NewMethod2()
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
public async Task TestExtractNonStaticLocalMethod_WithoutDeclaration()
{
await TestInRegularAndScriptAsync(
@"class Test
{
static void Main(string[] args)
{
[|ExistingLocalFunction();|]
void ExistingLocalFunction()
{
}
}
}",
@"class Test
{
static void Main(string[] args)
{
{|Rename:NewMethod|}();
void ExistingLocalFunction()
{
}
void NewMethod()
{
ExistingLocalFunction();
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
public async Task TestExtractNonStaticLocalMethod_WithDeclaration()
{
......
......@@ -86,7 +86,7 @@ protected override SyntaxNode GetPreviousMember(SemanticDocument document)
return (node.Parent is GlobalStatementSyntax) ? node.Parent : node;
}
protected override OperationStatus<IMethodSymbol> GenerateMethodDefinition(CancellationToken cancellationToken)
protected override OperationStatus<IMethodSymbol> GenerateMethodDefinition(bool generateLocalFunction, CancellationToken cancellationToken)
{
var result = CreateMethodBody(cancellationToken);
......@@ -100,7 +100,8 @@ protected override OperationStatus<IMethodSymbol> GenerateMethodDefinition(Cance
name: _methodName.ToString(),
typeParameters: CreateMethodTypeParameters(),
parameters: CreateMethodParameters(),
statements: result.Data);
statements: result.Data,
methodKind: generateLocalFunction ? MethodKind.LocalFunction : MethodKind.Ordinary);
return result.With(
this.MethodDefinitionAnnotation.AddAnnotationToSymbol(
......
......@@ -55,7 +55,7 @@ protected CodeGenerator(InsertionPoint insertionPoint, SelectionResult selection
protected abstract SyntaxNode GetOutermostCallSiteContainerToProcess(CancellationToken cancellationToken);
protected abstract Task<SyntaxNode> GenerateBodyForCallSiteContainerAsync(CancellationToken cancellationToken);
protected abstract SyntaxNode GetPreviousMember(SemanticDocument document);
protected abstract OperationStatus<IMethodSymbol> GenerateMethodDefinition(CancellationToken cancellationToken);
protected abstract OperationStatus<IMethodSymbol> GenerateMethodDefinition(bool generateLocalFunction, CancellationToken cancellationToken);
protected abstract SyntaxToken CreateIdentifier(string name);
protected abstract SyntaxToken CreateMethodName();
......@@ -85,17 +85,17 @@ public async Task<GeneratedCode> GenerateAsync(CancellationToken cancellationTok
var previousMemberNode = GetPreviousMember(callSiteDocument);
var codeGenerationService = SemanticDocument.Document.GetLanguageService<ICodeGenerationService>();
var result = GenerateMethodDefinition(cancellationToken);
var result = GenerateMethodDefinition(ExtractLocalFunction, cancellationToken);
SyntaxNode destination, newContainer;
if (ExtractLocalFunction)
{
destination = InsertionPoint.With(callSiteDocument).GetContext();
newContainer = codeGenerationService.CreateMethodDeclaration(
var codeGenerationOptions = new CodeGenerationOptions(generateDefaultAccessibility: false, generateMethodBodies: true, parseOptions: destination?.SyntaxTree.Options);
var localMethod = codeGenerationService.CreateMethodDeclaration(
method: result.Data,
options: new CodeGenerationOptions(generateDefaultAccessibility: false, generateMethodBodies: true),
destinationNode: destination,
createLocalFunction: true);
options: codeGenerationOptions);
newContainer = codeGenerationService.AddStatements(destination, new[] { localMethod }, codeGenerationOptions, cancellationToken);
}
else
{
......
......@@ -58,7 +58,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod
Return Me.InsertionPoint.With(document).GetContext()
End Function
Protected Overrides Function GenerateMethodDefinition(cancellationToken As CancellationToken) As OperationStatus(Of IMethodSymbol)
Protected Overrides Function GenerateMethodDefinition(generateLocalFunction As Boolean, cancellationToken As CancellationToken) As OperationStatus(Of IMethodSymbol)
Dim result = CreateMethodBody(cancellationToken)
Dim statements = result.Data
......
......@@ -470,6 +470,10 @@ protected override TDeclarationNode AddMembers<TDeclarationNode>(TDeclarationNod
{
return AddStatementsToMemberDeclaration<TDeclarationNode>(destinationMember, statements, memberDeclaration);
}
else if (destinationMember is LocalFunctionStatementSyntax localFunctionDeclaration)
{
return AddStatementsToLocalFunction<TDeclarationNode>(destinationMember, statements, localFunctionDeclaration);
}
else
{
return AddStatementsWorker(destinationMember, statements, options, cancellationToken);
......@@ -533,6 +537,23 @@ protected override TDeclarationNode AddMembers<TDeclarationNode>(TDeclarationNod
return Cast<TDeclarationNode>(finalMember);
}
private static TDeclarationNode AddStatementsToLocalFunction<TDeclarationNode>(TDeclarationNode destinationMember, IEnumerable<SyntaxNode> statements, LocalFunctionStatementSyntax localFunctionDeclaration) where TDeclarationNode : SyntaxNode
{
var body = localFunctionDeclaration.Body;
if (body == null)
{
return destinationMember;
}
var statementNodes = body.Statements.ToList();
statementNodes.AddRange(StatementGenerator.GenerateStatements(statements));
var finalBody = body.WithStatements(SyntaxFactory.List<StatementSyntax>(statementNodes));
var finalMember = localFunctionDeclaration.WithBody(finalBody);
return Cast<TDeclarationNode>(finalMember);
}
public override SyntaxNode CreateEventDeclaration(
IEventSymbol @event, CodeGenerationDestination destination, CodeGenerationOptions options)
{
......@@ -547,7 +568,7 @@ public override SyntaxNode CreateFieldDeclaration(IFieldSymbol field, CodeGenera
}
public override SyntaxNode CreateMethodDeclaration(
IMethodSymbol method, CodeGenerationDestination destination, CodeGenerationOptions options, SyntaxNode destinationNode, bool createLocalFunction)
IMethodSymbol method, CodeGenerationDestination destination, CodeGenerationOptions options)
{
// Synthesized methods for properties/events are not things we actually generate
// declarations for.
......@@ -584,26 +605,10 @@ public override SyntaxNode CreateFieldDeclaration(IFieldSymbol field, CodeGenera
return ConversionGenerator.GenerateConversionDeclaration(
method, destination, Workspace, options, options.ParseOptions);
}
else if (createLocalFunction)
else if (method.IsLocalFunction())
{
if (destinationNode is PropertyDeclarationSyntax || destinationNode is IndexerDeclarationSyntax)
{
return destinationNode;
}
if (destinationNode is LocalFunctionStatementSyntax localFunction)
{
return localFunction.AddBodyStatements(MethodGenerator.GenerateLocalMethodDeclaration(
method, destination, Workspace, options, destinationNode?.SyntaxTree.Options ?? options.ParseOptions));
}
else if (destinationNode is MethodDeclarationSyntax methodDeclaration)
{
return methodDeclaration.AddBodyStatements(MethodGenerator.GenerateLocalMethodDeclaration(
method, destination, Workspace, options, destinationNode?.SyntaxTree.Options ?? options.ParseOptions));
}
else
{
throw new InvalidOperationException("SyntaxNode expected to be MethodDeclarationSyntax or LocalFunctionStatementSyntax.");
}
return MethodGenerator.GenerateLocalMethodDeclaration(
method, destination, Workspace, options, options.ParseOptions);
}
else
{
......
......@@ -92,7 +92,7 @@ public TDeclarationNode AddMembers<TDeclarationNode>(TDeclarationNode destinatio
public abstract CodeGenerationDestination GetDestination(SyntaxNode node);
public abstract SyntaxNode CreateEventDeclaration(IEventSymbol @event, CodeGenerationDestination destination, CodeGenerationOptions options);
public abstract SyntaxNode CreateFieldDeclaration(IFieldSymbol field, CodeGenerationDestination destination, CodeGenerationOptions options);
public abstract SyntaxNode CreateMethodDeclaration(IMethodSymbol method, CodeGenerationDestination destination, CodeGenerationOptions options, SyntaxNode destinationNode = null, bool createLocalFunction = false);
public abstract SyntaxNode CreateMethodDeclaration(IMethodSymbol method, CodeGenerationDestination destination, CodeGenerationOptions options);
public abstract SyntaxNode CreatePropertyDeclaration(IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options);
public abstract SyntaxNode CreateNamedTypeDeclaration(INamedTypeSymbol namedType, CodeGenerationDestination destination, CodeGenerationOptions options, CancellationToken cancellationToken);
public abstract SyntaxNode CreateNamespaceDeclaration(INamespaceSymbol @namespace, CodeGenerationDestination destination, CodeGenerationOptions options, CancellationToken cancellationToken);
......
......@@ -22,7 +22,7 @@ internal interface ICodeGenerationService : ILanguageService
/// <summary>
/// Returns a newly created method declaration node from the provided method.
/// </summary>
SyntaxNode CreateMethodDeclaration(IMethodSymbol method, CodeGenerationDestination destination = CodeGenerationDestination.Unspecified, CodeGenerationOptions options = null, SyntaxNode destinationNode = null, bool createLocalFunction = false);
SyntaxNode CreateMethodDeclaration(IMethodSymbol method, CodeGenerationDestination destination = CodeGenerationDestination.Unspecified, CodeGenerationOptions options = null);
/// <summary>
/// Returns a newly created property declaration node from the provided property.
......
......@@ -45,7 +45,8 @@ protected override CodeGenerationSymbol Clone()
var result = new CodeGenerationMethodSymbol(this.ContainingType,
this.GetAttributes(), this.DeclaredAccessibility, this.Modifiers,
this.ReturnType, this.RefKind, this.ExplicitInterfaceImplementations,
this.Name, this.TypeParameters, this.Parameters, this.GetReturnTypeAttributes());
this.Name, this.TypeParameters, this.Parameters, this.GetReturnTypeAttributes(),
this.MethodKind);
CodeGenerationMethodInfo.Attach(result,
CodeGenerationMethodInfo.GetIsNew(this),
......
......@@ -496,9 +496,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Public Overrides Function CreateMethodDeclaration(method As IMethodSymbol,
destination As CodeGenerationDestination,
options As CodeGenerationOptions,
Optional destinationNode As SyntaxNode = Nothing,
Optional createLocalFunction As Boolean = False) As SyntaxNode
options As CodeGenerationOptions) As SyntaxNode
' Synthesized methods for properties/events are not things we actually generate
' declarations for.
If method.AssociatedSymbol IsNot Nothing Then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册