Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
5aedc276
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,发现更多精彩内容 >>
提交
5aedc276
编写于
4月 18, 2018
作者:
N
Neme12
提交者:
Julien Couvreur
4月 17, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixing 'else' keyword completion (#25703)
Merging on behalf of @Neme12. Thanks!
上级
998b5b30
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
341 addition
and
47 deletion
+341
-47
src/EditorFeatures/CSharpTest2/Recommendations/ElseKeywordRecommenderTests.cs
...SharpTest2/Recommendations/ElseKeywordRecommenderTests.cs
+325
-19
src/Features/CSharp/Portable/Completion/KeywordRecommenders/ElseKeywordRecommender.cs
.../Completion/KeywordRecommenders/ElseKeywordRecommender.cs
+16
-28
未找到文件。
src/EditorFeatures/CSharpTest2/Recommendations/ElseKeywordRecommenderTests.cs
浏览文件 @
5aedc276
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.Test.Utilities
;
using
Microsoft.CodeAnalysis.Test.Utilities
;
using
Roslyn.Test.Utilities
;
using
Xunit
;
using
Xunit
;
namespace
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
namespace
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
...
@@ -86,50 +87,355 @@ public async Task TestAfterHashAndSpace()
...
@@ -86,50 +87,355 @@ public async Task TestAfterHashAndSpace()
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
public
async
Task
TestAfterIf
()
public
async
Task
Test
Not
AfterIf
()
{
{
await
Verify
Keyword
Async
(
AddInsideMethod
(
await
Verify
Absence
Async
(
AddInsideMethod
(
@"if (true)
@"if (true)
Console.WriteLine();
$$"
));
$$"
));
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
public
async
Task
TestAfterIfBlock
()
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfStatement
(
string
statement
)
{
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
await
VerifyKeywordAsync
(
AddInsideMethod
(
@"if (true)
$@"if (true)
{
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
{
statement
}
$$
else"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfStatement
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
{
statement
}
$$
else"
));
}
[
WorkItem
(
25336
,
"https://github.com/dotnet/roslyn/issues/25336"
)]
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfElseStatement
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
WorkItem
(
25336
,
"https://github.com/dotnet/roslyn/issues/25336"
)]
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfElseStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console.WriteLine();
else
{
statement
}
$$
else"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestNotAfterIfNestedIfElseElseStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console.WriteLine();
else
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestNotAfterIfStatementElse
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
{
statement
}
else
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestNotAfterIfElseStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
Console.WriteLine();
Console.WriteLine();
}
else
{
statement
}
$$"
));
$$"
));
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
public
async
Task
TestNotAfterElse1
()
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfElseNestedIfStatement
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
Console.WriteLine();
else
if (true)
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfElseNestedIfStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
Console.WriteLine();
else
if (true)
{
statement
}
$$
else"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestNotAfterIfElseNestedIfElseStatement
(
string
statement
)
{
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
await
VerifyAbsenceAsync
(
AddInsideMethod
(
@"if (true)
$
@"if (true)
Console.WriteLine();
Console.WriteLine();
else $$"
));
else
if (true)
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterWhileIfWhileNestedIfElseStatement
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"while (true)
if (true)
while (true)
if (true)
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterWhileIfWhileNestedIfElseStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"while (true)
if (true)
while (true)
if (true)
Console.WriteLine();
else
{
statement
}
$$
else"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestNotAfterWhileIfWhileNestedIfElseElseStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"while (true)
if (true)
while (true)
if (true)
Console.WriteLine();
else
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console"
)]
[
InlineData
(
"Console."
)]
[
InlineData
(
"Console.WriteLine("
)]
[
InlineData
(
"Console.WriteLine()"
)]
[
InlineData
(
"{"
)]
[
InlineData
(
"{ Console.WriteLine();"
)]
[
InlineData
(
"while"
)]
[
InlineData
(
"while (true)"
)]
[
InlineData
(
"while (true) {"
)]
[
InlineData
(
"while (true) { { }"
)]
[
InlineData
(
"for (int i = 0;"
)]
public
async
Task
TestNotAfterIfIncompleteStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console"
)]
[
InlineData
(
"Console."
)]
[
InlineData
(
"Console.WriteLine("
)]
[
InlineData
(
"Console.WriteLine()"
)]
[
InlineData
(
"{"
)]
[
InlineData
(
"{ Console.WriteLine();"
)]
[
InlineData
(
"while"
)]
[
InlineData
(
"while (true)"
)]
[
InlineData
(
"while (true) {"
)]
[
InlineData
(
"while (true) { { }"
)]
[
InlineData
(
"for (int i = 0;"
)]
public
async
Task
TestNotAfterIfNestedIfIncompleteStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console"
)]
[
InlineData
(
"Console."
)]
[
InlineData
(
"Console.WriteLine("
)]
[
InlineData
(
"Console.WriteLine()"
)]
[
InlineData
(
"{"
)]
[
InlineData
(
"{ Console.WriteLine();"
)]
[
InlineData
(
"while"
)]
[
InlineData
(
"while (true)"
)]
[
InlineData
(
"while (true) {"
)]
[
InlineData
(
"while (true) { { }"
)]
[
InlineData
(
"for (int i = 0;"
)]
public
async
Task
TestNotAfterIfNestedIfElseIncompleteStatement
(
string
statement
)
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console.WriteLine();
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfIncompleteStatementElseStatement
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console // Incomplete, but that's fine. This is not the if statement we care about.
else
{
statement
}
$$"
));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
InlineData
(
"Console.WriteLine();"
)]
[
InlineData
(
"{ }"
)]
[
InlineData
(
"while (true) { }"
)]
public
async
Task
TestAfterIfNestedIfIncompleteStatementElseStatement_BeforeElse
(
string
statement
)
{
await
VerifyKeywordAsync
(
AddInsideMethod
(
$@"if (true)
if (true)
Console // Incomplete, but that's fine. This is not the if statement we care about.
else
{
statement
}
$$
else"
));
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
public
async
Task
TestNot
AfterElse2
()
public
async
Task
TestNot
InsideStatement
()
{
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
await
VerifyAbsenceAsync
(
AddInsideMethod
(
@"if (true)
@"if (true)
{
Console.WriteLine()$$; // Complete statement, but we're not at the end of it.
Console.WriteLine();
"
));
}
else $$"
));
}
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
KeywordRecommending
)]
public
async
Task
TestNotAfter
MemberAccess
()
public
async
Task
TestNotAfter
SkippedToken
()
{
{
await
VerifyAbsenceAsync
(
AddInsideMethod
(
await
VerifyAbsenceAsync
(
AddInsideMethod
(
@"if (true)string.$$"
));
@"if (true)
Console.WriteLine();,
$$"
));
}
}
}
}
}
}
src/Features/CSharp/Portable/Completion/KeywordRecommenders/ElseKeywordRecommender.cs
浏览文件 @
5aedc276
...
@@ -23,35 +23,23 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context
...
@@ -23,35 +23,23 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context
var
token
=
context
.
TargetToken
;
var
token
=
context
.
TargetToken
;
var
statement
=
token
.
GetAncestor
<
StatementSyntax
>();
// We have to consider all ancestor if statements of the last token until we find a match for this 'else':
var
ifStatement
=
statement
.
GetAncestorOrThis
<
IfStatementSyntax
>();
// while (true)
// if (true)
if
(
statement
==
null
||
ifStatement
==
null
)
// while (true)
// if (true)
// Console.WriteLine();
// else
// Console.WriteLine();
// $$
foreach
(
var
ifStatement
in
token
.
GetAncestors
<
IfStatementSyntax
>())
{
{
return
false
;
// If there's a missing token at the end of the statement, it's incomplete and we do not offer 'else'.
}
// context.TargetToken does not include zero width so in that case these will never be equal.
if
(
ifStatement
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
)
==
token
)
// cases:
{
// if (goo)
return
true
;
// Console.WriteLine();
}
// |
// if (goo)
// Console.WriteLine();
// e|
if
(
token
.
IsKind
(
SyntaxKind
.
SemicolonToken
)
&&
ifStatement
.
Statement
.
GetLastToken
(
includeSkipped
:
true
)
==
token
)
{
return
true
;
}
// if (goo) {
// Console.WriteLine();
// } |
// if (goo) {
// Console.WriteLine();
// } e|
if
(
token
.
IsKind
(
SyntaxKind
.
CloseBraceToken
)
&&
ifStatement
.
Statement
is
BlockSyntax
&&
token
==
((
BlockSyntax
)
ifStatement
.
Statement
).
CloseBraceToken
)
{
return
true
;
}
}
return
false
;
return
false
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录