未验证 提交 179d8d64 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #42411 from CyrusNajmabadi/pluralizeVarNames

Infer better variable names for a foreach expression.
......@@ -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" />
......
......@@ -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" />
......
......@@ -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 vs1 = new int[] { 1, 2 };
var vs = new int[] { 3, 4 };
foreach (var num in vs1)
{
foreach (var a in new int[] { 5, 6 })
{
foreach (var x1 in v)
foreach (var x1 in vs)
{
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 vs2 = new int[] { 1, 2 };
var vs1 = new int[] { 3, 4 };
var vs = new int[] { 7, 8 };
foreach (var num in vs2)
{
foreach (var a in new int[] { 5, 6 })
{
foreach (var x1 in v1)
foreach (var x1 in vs1)
{
if (object.Equals(num, x1))
{
foreach (var x2 in v)
foreach (var x2 in vs)
{
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> queryables()
{
foreach (int n1 in nums.AsQueryable())
{
......@@ -2488,7 +2488,7 @@ IEnumerable<int> queryable()
}
}
IEnumerable<int> q = queryable();
IEnumerable<int> q = queryables();
}
}";
......
......@@ -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:vs|} = a = new[] { 1, 2, 3 };
int[] temp = checked(vs);
}
}",
options: ImplicitTypingEverywhere());
......@@ -5980,8 +5980,8 @@ class C
byte[] getArray() => null;
void test()
{
byte[] {|Rename:v|} = getArray();
var goo = v[0];
byte[] {|Rename:vs|} = getArray();
var goo = vs[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()
......
......@@ -2323,22 +2323,23 @@ enum A
[AttributeUsage(AttributeTargets.Class)]
class MyAttrAttribute : Attribute
{
private int[] v1;
private int[] vs;
private A a1;
private bool v2;
private byte v3;
private char v4;
private short v5;
private int v6;
private long v7;
private double v8;
private float v9;
private string v10;
public MyAttrAttribute(int[] v1, A a1, bool v2, byte v3, char v4, short v5, int v6, long v7, double v8, float v9, string v10)
{
this.v1 = v1;
private bool v1;
private byte v2;
private char v3;
private short v4;
private int v5;
private long v6;
private double v7;
private float v8;
private string v9;
public MyAttrAttribute(int[] vs, A a1, bool v1, byte v2, char v3, short v4, int v5, long v6, double v7, float v8, string v9)
{
this.vs = 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;
}
}
......
......@@ -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" />
......
......@@ -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.IdentifierName)
if (current is IdentifierNameSyntax identifierName)
{
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.
先完成此消息的编辑!
想要评论请 注册