Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
998e0c04
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,发现更多精彩内容 >>
提交
998e0c04
编写于
6月 20, 2018
作者:
I
Ivan Basov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feedback addressed
上级
dedeb8a3
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
68 addition
and
49 deletion
+68
-49
src/Features/CSharp/Portable/ConvertLinq/ConvertForEachToLinqQuery/CSharpConvertForEachToLinqQueryProvider.cs
...achToLinqQuery/CSharpConvertForEachToLinqQueryProvider.cs
+48
-47
src/Features/CSharp/Portable/ConvertLinq/ConvertForEachToLinqQuery/DefaultConverter.cs
...ConvertLinq/ConvertForEachToLinqQuery/DefaultConverter.cs
+2
-2
src/Features/Core/Portable/ConvertLinq/ConvertForEachToLinqQuery/AbstractConvertForEachToLinqQueryProvider.cs
...hToLinqQuery/AbstractConvertForEachToLinqQueryProvider.cs
+5
-0
src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf
src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.de.xlf
src/Features/Core/Portable/xlf/FeaturesResources.de.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.es.xlf
src/Features/Core/Portable/xlf/FeaturesResources.es.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf
src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.it.xlf
src/Features/Core/Portable/xlf/FeaturesResources.it.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf
src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf
src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf
src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf
src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf
src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.tr.xlf
src/Features/Core/Portable/xlf/FeaturesResources.tr.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hans.xlf
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hans.xlf
+1
-0
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hant.xlf
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hant.xlf
+1
-0
未找到文件。
src/Features/CSharp/Portable/ConvertLinq/ConvertForEachToLinqQuery/CSharpConvertForEachToLinqQueryProvider.cs
浏览文件 @
998e0c04
...
...
@@ -10,6 +10,7 @@
using
Microsoft.CodeAnalysis.ConvertLinq.ConvertForEachToLinqQuery
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Roslyn.Utilities
;
using
SyntaxNodeOrTokenExtensions
=
Microsoft
.
CodeAnalysis
.
Shared
.
Extensions
.
SyntaxNodeOrTokenExtensions
;
...
...
@@ -28,12 +29,12 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
ForEachStatementSyntax
forEachStatement
,
SemanticModel
semanticModel
)
{
var
identifiersBuilder
=
ImmutableArray
.
CreateBuilder
<
SyntaxToken
>
();
var
identifiersBuilder
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
identifiersBuilder
.
Add
(
forEachStatement
.
Identifier
);
var
convertingNodesBuilder
=
ImmutableArray
.
CreateBuilder
<
ExtendedSyntaxNode
>
();
var
convertingNodesBuilder
=
ArrayBuilder
<
ExtendedSyntaxNode
>.
GetInstance
();
IEnumerable
<
StatementSyntax
>
statementsCannotBeConverted
=
null
;
var
trailingTokensBuilder
=
ImmutableArray
.
CreateBuilder
<
SyntaxToken
>
();
var
currentLeadingTokens
=
new
List
<
SyntaxToken
>
();
var
trailingTokensBuilder
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
var
currentLeadingTokens
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
var
current
=
forEachStatement
.
Statement
;
// Traverse descentants of the forEachStatement.
...
...
@@ -51,30 +52,23 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
currentLeadingTokens
.
Add
(
block
.
OpenBraceToken
);
trailingTokensBuilder
.
Add
(
block
.
CloseBraceToken
);
var
array
=
block
.
Statements
.
ToArray
();
if
(
array
.
Any
()
)
if
(
array
.
Length
>
0
)
{
// All except the last one can be local declaration statements like
// {
// var a = 0;
// var b = 0;
// if (x != y) <- this is the last one in the block.
// We can support it to be a co
pm
lex foreach or if or whatever. So, set the current to it.
// We can support it to be a co
mp
lex foreach or if or whatever. So, set the current to it.
// ...
// }
for
(
var
i
=
0
;
i
<
array
.
Length
-
1
;
i
++)
{
var
statement
=
array
[
i
];
if
(
statement
is
LocalDeclarationStatementSyntax
localDeclarationStatement
&&
// Do not support declarations without initialization.
// int a = 0, b, c = 0;
localDeclarationStatement
.
Declaration
.
Variables
.
All
(
variable
=>
variable
.
Initializer
!=
null
))
{
// Prepare variable declarations to be converted into separate let clauses.
ProcessLocalDeclarationStatement
(
localDeclarationStatement
);
}
else
{
// If this one local declaration or has an empty initializer, stop processing.
if
(!(
statement
is
LocalDeclarationStatementSyntax
localDeclarationStatement
&&
TryProcessLocalDeclarationStatement
(
localDeclarationStatement
)))
{
// If this one is a local function declaration or has an empty initializer, stop processing.
statementsCannotBeConverted
=
array
.
Skip
(
i
).
ToArray
();
break
;
}
...
...
@@ -95,8 +89,8 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
// foreach can always be converted to a from clause.
var
currentForEachStatement
=
(
ForEachStatementSyntax
)
current
;
identifiersBuilder
.
Add
(
currentForEachStatement
.
Identifier
);
convertingNodesBuilder
.
Add
(
new
ExtendedSyntaxNode
(
currentForEachStatement
,
currentLeadingTokens
,
Enumerable
.
Empty
<
SyntaxToken
>()));
currentLeadingTokens
=
new
List
<
SyntaxToken
>
();
convertingNodesBuilder
.
Add
(
new
ExtendedSyntaxNode
(
currentForEachStatement
,
currentLeadingTokens
.
ToImmutableAndFree
()
,
Enumerable
.
Empty
<
SyntaxToken
>()));
currentLeadingTokens
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
// Proceed the loop with the nested statement.
current
=
currentForEachStatement
.
Statement
;
break
;
...
...
@@ -108,8 +102,8 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
if
(
ifStatement
.
Else
==
null
)
{
convertingNodesBuilder
.
Add
(
new
ExtendedSyntaxNode
(
ifStatement
,
currentLeadingTokens
,
Enumerable
.
Empty
<
SyntaxToken
>()));
currentLeadingTokens
=
new
List
<
SyntaxToken
>
();
ifStatement
,
currentLeadingTokens
.
ToImmutableAndFree
()
,
Enumerable
.
Empty
<
SyntaxToken
>()));
currentLeadingTokens
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
// Proceed the loop with the nested statement.
current
=
ifStatement
.
Statement
;
break
;
...
...
@@ -121,12 +115,10 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
}
case
SyntaxKind
.
LocalDeclarationStatement
:
// This is a situation with "var a = something;" s the innermost statements inside the loop.
// This is a situation with "var a = something;"
i
s the innermost statements inside the loop.
var
localDeclaration
=
(
LocalDeclarationStatementSyntax
)
current
;
if
(
localDeclaration
.
Declaration
.
Variables
.
All
(
variable
=>
variable
.
Initializer
!=
null
))
if
(
TryProcessLocalDeclarationStatement
(
localDeclaration
))
{
// Prepare variable declarations to be converted into separate let clauses.
ProcessLocalDeclarationStatement
(
localDeclaration
);
statementsCannotBeConverted
=
Enumerable
.
Empty
<
StatementSyntax
>();
}
else
...
...
@@ -154,39 +146,48 @@ internal sealed class CSharpConvertForEachToLinqQueryProvider
}
}
// Trailing tokens are collected in the reverse order: from ext
re
nal block down to internal ones. Reverse them.
trailingTokensBuilder
.
Reverse
();
// Trailing tokens are collected in the reverse order: from ext
er
nal block down to internal ones. Reverse them.
trailingTokensBuilder
.
Reverse
Contents
();
return
new
ForEachInfo
<
ForEachStatementSyntax
,
StatementSyntax
>(
forEachStatement
,
semanticModel
,
convertingNodesBuilder
.
ToImmutable
(),
identifiersBuilder
.
ToImmutable
(),
convertingNodesBuilder
.
ToImmutable
AndFree
(),
identifiersBuilder
.
ToImmutable
AndFree
(),
statementsCannotBeConverted
.
ToImmutableArray
(),
currentLeadingTokens
.
ToImmutableA
rray
(),
trailingTokensBuilder
.
ToImmutable
());
currentLeadingTokens
.
ToImmutableA
ndFree
(),
trailingTokensBuilder
.
ToImmutable
AndFree
());
void
ProcessLocalDeclarationStatement
(
LocalDeclarationStatementSyntax
localDeclarationStatement
)
// Try to prepare variable declarations to be converted into separate let clauses.
bool
TryProcessLocalDeclarationStatement
(
LocalDeclarationStatementSyntax
localDeclarationStatement
)
{
var
localDeclarationLeadingTrivia
=
new
IEnumerable
<
SyntaxTrivia
>[]
{
currentLeadingTokens
.
GetTrivia
(),
// Do not support declarations without initialization.
// int a = 0, b, c = 0;
if
(
localDeclarationStatement
.
Declaration
.
Variables
.
All
(
variable
=>
variable
.
Initializer
!=
null
))
{
var
localDeclarationLeadingTrivia
=
new
IEnumerable
<
SyntaxTrivia
>[]
{
currentLeadingTokens
.
ToImmutableAndFree
().
GetTrivia
(),
localDeclarationStatement
.
Declaration
.
Type
.
GetLeadingTrivia
(),
localDeclarationStatement
.
Declaration
.
Type
.
GetTrailingTrivia
()
}.
Flatten
();
var
localDeclarationTrailingTrivia
=
SyntaxNodeOrTokenExtensions
.
GetTrivia
(
localDeclarationStatement
.
SemicolonToken
);
var
separators
=
localDeclarationStatement
.
Declaration
.
Variables
.
GetSeparators
().
ToArray
();
for
(
var
i
=
0
;
i
<
localDeclarationStatement
.
Declaration
.
Variables
.
Count
;
i
++)
{
var
variable
=
localDeclarationStatement
.
Declaration
.
Variables
[
i
];
convertingNodesBuilder
.
Add
(
new
ExtendedSyntaxNode
(
variable
,
i
==
0
?
localDeclarationLeadingTrivia
:
separators
[
i
-
1
].
TrailingTrivia
,
i
==
localDeclarationStatement
.
Declaration
.
Variables
.
Count
-
1
?
(
IEnumerable
<
SyntaxTrivia
>)
localDeclarationTrailingTrivia
:
separators
[
i
].
LeadingTrivia
));
identifiersBuilder
.
Add
(
variable
.
Identifier
);
currentLeadingTokens
=
ArrayBuilder
<
SyntaxToken
>.
GetInstance
();
var
localDeclarationTrailingTrivia
=
SyntaxNodeOrTokenExtensions
.
GetTrivia
(
localDeclarationStatement
.
SemicolonToken
);
var
separators
=
localDeclarationStatement
.
Declaration
.
Variables
.
GetSeparators
().
ToArray
();
for
(
var
i
=
0
;
i
<
localDeclarationStatement
.
Declaration
.
Variables
.
Count
;
i
++)
{
var
variable
=
localDeclarationStatement
.
Declaration
.
Variables
[
i
];
convertingNodesBuilder
.
Add
(
new
ExtendedSyntaxNode
(
variable
,
i
==
0
?
localDeclarationLeadingTrivia
:
separators
[
i
-
1
].
TrailingTrivia
,
i
==
localDeclarationStatement
.
Declaration
.
Variables
.
Count
-
1
?
(
IEnumerable
<
SyntaxTrivia
>)
localDeclarationTrailingTrivia
:
separators
[
i
].
LeadingTrivia
));
identifiersBuilder
.
Add
(
variable
.
Identifier
);
}
return
true
;
}
currentLeadingTokens
=
new
List
<
SyntaxToken
>()
;
return
false
;
}
}
...
...
src/Features/CSharp/Portable/ConvertLinq/ConvertForEachToLinqQuery/DefaultConverter.cs
浏览文件 @
998e0c04
...
...
@@ -23,10 +23,10 @@ public DefaultConverter(ForEachInfo<ForEachStatementSyntax, StatementSyntax> for
public
override
void
Convert
(
SyntaxEditor
editor
,
CancellationToken
cancellationToken
)
{
// Filter out identifiers which are not used in statements.
var
variableNamesRead
y
Inside
=
new
HashSet
<
string
>(
ForEachInfo
.
Statements
var
variableNamesReadInside
=
new
HashSet
<
string
>(
ForEachInfo
.
Statements
.
SelectMany
(
statement
=>
ForEachInfo
.
SemanticModel
.
AnalyzeDataFlow
(
statement
).
ReadInside
).
Select
(
symbol
=>
symbol
.
Name
));
var
identifiersUsedInStatements
=
ForEachInfo
.
Identifiers
.
Where
(
identifier
=>
variableNamesRead
y
Inside
.
Contains
(
identifier
.
ValueText
));
.
Where
(
identifier
=>
variableNamesReadInside
.
Contains
(
identifier
.
ValueText
));
// If there is a single statement and it is a block, leave it as is.
// Otherwise, wrap with a block.
...
...
src/Features/Core/Portable/ConvertLinq/ConvertForEachToLinqQuery/AbstractConvertForEachToLinqQueryProvider.cs
浏览文件 @
998e0c04
...
...
@@ -71,6 +71,11 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return
;
}
if
(
forEachStatement
.
ContainsDiagnostics
)
{
return
;
}
// Do not try to refactor queries with conditional compilation in them.
if
(
forEachStatement
.
ContainsDirectives
)
{
...
...
src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.de.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.es.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.it.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.tr.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hans.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
src/Features/Core/Portable/xlf/FeaturesResources.zh-Hant.xlf
浏览文件 @
998e0c04
...
...
@@ -25,6 +25,7 @@
<trans-unit
id=
"Convert_to_query"
>
<source>
Convert to query
</source>
<target
state=
"new"
>
Convert to query
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"Add_to_0"
>
<source>
Add to '{0}'
</source>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录