Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
05779838
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,发现更多精彩内容 >>
提交
05779838
编写于
4月 29, 2020
作者:
A
Allison Chou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Can't figure out why ValueTask won't work here
上级
529f15c4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
183 addition
and
44 deletion
+183
-44
src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
...t/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
+118
-0
src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs
...Variable/CSharpIntroduceVariableService_IntroduceLocal.cs
+65
-44
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
浏览文件 @
05779838
...
...
@@ -7544,6 +7544,124 @@ void M()
string {|Rename:v|} = (x.ToString());
};
}
}"
);
}
[
WorkItem
(
40745
,
"https://github.com/dotnet/roslyn/issues/40745"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsIntroduceVariable
)]
public
async
Task
TestMissingReturnStatementInAsyncTaskMethod
()
{
await
TestInRegularAndScriptAsync
(
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, Task> f = async x => await [|M2()|];
}
async Task M2()
{
}
}"
,
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, Task> f = async x =>
{
Task {|Rename:task|} = M2();
await task;
};
}
async Task M2()
{
}
}"
);
}
[
WorkItem
(
40745
,
"https://github.com/dotnet/roslyn/issues/40745"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsIntroduceVariable
)]
public
async
Task
TestMissingReturnStatementInAsyncValueTaskMethod
()
{
await
TestInRegularAndScriptAsync
(
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, ValueTask> f = async x => await [|M2()|];
}
async ValueTask M2()
{
}
}"
,
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, ValueTask> f = async x =>
{
ValueTask {|Rename:task|} = M2();
await task;
};
}
async ValueTask M2()
{
}
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsIntroduceVariable
)]
public
async
Task
TestReturnStatementInAsyncTaskTypeMethod
()
{
await
TestInRegularAndScriptAsync
(
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, Task<int>> f = async x => await [|M2()|];
}
async Task<int> M2()
{
return 0;
}
}"
,
@"using System;
using System.Threading.Tasks;
class Program
{
void M()
{
Func<int, Task<int>> f = async x =>
{
Task<int> {|Rename:task|} = M2();
return await task;
};
}
async Task<int> M2()
{
return 0;
}
}"
);
}
}
...
...
src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs
浏览文件 @
05779838
...
...
@@ -96,76 +96,97 @@ internal partial class CSharpIntroduceVariableService
var
rewrittenBody
=
Rewrite
(
document
,
expression
,
newLocalName
,
document
,
oldBody
,
allOccurrences
,
cancellationToken
);
var
doesDelegateMethodReturnVoid
=
document
.
SemanticModel
.
GetTypeInfo
(
oldLambda
,
cancellationToken
).
ConvertedType
is
INamedTypeSymbol
delegateType
&&
delegateType
.
DelegateInvokeMethod
!=
null
&&
delegateType
.
DelegateInvokeMethod
.
ReturnsVoid
;
var
shouldIncludeReturnStatement
=
ShouldIncludeReturnStatement
(
document
,
oldLambda
,
cancellationToken
);
var
newBody
=
GetNewBlockBodyForLambda
(
declarationStatement
,
isEntireLambdaBodySelected
,
rewrittenBody
,
doesDelegateMethodReturnVoid
);
declarationStatement
,
isEntireLambdaBodySelected
,
rewrittenBody
,
shouldIncludeReturnStatement
);
// Add an elastic newline so that the formatter will place this new lambda body across multiple lines.
newBody
=
newBody
.
WithOpenBraceToken
(
newBody
.
OpenBraceToken
.
WithAppendedTrailingTrivia
(
SyntaxFactory
.
ElasticCarriageReturnLineFeed
))
.
WithAdditionalAnnotations
(
Formatter
.
Annotation
);
var
newLambda
=
oldLambda
.
WithBody
(
newBody
);
var
newRoot
=
document
.
Root
.
ReplaceNode
(
oldLambda
,
newLambda
);
return
document
.
Document
.
WithSyntaxRoot
(
newRoot
);
}
private
static
bool
ShouldIncludeReturnStatement
(
SemanticDocument
document
,
LambdaExpressionSyntax
oldLambda
,
CancellationToken
cancellationToken
)
{
if
(
document
.
SemanticModel
.
GetTypeInfo
(
oldLambda
,
cancellationToken
).
ConvertedType
is
INamedTypeSymbol
delegateType
&&
delegateType
.
DelegateInvokeMethod
!=
null
)
{
if
(
delegateType
.
DelegateInvokeMethod
.
ReturnsVoid
)
{
return
false
;
}
if
(
oldLambda
.
AsyncKeyword
!=
default
)
{
if
(
document
.
SemanticModel
.
Compilation
.
TaskType
().
Equals
(
delegateType
.
DelegateInvokeMethod
.
ReturnType
)
||
document
.
SemanticModel
.
Compilation
.
ValueTaskOfTType
().
Equals
(
delegateType
.
DelegateInvokeMethod
.
ReturnType
))
{
return
false
;
}
}
}
return
true
;
}
private
static
BlockSyntax
GetNewBlockBodyForLambda
(
LocalDeclarationStatementSyntax
declarationStatement
,
bool
isEntireLambdaBodySelected
,
ExpressionSyntax
rewrittenBody
,
bool
doesDelegateMethodReturnVoid
)
bool
includeReturnStatement
)
{
// For lambdas with void return types, we don't need to include the rewritten body if the entire lambda body
// was originally selected for refactoring, as the rewritten body should already be encompassed within the
// declaration statement.
// Case 1a: The lambda has a void return type, and the user selects the entire lambda body.
// e.g.:
// Action<int> goo = x => [|x.ToString()|];
//
// After refactoring:
// Action<int> goo = x =>
// {
// string v = x.ToString();
// };
var
newBody
=
SyntaxFactory
.
Block
(
declarationStatement
);
if
(
doesDelegateMethodReturnVoid
&&
!
isEntireLambdaBodySelected
)
if
(
includeReturnStatement
)
{
// Case 1
b: The lambda has a void return type, and the user didn't select the entire lambda body
.
// Case 1
: The lambda has a non-void return type
.
// e.g.:
//
Task.Run(() => File.Copy("src", [|Path.Combine("dir", "file")|]))
;
//
Func<int, int> f = x => [|x + 1|]
;
//
// After refactoring:
//
Task.Run(()
=>
//
Func<int, int> f = x
=>
// {
// string destFileName = Path.Combine("dir", "file");
// File.Copy("src", destFileName);
// });
newBody
=
newBody
.
AddStatements
(
SyntaxFactory
.
ExpressionStatement
(
rewrittenBody
,
SyntaxFactory
.
Token
(
SyntaxKind
.
SemicolonToken
)));
// var v = x + 1;
// return v;
// };
return
SyntaxFactory
.
Block
(
declarationStatement
,
SyntaxFactory
.
ReturnStatement
(
rewrittenBody
));
}
else
if
(!
doesDelegateMethodReturnVoid
)
// For lambdas with void return types, we don't need to include the rewritten body if the entire lambda body
// was originally selected for refactoring, as the rewritten body should already be encompassed within the
// declaration statement.
if
(
isEntireLambdaBodySelected
)
{
// Case 2
: The lambda has a non-void return type
.
// Case 2
a: The lambda has a void return type, and the user selects the entire lambda body
.
// e.g.:
//
Func<int, int> f = x => [|x + 1
|];
//
Action<int> goo = x => [|x.ToString()
|];
//
// After refactoring:
//
Func<int, int> f
= x =>
//
Action<int> goo
= x =>
// {
// var v = x + 1;
// return v;
// string v = x.ToString();
// };
newBody
=
newBody
.
AddStatements
(
SyntaxFactory
.
ReturnStatement
(
rewrittenBody
));
return
SyntaxFactory
.
Block
(
declarationStatement
);
}
// Add an elastic newline so that the formatter will place this new lambda body across multiple lines.
newBody
=
newBody
.
WithOpenBraceToken
(
newBody
.
OpenBraceToken
.
WithAppendedTrailingTrivia
(
SyntaxFactory
.
ElasticCarriageReturnLineFeed
))
.
WithAdditionalAnnotations
(
Formatter
.
Annotation
);
return
newBody
;
// Case 2b: The lambda has a void return type, and the user didn't select the entire lambda body.
// e.g.:
// Task.Run(() => File.Copy("src", [|Path.Combine("dir", "file")|]));
//
// After refactoring:
// Task.Run(() =>
// {
// string destFileName = Path.Combine("dir", "file");
// File.Copy("src", destFileName);
// });
return
SyntaxFactory
.
Block
(
declarationStatement
,
SyntaxFactory
.
ExpressionStatement
(
rewrittenBody
,
SyntaxFactory
.
Token
(
SyntaxKind
.
SemicolonToken
)));
}
private
TypeSyntax
GetTypeSyntax
(
SemanticDocument
document
,
ExpressionSyntax
expression
,
CancellationToken
cancellationToken
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录