Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
2e272c10
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,发现更多精彩内容 >>
提交
2e272c10
编写于
10月 06, 2015
作者:
K
Kevin Pilch-Bisson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5478 from Pilchie/Fix111079-FormattingOfEmbeddedThrow
Wrapping of embedded statements
上级
454938f2
38679fd1
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
105 addition
and
88 deletion
+105
-88
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
...spaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
+16
-8
src/Workspaces/CSharp/Portable/Formatting/Rules/WrappingFormattingRule.cs
...Sharp/Portable/Formatting/Rules/WrappingFormattingRule.cs
+40
-65
src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
+19
-1
src/Workspaces/CoreTest/FormattingTests.cs
src/Workspaces/CoreTest/FormattingTests.cs
+30
-14
未找到文件。
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
浏览文件 @
2e272c10
...
...
@@ -960,25 +960,33 @@ public static SyntaxNode GetParent(this SyntaxNode node)
public
static
bool
IsEmbeddedStatementOwner
(
this
SyntaxNode
node
)
{
return
node
is
IfStatementSyntax
||
return
node
is
DoStatementSyntax
||
node
is
ElseClauseSyntax
||
node
is
WhileStatementSyntax
||
node
is
ForStatementSyntax
||
node
is
FixedStatementSyntax
||
node
is
ForEachStatementSyntax
||
node
is
ForStatementSyntax
||
node
is
IfStatementSyntax
||
node
is
LabeledStatementSyntax
||
node
is
LockStatementSyntax
||
node
is
UsingStatementSyntax
||
node
is
Do
StatementSyntax
;
node
is
While
StatementSyntax
;
}
public
static
StatementSyntax
GetEmbeddedStatement
(
this
SyntaxNode
node
)
{
return
node
.
TypeSwitch
(
(
If
StatementSyntax
n
)
=>
n
.
Statement
,
(
Do
StatementSyntax
n
)
=>
n
.
Statement
,
(
ElseClauseSyntax
n
)
=>
n
.
Statement
,
(
WhileStatementSyntax
n
)
=>
n
.
Statement
,
(
ForStatementSyntax
n
)
=>
n
.
Statement
,
(
FixedStatementSyntax
n
)
=>
n
.
Statement
,
(
ForEachStatementSyntax
n
)
=>
n
.
Statement
,
(
ForStatementSyntax
n
)
=>
n
.
Statement
,
(
IfStatementSyntax
n
)
=>
n
.
Statement
,
(
LabeledStatementSyntax
n
)
=>
n
.
Statement
,
(
LockStatementSyntax
n
)
=>
n
.
Statement
,
(
UsingStatementSyntax
n
)
=>
n
.
Statement
,
(
Do
StatementSyntax
n
)
=>
n
.
Statement
,
(
While
StatementSyntax
n
)
=>
n
.
Statement
,
(
SyntaxNode
n
)
=>
null
);
}
...
...
src/Workspaces/CSharp/Portable/Formatting/Rules/WrappingFormattingRule.cs
浏览文件 @
2e272c10
...
...
@@ -7,6 +7,7 @@
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Formatting.Rules
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Text
;
using
Roslyn.Utilities
;
...
...
@@ -35,55 +36,59 @@ public override void AddSuppressOperations(List<SuppressOperation> list, SyntaxN
}
}
private
void
AddSpecificNodesSuppressOperations
(
List
<
SuppressOperation
>
list
,
SyntaxNode
node
)
private
ValueTuple
<
SyntaxToken
,
SyntaxToken
>
GetSpecificNodeSuppressionTokenRange
(
SyntaxNode
node
)
{
var
ifStatementNode
=
node
as
IfStatementSyntax
;
if
(
ifStatementNode
!=
null
)
var
embeddedStatement
=
node
.
GetEmbeddedStatement
()
;
if
(
embeddedStatement
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
ifStatementNode
.
IfKeyword
,
ifStatementNode
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
if
(
ifStatementNode
.
Else
!=
null
)
var
firstTokenOfEmbeddedStatement
=
embeddedStatement
.
GetFirstToken
(
includeZeroWidth
:
true
);
if
(
embeddedStatement
.
IsKind
(
SyntaxKind
.
Block
))
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
ifStatementNode
.
Else
.
ElseKeyword
,
ifStatementNode
.
Else
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
return
ValueTuple
.
Create
(
firstTokenOfEmbeddedStatement
.
GetPreviousToken
(
includeZeroWidth
:
true
),
embeddedStatement
.
GetLastToken
(
includeZeroWidth
:
true
));
}
else
{
return
ValueTuple
.
Create
(
firstTokenOfEmbeddedStatement
.
GetPreviousToken
(
includeZeroWidth
:
true
),
firstTokenOfEmbeddedStatement
);
}
return
;
}
var
whileStatementNode
=
node
as
DoStatementSyntax
;
if
(
whileStatementNode
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
whileStatementNode
.
GetFirstToken
(
includeZeroWidth
:
true
),
whileStatementNode
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
}
var
memberDeclNode
=
node
as
MemberDeclarationSyntax
;
if
(
memberDeclNode
!=
null
)
{
var
tokens
=
memberDeclNode
.
GetFirstAndLastMemberDeclarationTokensAfterAttributes
();
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
tokens
.
Item1
,
tokens
.
Item2
);
return
;
}
var
accessorDeclNode
=
node
as
AccessorDeclarationSyntax
;
if
(
accessorDeclNode
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
accessorDeclNode
.
GetFirstToken
(
includeZeroWidth
:
true
),
accessorDeclNode
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
return
memberDeclNode
.
GetFirstAndLastMemberDeclarationTokensAfterAttributes
();
}
var
switchSection
=
node
as
SwitchSectionSyntax
;
if
(
switchSection
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
switchSection
.
GetFirstToken
(
includeZeroWidth
:
true
),
switchSection
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
return
ValueTuple
.
Create
(
switchSection
.
GetFirstToken
(
includeZeroWidth
:
true
),
switchSection
.
GetLastToken
(
includeZeroWidth
:
true
));
}
var
anonymousMethod
=
node
as
AnonymousMethodExpressionSyntax
;
if
(
anonymousMethod
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
anonymousMethod
.
DelegateKeyword
,
anonymousMethod
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
return
ValueTuple
.
Create
(
anonymousMethod
.
DelegateKeyword
,
anonymousMethod
.
GetLastToken
(
includeZeroWidth
:
true
));
}
return
default
(
ValueTuple
<
SyntaxToken
,
SyntaxToken
>);
}
private
void
AddSpecificNodesSuppressOperations
(
List
<
SuppressOperation
>
list
,
SyntaxNode
node
)
{
var
tokens
=
GetSpecificNodeSuppressionTokenRange
(
node
);
if
(
tokens
!=
default
(
ValueTuple
<
SyntaxToken
,
SyntaxToken
>))
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
tokens
.
Item1
,
tokens
.
Item2
);
}
var
ifStatementNode
=
node
as
IfStatementSyntax
;
if
(
ifStatementNode
?.
Else
!=
null
)
{
AddSuppressWrappingIfOnSingleLineOperation
(
list
,
ifStatementNode
.
Else
.
ElseKeyword
,
ifStatementNode
.
Else
.
Statement
.
GetFirstToken
(
includeZeroWidth
:
true
));
}
}
...
...
@@ -112,46 +117,16 @@ private void RemoveSuppressOperationForStatementMethodDeclaration(List<SuppressO
RemoveSuppressOperation
(
list
,
firstToken
,
lastToken
);
}
var
ifStatementNode
=
node
as
IfStatementSyntax
;
if
(
ifStatementNode
!=
null
)
{
RemoveSuppressOperation
(
list
,
ifStatementNode
.
IfKeyword
,
ifStatementNode
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
if
(
ifStatementNode
.
Else
!=
null
)
{
RemoveSuppressOperation
(
list
,
ifStatementNode
.
Else
.
ElseKeyword
,
ifStatementNode
.
Else
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
}
return
;
}
var
whileStatementNode
=
node
as
DoStatementSyntax
;
if
(
whileStatementNode
!=
null
)
{
RemoveSuppressOperation
(
list
,
whileStatementNode
.
GetFirstToken
(
includeZeroWidth
:
true
),
whileStatementNode
.
Statement
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
}
var
memberDeclNode
=
node
as
MemberDeclarationSyntax
;
if
(
memberDeclNode
!=
null
)
var
tokens
=
GetSpecificNodeSuppressionTokenRange
(
node
);
if
(
tokens
!=
default
(
ValueTuple
<
SyntaxToken
,
SyntaxToken
>))
{
var
tokens
=
memberDeclNode
.
GetFirstAndLastMemberDeclarationTokensAfterAttributes
();
RemoveSuppressOperation
(
list
,
tokens
.
Item1
,
tokens
.
Item2
);
return
;
}
var
switchSection
=
node
as
SwitchSectionSyntax
;
if
(
switchSection
!=
null
)
{
RemoveSuppressOperation
(
list
,
switchSection
.
GetFirstToken
(
includeZeroWidth
:
true
),
switchSection
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
}
var
anonymousMethod
=
node
as
AnonymousMethodExpression
Syntax
;
if
(
anonymousMethod
!=
null
)
var
ifStatementNode
=
node
as
IfStatement
Syntax
;
if
(
ifStatementNode
?.
Else
!=
null
)
{
RemoveSuppressOperation
(
list
,
anonymousMethod
.
DelegateKeyword
,
anonymousMethod
.
GetLastToken
(
includeZeroWidth
:
true
));
return
;
RemoveSuppressOperation
(
list
,
ifStatementNode
.
Else
.
ElseKeyword
,
ifStatementNode
.
Else
.
Statement
.
GetFirstToken
(
includeZeroWidth
:
true
));
}
}
...
...
src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
浏览文件 @
2e272c10
...
...
@@ -6519,5 +6519,23 @@ int MethodFoo()
}
}"
,
false
,
changingOptions
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Formatting
)]
[
WorkItem
(
111079
,
"devdiv.visualstudio.com"
)]
public
void
TestThrowInIfOnSingleLine
()
{
var
code
=
@"
class C
{
void M()
{
if (true) throw new Exception(
""message"");
}
}
\ No newline at end of file
}
"
;
AssertFormat
(
code
,
code
);
}
}
}
src/Workspaces/CoreTest/FormattingTests.cs
浏览文件 @
2e272c10
// 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
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.Test.Utilities
;
using
Roslyn.Test.Utilities
;
using
Xunit
;
using
CS
=
Microsoft
.
CodeAnalysis
.
CSharp
;
...
...
@@ -10,20 +11,16 @@ namespace Microsoft.CodeAnalysis.UnitTests
{
public
partial
class
FormattingTests
:
TestBase
{
[
Fact
]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Formatting
)
]
public
void
TestCSharpFormatting
()
{
var
text
=
@"public class C{public int X;}"
;
var
expectedFormattedText
=
@"public class C { public int X; }"
;
var
tree
=
CS
.
SyntaxFactory
.
ParseSyntaxTree
(
text
);
var
formattedRoot
=
Formatter
.
Format
(
tree
.
GetRoot
(),
new
TestWorkspace
());
var
actualFormattedText
=
formattedRoot
.
ToFullString
();
Assert
.
Equal
(
expectedFormattedText
,
actualFormattedText
);
AssertFormatCSharp
(
expectedFormattedText
,
text
);
}
[
Fact
]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Formatting
)
]
public
void
TestCSharpDefaultRules
()
{
var
rules
=
Formatter
.
GetDefaultFormattingRules
(
new
TestWorkspace
(),
LanguageNames
.
CSharp
);
...
...
@@ -32,7 +29,7 @@ public void TestCSharpDefaultRules()
Assert
.
NotEmpty
(
rules
);
}
[
Fact
]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Formatting
)
]
public
void
TestVisualBasicFormatting
()
{
var
text
=
@"
...
...
@@ -46,14 +43,10 @@ End Class
End Class
"
;
var
tree
=
VB
.
SyntaxFactory
.
ParseSyntaxTree
(
text
);
var
formattedRoot
=
Formatter
.
Format
(
tree
.
GetRoot
(),
new
TestWorkspace
());
var
actualFormattedText
=
formattedRoot
.
ToFullString
();
Assert
.
Equal
(
expectedFormattedText
,
actualFormattedText
);
AssertFormatVB
(
expectedFormattedText
,
text
);
}
[
Fact
]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
Formatting
)
]
public
void
TestVisualBasicDefaultFormattingRules
()
{
var
rules
=
Formatter
.
GetDefaultFormattingRules
(
new
TestWorkspace
(),
LanguageNames
.
VisualBasic
);
...
...
@@ -61,5 +54,28 @@ public void TestVisualBasicDefaultFormattingRules()
Assert
.
NotNull
(
rules
);
Assert
.
NotEmpty
(
rules
);
}
private
void
AssertFormatCSharp
(
string
expected
,
string
input
)
{
var
tree
=
CS
.
SyntaxFactory
.
ParseSyntaxTree
(
input
);
AssertFormat
(
expected
,
tree
);
}
private
void
AssertFormatVB
(
string
expected
,
string
input
)
{
var
tree
=
VB
.
SyntaxFactory
.
ParseSyntaxTree
(
input
);
AssertFormat
(
expected
,
tree
);
}
private
void
AssertFormat
(
string
expected
,
SyntaxTree
tree
)
{
using
(
var
workspace
=
new
TestWorkspace
())
{
var
formattedRoot
=
Formatter
.
Format
(
tree
.
GetRoot
(),
workspace
);
var
actualFormattedText
=
formattedRoot
.
ToFullString
();
Assert
.
Equal
(
expected
,
actualFormattedText
);
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录