提交 a5ec068b 编写于 作者: M Martin Strecker

Changes requested by code review.

上级 70d2add8
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.ConditionalExpressionInStringInterpolation namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.ConditionalExpressionInStringInterpolation
{ {
public class CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest public class CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{ {
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace) internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (null, new CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider()); => (null, new CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider());
private async Task TestInMethodAsync(string initialMethodBody, string expectedMethodBody) private async Task TestInMethodAsync(string initialMethodBody, string expectedMethodBody)
{ {
...@@ -29,7 +29,7 @@ public static M() ...@@ -29,7 +29,7 @@ public static M()
string.Format(template, expectedMethodBody)).ConfigureAwait(false); string.Format(template, expectedMethodBody)).ConfigureAwait(false);
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisSimpleConditionalExpression() public async Task TestAddParenthesisSimpleConditionalExpression()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -37,7 +37,7 @@ public async Task TestAddParenthesisSimpleConditionalExpression() ...@@ -37,7 +37,7 @@ public async Task TestAddParenthesisSimpleConditionalExpression()
@"var s = $""{ (true ? 1 : 2)}"";"); @"var s = $""{ (true ? 1 : 2)}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisMultiLineConditionalExpression1() public async Task TestAddParenthesisMultiLineConditionalExpression1()
{ {
await TestInMethodAsync(@" await TestInMethodAsync(@"
...@@ -51,7 +51,7 @@ public async Task TestAddParenthesisMultiLineConditionalExpression1() ...@@ -51,7 +51,7 @@ public async Task TestAddParenthesisMultiLineConditionalExpression1()
"); ");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisMultiLineConditionalExpression2() public async Task TestAddParenthesisMultiLineConditionalExpression2()
{ {
await TestInMethodAsync(@" await TestInMethodAsync(@"
...@@ -73,7 +73,7 @@ public async Task TestAddParenthesisMultiLineConditionalExpression2() ...@@ -73,7 +73,7 @@ public async Task TestAddParenthesisMultiLineConditionalExpression2()
"); ");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWithTrivia() public async Task TestAddParenthesisWithTrivia()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -81,7 +81,7 @@ public async Task TestAddParenthesisWithTrivia() ...@@ -81,7 +81,7 @@ public async Task TestAddParenthesisWithTrivia()
@"var s = $""{ /* Leading1 */ (true /* Leading2 */ ? /* TruePart1 */ 1 /* TruePart2 */: /* FalsePart1 */ 2 /* FalsePart2 */ )}"";"); @"var s = $""{ /* Leading1 */ (true /* Leading2 */ ? /* TruePart1 */ 1 /* TruePart2 */: /* FalsePart1 */ 2 /* FalsePart2 */ )}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisClosingBracketInFalseCondition() public async Task TestAddParenthesisClosingBracketInFalseCondition()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -89,7 +89,7 @@ public async Task TestAddParenthesisClosingBracketInFalseCondition() ...@@ -89,7 +89,7 @@ public async Task TestAddParenthesisClosingBracketInFalseCondition()
@"var s = $""{ (true ? new int[0] : new int[] {})}"";"); @"var s = $""{ (true ? new int[0] : new int[] {})}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisStringLiteralInFalseCondition() public async Task TestAddParenthesisStringLiteralInFalseCondition()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -97,7 +97,7 @@ public async Task TestAddParenthesisStringLiteralInFalseCondition() ...@@ -97,7 +97,7 @@ public async Task TestAddParenthesisStringLiteralInFalseCondition()
@"var s = $""{ (true ? ""1"" : ""2"")}"";"); @"var s = $""{ (true ? ""1"" : ""2"")}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition() public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -105,7 +105,7 @@ public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition() ...@@ -105,7 +105,7 @@ public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition()
@"var s = $""{ (true ? ""1"" : @""""""2"""""")}"";"); @"var s = $""{ (true ? ""1"" : @""""""2"""""")}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingParenthesisInLiteral() public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingParenthesisInLiteral()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -113,7 +113,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingPare ...@@ -113,7 +113,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingPare
@"var s = $""{ (true ? ""1"" : ""2)"")}"";"); @"var s = $""{ (true ? ""1"" : ""2)"")}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoubleQuotes() public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoubleQuotes()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -121,7 +121,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoub ...@@ -121,7 +121,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoub
@"var s = $""{ (true ? ""1"" : ""2\"""")}"";"); @"var s = $""{ (true ? ""1"" : ""2\"""")}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeContent() public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeContent()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -129,7 +129,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeCon ...@@ -129,7 +129,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeCon
@"var s = $""{ (true ? ""1"" : ""M(new int[] {}, \""Parameter\"");"")}"";"); @"var s = $""{ (true ? ""1"" : ""M(new int[] {}, \""Parameter\"");"")}"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisNestedConditionalExpression1() public async Task TestAddParenthesisNestedConditionalExpression1()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -137,7 +137,7 @@ public async Task TestAddParenthesisNestedConditionalExpression1() ...@@ -137,7 +137,7 @@ public async Task TestAddParenthesisNestedConditionalExpression1()
@"var s2 = $""{ (true ? ""1"" : (false ? ""2"" : ""3""))};"); @"var s2 = $""{ (true ? ""1"" : (false ? ""2"" : ""3""))};");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisNestedConditionalExpression2() public async Task TestAddParenthesisNestedConditionalExpression2()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -145,7 +145,7 @@ public async Task TestAddParenthesisNestedConditionalExpression2() ...@@ -145,7 +145,7 @@ public async Task TestAddParenthesisNestedConditionalExpression2()
@"var s2 = $""{ (true ? ""1"" : false ? ""2"" : ""3"")};"); @"var s2 = $""{ (true ? ""1"" : false ? ""2"" : ""3"")};");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisNestedConditionalWithNestedInterpolatedString() public async Task TestAddParenthesisNestedConditionalWithNestedInterpolatedString()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -153,7 +153,7 @@ public async Task TestAddParenthesisNestedConditionalWithNestedInterpolatedStrin ...@@ -153,7 +153,7 @@ public async Task TestAddParenthesisNestedConditionalWithNestedInterpolatedStrin
@"var s2 = $""{ (true ? ""1"" : false ? $""{ (true ? ""2"" : ""3"")}"" : ""4"") }"""); @"var s2 = $""{ (true ? ""1"" : false ? $""{ (true ? ""2"" : ""3"")}"" : ""4"") }""");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisMultipleInterpolatedSections1() public async Task TestAddParenthesisMultipleInterpolatedSections1()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -161,7 +161,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections1() ...@@ -161,7 +161,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections1()
@"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { (true ? ""Text5"" : ""Text6"")} Text7"";"); @"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { (true ? ""Text5"" : ""Text6"")} Text7"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisMultipleInterpolatedSections2() public async Task TestAddParenthesisMultipleInterpolatedSections2()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -169,7 +169,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections2() ...@@ -169,7 +169,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections2()
@"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { (true ? ""Text5"" : ""Text6"")} Text7"";"); @"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { (true ? ""Text5"" : ""Text6"")} Text7"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisMultipleInterpolatedSections3() public async Task TestAddParenthesisMultipleInterpolatedSections3()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -177,7 +177,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections3() ...@@ -177,7 +177,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections3()
@"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { true ? ""Text5"" : ""Text6""} Text7"";"); @"var s3 = $""Text1 { (true ? ""Text2"" : ""Text3"")} Text4 { true ? ""Text5"" : ""Text6""} Text7"";");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWhileTyping1() public async Task TestAddParenthesisWhileTyping1()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -191,7 +191,7 @@ public async Task TestAddParenthesisWhileTyping1() ...@@ -191,7 +191,7 @@ public async Task TestAddParenthesisWhileTyping1()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWhileTyping2() public async Task TestAddParenthesisWhileTyping2()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -205,7 +205,7 @@ public async Task TestAddParenthesisWhileTyping2() ...@@ -205,7 +205,7 @@ public async Task TestAddParenthesisWhileTyping2()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWhileTyping3() public async Task TestAddParenthesisWhileTyping3()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -219,7 +219,7 @@ public async Task TestAddParenthesisWhileTyping3() ...@@ -219,7 +219,7 @@ public async Task TestAddParenthesisWhileTyping3()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWhileTyping4() public async Task TestAddParenthesisWhileTyping4()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -233,7 +233,7 @@ public async Task TestAddParenthesisWhileTyping4() ...@@ -233,7 +233,7 @@ public async Task TestAddParenthesisWhileTyping4()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWhileTyping5() public async Task TestAddParenthesisWhileTyping5()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -247,7 +247,7 @@ public async Task TestAddParenthesisWhileTyping5() ...@@ -247,7 +247,7 @@ public async Task TestAddParenthesisWhileTyping5()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1() public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -261,7 +261,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1() ...@@ -261,7 +261,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2() public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -275,7 +275,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2() ...@@ -275,7 +275,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2()
NextLineOfCode("); NextLineOfCode(");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3() public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3()
{ {
await TestInMethodAsync( await TestInMethodAsync(
...@@ -289,7 +289,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3() ...@@ -289,7 +289,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3()
NextLineOfCode();"); NextLineOfCode();");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)]
public async Task TestAddParenthesisAddOpeningParenthesisOnly() public async Task TestAddParenthesisAddOpeningParenthesisOnly()
{ {
await TestInMethodAsync( await TestInMethodAsync(
......
...@@ -40,7 +40,7 @@ public static class Features ...@@ -40,7 +40,7 @@ public static class Features
public const string CodeActionsAddNew = "CodeActions.AddNew"; public const string CodeActionsAddNew = "CodeActions.AddNew";
public const string CodeActionsAddOverload = "CodeActions.AddOverloads"; public const string CodeActionsAddOverload = "CodeActions.AddOverloads";
public const string CodeActionsAddParameter = "CodeActions.AddParameter"; public const string CodeActionsAddParameter = "CodeActions.AddParameter";
public const string CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString = "CodeActions.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString"; public const string CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString = "CodeActions.AddParenthesesAroundConditionalExpressionInInterpolatedString";
public const string CodeActionsChangeToAsync = "CodeActions.ChangeToAsync"; public const string CodeActionsChangeToAsync = "CodeActions.ChangeToAsync";
public const string CodeActionsChangeToIEnumerable = "CodeActions.ChangeToIEnumerable"; public const string CodeActionsChangeToIEnumerable = "CodeActions.ChangeToIEnumerable";
public const string CodeActionsChangeToYield = "CodeActions.ChangeToYield"; public const string CodeActionsChangeToYield = "CodeActions.ChangeToYield";
......
...@@ -71,20 +71,20 @@ internal class CSharpFeaturesResources { ...@@ -71,20 +71,20 @@ internal class CSharpFeaturesResources {
} }
/// <summary> /// <summary>
/// Sucht eine lokalisierte Zeichenfolge, die Add &apos;this.&apos; ähnelt. /// Sucht eine lokalisierte Zeichenfolge, die Add parentheses ähnelt.
/// </summary> /// </summary>
internal static string Add_this { internal static string Add_parentheses_around_conditional_expression_in_interpolated_string {
get { get {
return ResourceManager.GetString("Add_this", resourceCulture); return ResourceManager.GetString("Add_parentheses_around_conditional_expression_in_interpolated_string", resourceCulture);
} }
} }
/// <summary> /// <summary>
/// Sucht eine lokalisierte Zeichenfolge, die Add parenthesis ähnelt. /// Sucht eine lokalisierte Zeichenfolge, die Add &apos;this.&apos; ähnelt.
/// </summary> /// </summary>
internal static string AddParenthesisAroundConditionalExpressionInInterpolatedString { internal static string Add_this {
get { get {
return ResourceManager.GetString("AddParenthesisAroundConditionalExpressionInInterpolatedString", resourceCulture); return ResourceManager.GetString("Add_this", resourceCulture);
} }
} }
......
...@@ -524,7 +524,7 @@ ...@@ -524,7 +524,7 @@
<data name="Use_is_null_check" xml:space="preserve"> <data name="Use_is_null_check" xml:space="preserve">
<value>Use 'is null' check</value> <value>Use 'is null' check</value>
</data> </data>
<data name="AddParenthesisAroundConditionalExpressionInInterpolatedString" xml:space="preserve"> <data name="Add_parentheses_around_conditional_expression_in_interpolated_string" xml:space="preserve">
<value>Add parenthesis</value> <value>Add parentheses</value>
</data> </data>
</root> </root>
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.ConditionalExpressionInStringInterpolation namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.ConditionalExpressionInStringInterpolation
{ {
internal partial class CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider [ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddParenthesesAroundConditionalExpressionInInterpolatedString), Shared]
internal partial class CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider : CodeFixProvider
{ {
private const string CS8361 = nameof(CS8361); //A conditional expression cannot be used directly in a string interpolation because the ':' ends the interpolation.Parenthesize the conditional expression.
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS8361);
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
var conditionalExpression = token.GetAncestor<ConditionalExpressionSyntax>();
if (conditionalExpression != null)
{
var documentChangeAction = new MyCodeAction(
cancellationToken => GetChangedDocumentAsync(context.Document, conditionalExpression.SpanStart, cancellationToken));
context.RegisterCodeFix(documentChangeAction, diagnostic);
}
}
private static async Task<Document> GetChangedDocumentAsync(Document document, int conditionalExpressionSyntaxStartPosition, CancellationToken cancellationToken) private static async Task<Document> GetChangedDocumentAsync(Document document, int conditionalExpressionSyntaxStartPosition, CancellationToken cancellationToken)
{ {
...@@ -39,5 +65,16 @@ private static async Task<Document> GetChangedDocumentAsync(Document document, i ...@@ -39,5 +65,16 @@ private static async Task<Document> GetChangedDocumentAsync(Document document, i
return documentWithOpenParenthesis; return documentWithOpenParenthesis;
} }
private class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
: base(
CSharpFeaturesResources.Add_parentheses_around_conditional_expression_in_interpolated_string,
createChangedDocument,
CSharpFeaturesResources.Add_parentheses_around_conditional_expression_in_interpolated_string)
{
}
}
} }
} }
// 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.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.ConditionalExpressionInStringInterpolation
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.AddParenthesisAroundConditionalExpressionInInterpolatedString), Shared]
internal partial class CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider : CodeFixProvider
{
private const string CS8361 = nameof(CS8361); //A conditional expression cannot be used directly in a string interpolation because the ':' ends the interpolation.Parenthesize the conditional expression.
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS8361);
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
var conditionalExpression = token.GetAncestor<ConditionalExpressionSyntax>();
if (conditionalExpression != null)
{
var documentChangeAction = new CodeAction.DocumentChangeAction(
CSharpFeaturesResources.AddParenthesisAroundConditionalExpressionInInterpolatedString,
cancellationToken => GetChangedDocumentAsync(context.Document, conditionalExpression.SpanStart, cancellationToken));
context.RegisterCodeFix(documentChangeAction, diagnostic);
}
}
}
}
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -637,9 +637,9 @@ ...@@ -637,9 +637,9 @@
<target state="new">Use 'is null' check</target> <target state="new">Use 'is null' check</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="AddParenthesisAroundConditionalExpressionInInterpolatedString"> <trans-unit id="Add_parentheses_around_conditional_expression_in_interpolated_string">
<source>Add parenthesis</source> <source>Add parentheses</source>
<target state="new">Add parenthesis</target> <target state="new">Add parentheses</target>
<note /> <note />
</trans-unit> </trans-unit>
</body> </body>
......
...@@ -8,7 +8,7 @@ internal static class PredefinedCodeFixProviderNames ...@@ -8,7 +8,7 @@ internal static class PredefinedCodeFixProviderNames
public const string AddAwait = nameof(AddAwait); public const string AddAwait = nameof(AddAwait);
public const string AddAsync = nameof(AddAsync); public const string AddAsync = nameof(AddAsync);
public const string AddParameter = nameof(AddParameter); public const string AddParameter = nameof(AddParameter);
public const string AddParenthesisAroundConditionalExpressionInInterpolatedString = nameof(AddParenthesisAroundConditionalExpressionInInterpolatedString); public const string AddParenthesesAroundConditionalExpressionInInterpolatedString = nameof(AddParenthesesAroundConditionalExpressionInInterpolatedString);
public const string ApplyNamingStyle = nameof(ApplyNamingStyle); public const string ApplyNamingStyle = nameof(ApplyNamingStyle);
public const string AddBraces = nameof(AddBraces); public const string AddBraces = nameof(AddBraces);
public const string ChangeReturnType = nameof(ChangeReturnType); public const string ChangeReturnType = nameof(ChangeReturnType);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册