Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
e7832a6a
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e7832a6a
编写于
11月 18, 2019
作者:
A
Allison Chou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactoring
上级
02758aaf
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
45 addition
and
88 deletion
+45
-88
src/EditorFeatures/CSharpTest/CodeActions/ExtractLocalFunction/ExtractLocalFunctionTests.cs
...Actions/ExtractLocalFunction/ExtractLocalFunctionTests.cs
+5
-53
src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs
...xtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs
+3
-2
src/Features/Core/Portable/ExtractMethod/MethodExtractor.CodeGenerator.cs
...e/Portable/ExtractMethod/MethodExtractor.CodeGenerator.cs
+6
-6
src/Features/VisualBasic/Portable/ExtractMethod/VisualBasicMethodExtractor.VisualBasicCodeGenerator.vb
...od/VisualBasicMethodExtractor.VisualBasicCodeGenerator.vb
+1
-1
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs
...rp/Portable/CodeGeneration/CSharpCodeGenerationService.cs
+25
-20
src/Workspaces/Core/Portable/CodeGeneration/AbstractCodeGenerationService.cs
.../Portable/CodeGeneration/AbstractCodeGenerationService.cs
+1
-1
src/Workspaces/Core/Portable/CodeGeneration/ICodeGenerationService.cs
...es/Core/Portable/CodeGeneration/ICodeGenerationService.cs
+1
-1
src/Workspaces/Core/Portable/CodeGeneration/Symbols/CodeGenerationMethodSymbol.cs
...able/CodeGeneration/Symbols/CodeGenerationMethodSymbol.cs
+2
-1
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicCodeGenerationService.vb
...rtable/CodeGeneration/VisualBasicCodeGenerationService.vb
+1
-3
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/ExtractLocalFunction/ExtractLocalFunctionTests.cs
浏览文件 @
e7832a6a
...
...
@@ -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
()
{
...
...
src/Features/CSharp/Portable/ExtractMethod/CSharpMethodExtractor.CSharpCodeGenerator.cs
浏览文件 @
e7832a6a
...
...
@@ -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
(
...
...
src/Features/Core/Portable/ExtractMethod/MethodExtractor.CodeGenerator.cs
浏览文件 @
e7832a6a
...
...
@@ -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
{
...
...
src/Features/VisualBasic/Portable/ExtractMethod/VisualBasicMethodExtractor.VisualBasicCodeGenerator.vb
浏览文件 @
e7832a6a
...
...
@@ -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
...
...
src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs
浏览文件 @
e7832a6a
...
...
@@ -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
{
...
...
src/Workspaces/Core/Portable/CodeGeneration/AbstractCodeGenerationService.cs
浏览文件 @
e7832a6a
...
...
@@ -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
);
...
...
src/Workspaces/Core/Portable/CodeGeneration/ICodeGenerationService.cs
浏览文件 @
e7832a6a
...
...
@@ -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.
...
...
src/Workspaces/Core/Portable/CodeGeneration/Symbols/CodeGenerationMethodSymbol.cs
浏览文件 @
e7832a6a
...
...
@@ -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
),
...
...
src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicCodeGenerationService.vb
浏览文件 @
e7832a6a
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录