Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
3151d52d
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,发现更多精彩内容 >>
提交
3151d52d
编写于
1月 24, 2019
作者:
D
David Barbet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix issue with interceding directive in else as part of if/else.
上级
ccc3c0d0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
371 addition
and
2 deletion
+371
-2
src/EditorFeatures/CSharpTest/AddBraces/AddBracesTests.cs
src/EditorFeatures/CSharpTest/AddBraces/AddBracesTests.cs
+330
-0
src/Features/CSharp/Portable/AddBraces/CSharpAddBracesDiagnosticAnalyzer.cs
...p/Portable/AddBraces/CSharpAddBracesDiagnosticAnalyzer.cs
+29
-1
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
...spaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
+11
-0
src/Workspaces/Core/Portable/LanguageServices/SyntaxFactsService/AbstractSyntaxFactsService.cs
...Services/SyntaxFactsService/AbstractSyntaxFactsService.cs
+1
-1
未找到文件。
src/EditorFeatures/CSharpTest/AddBraces/AddBracesTests.cs
浏览文件 @
3151d52d
...
...
@@ -1369,6 +1369,297 @@ static void Main()
new
TestParameters
(
options
:
Option
(
CSharpCodeStyleOptions
.
PreferBraces
,
(
PreferBracesPreference
)
bracesPreference
,
NotificationOption
.
Silent
)));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
DoNotFireForIfElseWithIntercedingDirectiveInBoth
(
int
bracesPreference
)
{
await
TestMissingInRegularAndScriptAsync
(
@"
#define test
class Program
{
static void Main()
{
[|if (true)
#if test
return;
else|]
#endif
return;
}
}"
,
new
TestParameters
(
options
:
Option
(
CSharpCodeStyleOptions
.
PreferBraces
,
(
PreferBracesPreference
)
bracesPreference
,
NotificationOption
.
Silent
)));
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
OnlyFireForIfWithIntercedingDirectiveInElseAroundIf
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
#if test
[|if (true)
return;
else|]
#endif
return;
}
}"
,
@"
#define test
class Program
{
static void Main()
{
#if test
if (true)
{
return;
}
else
#endif
return;
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
OnlyFireForElseWithIntercedingDirectiveInIfAroundElse
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
[|if (true)
#if test
return;
else|]
return;
#endif
}
}"
,
@"
#define test
class Program
{
static void Main()
{
if (true)
#if test
return;
else
{
return;
}
#endif
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
OnlyFireForElseWithIntercedingDirectiveInIf
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
#if test
[|if (true)
#endif
return;
else|]
return;
}
}"
,
@"
#define test
class Program
{
static void Main()
{
#if test
if (true)
#endif
return;
else
{
return;
}
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
OnlyFireForIfWithIntercedingDirectiveInElse
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
[|if (true)
return;
else|]
#if test
return;
#endif
}
}"
,
@"
#define test
class Program
{
static void Main()
{
if (true)
{
return;
}
else
#if test
return;
#endif
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
true
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
FireForIfElseWithDirectiveAroundIf
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
#if test
[|if (true)
return;
#endif
else|]
{
return;
}
}
}"
,
@"
#define test
class Program
{
static void Main()
{
#if test
if (true)
{
return;
}
#endif
else
{
return;
}
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
true
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
FireForIfElseWithDirectiveAroundElse
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
[|if (true)
{
return;
}
#if test
else|]
return;
#endif
}
}"
,
@"
#define test
class Program
{
static void Main()
{
if (true)
{
return;
}
#if test
else
{
return;
}
#endif
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
...
...
@@ -1408,6 +1699,45 @@ static void Main()
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
Always
,
true
)]
[
WorkItem
(
32480
,
"https://github.com/dotnet/roslyn/issues/32480"
)]
public
async
Task
FireForIfWithDirectiveAfterEmbeddedStatement
(
int
bracesPreference
,
bool
expectDiagnostic
)
{
await
TestAsync
(
@"
#define test
class Program
{
static void Main()
{
[|if (true)|]
return;
#if test
#endif
}
}"
,
@"
#define test
class Program
{
static void Main()
{
if (true)
{
return;
}
#if test
#endif
}
}"
,
(
PreferBracesPreference
)
bracesPreference
,
expectDiagnostic
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsAddBraces
)]
[
InlineData
((
int
)
PreferBracesPreference
.
None
,
false
)]
[
InlineData
((
int
)
PreferBracesPreference
.
WhenMultiline
,
false
)]
...
...
src/Features/CSharp/Portable/AddBraces/CSharpAddBracesDiagnosticAnalyzer.cs
浏览文件 @
3151d52d
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using
System.Diagnostics
;
using
System.Threading
;
using
Microsoft.CodeAnalysis.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Text
;
using
FormattingRangeHelper
=
Microsoft
.
CodeAnalysis
.
CSharp
.
Utilities
.
FormattingRangeHelper
;
namespace
Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces
...
...
@@ -44,6 +46,7 @@ public void AnalyzeNode(SyntaxNodeAnalysisContext context)
var
cancellationToken
=
context
.
CancellationToken
;
var
optionSet
=
context
.
Options
.
GetDocumentOptionSetAsync
(
statement
.
SyntaxTree
,
cancellationToken
).
GetAwaiter
().
GetResult
();
if
(
optionSet
==
null
)
{
return
;
...
...
@@ -99,7 +102,7 @@ public void AnalyzeNode(SyntaxNodeAnalysisContext context)
return
;
}
if
(
statement
.
ContainsInterleavedDirective
(
cancellationToken
))
if
(
ContainsInterleavedDirective
(
statement
,
embeddedStatement
,
cancellationToken
))
{
return
;
}
...
...
@@ -114,6 +117,31 @@ public void AnalyzeNode(SyntaxNodeAnalysisContext context)
SyntaxFacts
.
GetText
(
firstToken
.
Kind
())));
}
/// <summary>
/// Check if there are interleaved directives on the statement.
/// Handles special case with if/else.
/// </summary>
/// <param name="statement"></param>
/// <param name="embeddedStatement"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private
static
bool
ContainsInterleavedDirective
(
SyntaxNode
statement
,
StatementSyntax
embeddedStatement
,
CancellationToken
cancellationToken
)
{
if
(
statement
.
Kind
()
==
SyntaxKind
.
IfStatement
)
{
var
ifStatementNode
=
(
IfStatementSyntax
)
statement
;
var
elseNode
=
ifStatementNode
.
Else
;
if
(
elseNode
!=
null
&&
!
embeddedStatement
.
IsMissing
)
{
// For IF/ELSE statements, only the IF part should be checked for interleaved directives when the diagnostic is triggered on the IF.
// A separate diagnostic will be triggered to handle the ELSE part.
var
ifStatementSpanWithoutElse
=
TextSpan
.
FromBounds
(
statement
.
Span
.
Start
,
embeddedStatement
.
Span
.
End
);
return
statement
.
ContainsInterleavedDirective
(
ifStatementSpanWithoutElse
,
cancellationToken
);
}
}
return
statement
.
ContainsInterleavedDirective
(
cancellationToken
);
}
/// <summary>
/// <para>In general, statements are considered multiline if any of the following span more than one line:</para>
/// <list type="bullet">
...
...
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
浏览文件 @
3151d52d
...
...
@@ -254,6 +254,17 @@ public static bool IsAnyLambdaOrAnonymousMethod(this SyntaxNode node)
public
static
bool
ContainsInterleavedDirective
(
this
SyntaxNode
syntaxNode
,
CancellationToken
cancellationToken
)
=>
CSharpSyntaxFactsService
.
Instance
.
ContainsInterleavedDirective
(
syntaxNode
,
cancellationToken
);
/// <summary>
/// Similar to <see cref="ContainsInterleavedDirective(SyntaxNode, CancellationToken)"/> except that the span to check
/// for interleaved directives can be specified separately to the node passed in.
/// </summary>
/// <param name="syntaxNode"></param>
/// <param name="span"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public
static
bool
ContainsInterleavedDirective
(
this
SyntaxNode
syntaxNode
,
TextSpan
span
,
CancellationToken
cancellationToken
)
=>
CSharpSyntaxFactsService
.
Instance
.
ContainsInterleavedDirective
(
span
,
syntaxNode
,
cancellationToken
);
public
static
bool
ContainsInterleavedDirective
(
this
SyntaxToken
token
,
TextSpan
textSpan
,
...
...
src/Workspaces/Core/Portable/LanguageServices/SyntaxFactsService/AbstractSyntaxFactsService.cs
浏览文件 @
3151d52d
...
...
@@ -411,7 +411,7 @@ public ImmutableArray<SyntaxTrivia> GetFileBanner(SyntaxToken firstToken)
public
bool
ContainsInterleavedDirective
(
SyntaxNode
node
,
CancellationToken
cancellationToken
)
=>
ContainsInterleavedDirective
(
node
.
Span
,
node
,
cancellationToken
);
p
rivate
bool
ContainsInterleavedDirective
(
p
ublic
bool
ContainsInterleavedDirective
(
TextSpan
span
,
SyntaxNode
node
,
CancellationToken
cancellationToken
)
{
foreach
(
var
token
in
node
.
DescendantTokens
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录