Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
179d8d64
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,发现更多精彩内容 >>
未验证
提交
179d8d64
编写于
3月 18, 2020
作者:
C
CyrusNajmabadi
提交者:
GitHub
3月 18, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #42411 from CyrusNajmabadi/pluralizeVarNames
Infer better variable names for a foreach expression.
上级
21426ab9
4a870020
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
109 addition
and
43 deletion
+109
-43
src/CodeStyle/CSharp/Analyzers/Microsoft.CodeAnalysis.CSharp.CodeStyle.csproj
.../Analyzers/Microsoft.CodeAnalysis.CSharp.CodeStyle.csproj
+1
-0
src/CodeStyle/CSharp/CodeFixes/Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.csproj
...ixes/Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.csproj
+1
-0
src/EditorFeatures/CSharpTest/CodeActions/ConvertLinq/ConvertLinqQueryToForEachTests.cs
...CodeActions/ConvertLinq/ConvertLinqQueryToForEachTests.cs
+18
-18
src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
...t/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
+46
-4
src/EditorFeatures/CSharpTest/GenerateConstructor/GenerateConstructorTests.cs
...SharpTest/GenerateConstructor/GenerateConstructorTests.cs
+15
-15
src/Workspaces/CSharp/Portable/Microsoft.CodeAnalysis.CSharp.Workspaces.csproj
.../Portable/Microsoft.CodeAnalysis.CSharp.Workspaces.csproj
+3
-0
src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/SemanticModelExtensions.cs
...ns/Workspace/CSharp/Extensions/SemanticModelExtensions.cs
+25
-6
未找到文件。
src/CodeStyle/CSharp/Analyzers/Microsoft.CodeAnalysis.CSharp.CodeStyle.csproj
浏览文件 @
179d8d64
...
...
@@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference
Include=
"Microsoft.CodeAnalysis.CSharp"
Version=
"$(MicrosoftCodeAnalysisCSharpFixedVersion)"
/>
<PackageReference
Include=
"Humanizer.Core"
Version=
"$(HumanizerCoreVersion)"
PrivateAssets=
"compile"
/>
</ItemGroup>
<ItemGroup
Label=
"Project References"
>
<ProjectReference
Include=
"..\..\Core\Analyzers\Microsoft.CodeAnalysis.CodeStyle.csproj"
/>
...
...
src/CodeStyle/CSharp/CodeFixes/Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.csproj
浏览文件 @
179d8d64
...
...
@@ -36,6 +36,7 @@
</Target>
<ItemGroup>
<PackageReference
Include=
"Microsoft.CodeAnalysis.CSharp.Workspaces"
Version=
"$(MicrosoftCodeAnalysisCSharpWorkspacesFixedVersion)"
/>
<PackageReference
Include=
"Humanizer.Core"
Version=
"$(HumanizerCoreVersion)"
PrivateAssets=
"compile"
/>
</ItemGroup>
<ItemGroup
Label=
"Project References"
>
<ProjectReference
Include=
"..\..\Core\Analyzers\Microsoft.CodeAnalysis.CodeStyle.csproj"
/>
...
...
src/EditorFeatures/CSharpTest/CodeActions/ConvertLinq/ConvertLinqQueryToForEachTests.cs
浏览文件 @
179d8d64
...
...
@@ -170,13 +170,13 @@ static void Main(string[] args)
{
System.Collections.Generic.IEnumerable<int> enumerable()
{
var v1 = new int[] { 1, 2 };
var v = new int[] { 3, 4 };
foreach (var num in v1)
var v
s
1 = new int[] { 1, 2 };
var v
s
= new int[] { 3, 4 };
foreach (var num in v
s
1)
{
foreach (var a in new int[] { 5, 6 })
{
foreach (var x1 in v)
foreach (var x1 in v
s
)
{
if (object.Equals(num, x1))
{
...
...
@@ -219,18 +219,18 @@ static void Main(string[] args)
{
System.Collections.Generic.IEnumerable<int> enumerable()
{
var v2 = new int[] { 1, 2 };
var v1 = new int[] { 3, 4 };
var v = new int[] { 7, 8 };
foreach (var num in v2)
var v
s
2 = new int[] { 1, 2 };
var v
s
1 = new int[] { 3, 4 };
var v
s
= new int[] { 7, 8 };
foreach (var num in v
s
2)
{
foreach (var a in new int[] { 5, 6 })
{
foreach (var x1 in v1)
foreach (var x1 in v
s
1)
{
if (object.Equals(num, x1))
{
foreach (var x2 in v)
foreach (var x2 in v
s
)
{
if (object.Equals(num, x2))
{
...
...
@@ -2141,7 +2141,7 @@ class C
{
void M(IEnumerable<int> nums)
{
IEnumerable<B>
enumerable
()
IEnumerable<B>
@as
()
{
foreach (B a in nums)
{
...
...
@@ -2152,7 +2152,7 @@ IEnumerable<B> enumerable()
}
}
foreach (A a in
enumerable
())
foreach (A a in
@as
())
{
Console.Write(a.ToString());
}
...
...
@@ -2238,7 +2238,7 @@ class C
{
void M(IEnumerable<int> nums)
{
IEnumerable<int>
enumerable
()
IEnumerable<int>
bs
()
{
foreach (int n1 in nums)
{
...
...
@@ -2249,7 +2249,7 @@ IEnumerable<int> enumerable()
}
}
foreach (var b in
enumerable
())
foreach (var b in
bs
())
{
int n1 = 5;
Console.WriteLine(b);
...
...
@@ -2318,7 +2318,7 @@ class C
void Test()
{
IEnumerable<C>
enumerable
()
IEnumerable<C>
xes
()
{
foreach (var x in new[] { 1, 2, 3, })
{
...
...
@@ -2326,7 +2326,7 @@ IEnumerable<C> enumerable()
}
}
foreach (int x in
enumerable
())
foreach (int x in
xes
())
{
Console.Write(x);
}
...
...
@@ -2480,7 +2480,7 @@ class C
{
void M(IEnumerable<int> nums)
{
IEnumerable<int> queryable()
IEnumerable<int> queryable
s
()
{
foreach (int n1 in nums.AsQueryable())
{
...
...
@@ -2488,7 +2488,7 @@ IEnumerable<int> queryable()
}
}
IEnumerable<int> q = queryable();
IEnumerable<int> q = queryable
s
();
}
}"
;
...
...
src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs
浏览文件 @
179d8d64
...
...
@@ -1478,8 +1478,8 @@ static void Main(string[] args)
static void Main(string[] args)
{
int[] a = null;
var {|Rename:v|} = a = new[] { 1, 2, 3 };
int[] temp = checked(v);
var {|Rename:v
s
|} = a = new[] { 1, 2, 3 };
int[] temp = checked(v
s
);
}
}"
,
options
:
ImplicitTypingEverywhere
());
...
...
@@ -5980,8 +5980,8 @@ class C
byte[] getArray() => null;
void test()
{
byte[] {|Rename:v|} = getArray();
var goo = v[0];
byte[] {|Rename:v
s
|} = getArray();
var goo = v
s
[0];
}
}"
);
}
...
...
@@ -7399,6 +7399,48 @@ private object Bar()
}"
);
}
[
WorkItem
(
56
,
"https://github.com/dotnet/roslyn/issues/56"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsIntroduceVariable
)]
public
async
Task
TestGenerateNameForForeachExpression
()
{
await
TestInRegularAndScriptAsync
(
@"using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
foreach (var num in [|GetNumbers()|])
{
}
}
static IEnumerable<int> GetNumbers()
{
return new[] { 1, 2, 3 };
}
}"
,
@"using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
IEnumerable<int> {|Rename:nums|} = GetNumbers();
foreach (var num in nums)
{
}
}
static IEnumerable<int> GetNumbers()
{
return new[] { 1, 2, 3 };
}
}"
);
}
[
WorkItem
(
15770
,
"https://github.com/dotnet/roslyn/issues/15770"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsIntroduceVariable
)]
public
async
Task
TestKeepReplacementIndentation1
()
...
...
src/EditorFeatures/CSharpTest/GenerateConstructor/GenerateConstructorTests.cs
浏览文件 @
179d8d64
...
...
@@ -2323,22 +2323,23 @@ enum A
[AttributeUsage(AttributeTargets.Class)]
class MyAttrAttribute : Attribute
{
private int[] v
1
;
private int[] v
s
;
private A a1;
private bool v
2
;
private byte v
3
;
private char v
4
;
private short v
5
;
private int v
6
;
private long v
7
;
private double v
8
;
private float v
9
;
private string v
10
;
public MyAttrAttribute(int[] v
1, A a1, bool v2, byte v3, char v4, short v5, int v6, long v7, double v8, float v9, string v10
)
{
this.v
1 = v1
;
private bool v
1
;
private byte v
2
;
private char v
3
;
private short v
4
;
private int v
5
;
private long v
6
;
private double v
7
;
private float v
8
;
private string v
9
;
public MyAttrAttribute(int[] v
s, A a1, bool v1, byte v2, char v3, short v4, int v5, long v6, double v7, float v8, string v9
)
{
this.v
s = vs
;
this.a1 = a1;
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
...
...
@@ -2347,7 +2348,6 @@ public MyAttrAttribute(int[] v1, A a1, bool v2, byte v3, char v4, short v5, int
this.v7 = v7;
this.v8 = v8;
this.v9 = v9;
this.v10 = v10;
}
}
...
...
src/Workspaces/CSharp/Portable/Microsoft.CodeAnalysis.CSharp.Workspaces.csproj
浏览文件 @
179d8d64
...
...
@@ -19,6 +19,9 @@
<ProjectReference
Include=
"..\..\..\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj"
/>
<ProjectReference
Include=
"..\..\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj"
/>
</ItemGroup>
<ItemGroup>
<PackageReference
Include=
"Humanizer.Core"
Version=
"$(HumanizerCoreVersion)"
PrivateAssets=
"compile"
/>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo
Include=
"Microsoft.CodeAnalysis.CSharp.EditorFeatures"
/>
<InternalsVisibleTo
Include=
"Microsoft.CodeAnalysis.CSharp.Features"
/>
...
...
src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/SemanticModelExtensions.cs
浏览文件 @
179d8d64
...
...
@@ -6,11 +6,10 @@
using
System.Collections.Immutable
;
using
System.Linq
;
using
System.Threading
;
using
Humanizer
;
using
Microsoft.CodeAnalysis
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles
;
using
Microsoft.CodeAnalysis.PooledObjects
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
using
Microsoft.CodeAnalysis.Utilities
;
using
Roslyn.Utilities
;
...
...
@@ -205,9 +204,9 @@ private static bool CanBindToken(SyntaxToken token)
{
current
=
current
.
WalkDownParentheses
();
if
(
current
.
Kind
()
==
SyntaxKind
.
I
dentifierName
)
if
(
current
is
IdentifierNameSyntax
i
dentifierName
)
{
return
((
IdentifierNameSyntax
)
current
)
.
Identifier
.
ValueText
.
ToCamelCase
();
return
identifierName
.
Identifier
.
ValueText
.
ToCamelCase
();
}
else
if
(
current
is
MemberAccessExpressionSyntax
memberAccess
)
{
...
...
@@ -234,6 +233,11 @@ private static bool CanBindToken(SyntaxToken token)
return
name
.
Identifier
.
ValueText
.
ToCamelCase
();
}
else
if
(
current
.
Parent
is
ForEachStatementSyntax
foreachStatement
&&
foreachStatement
.
Expression
==
expression
)
{
return
foreachStatement
.
Identifier
.
ValueText
.
ToCamelCase
().
Pluralize
();
}
else
{
break
;
...
...
@@ -255,7 +259,22 @@ private static bool CanBindToken(SyntaxToken token)
// If we can't determine the type, then fallback to some placeholders.
var
type
=
info
.
Type
;
return
type
.
CreateParameterName
(
capitalize
);
var
pluralize
=
Pluralize
(
semanticModel
,
type
);
var
parameterName
=
type
.
CreateParameterName
(
capitalize
);
return
pluralize
?
parameterName
.
Pluralize
()
:
parameterName
;
}
private
static
bool
Pluralize
(
SemanticModel
semanticModel
,
ITypeSymbol
type
)
{
if
(
type
==
null
)
return
false
;
if
(
type
.
SpecialType
==
SpecialType
.
System_String
)
return
false
;
var
enumerableType
=
semanticModel
.
Compilation
.
IEnumerableOfTType
();
return
type
.
AllInterfaces
.
Any
(
i
=>
i
.
OriginalDefinition
.
Equals
(
enumerableType
));
}
private
static
string
TryGenerateNameForArgumentExpression
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录