From a5ec068bc5ee8d68ee323ce200025b3ff063bece Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 12 Dec 2017 12:19:28 +0100 Subject: [PATCH] Changes requested by code review. --- ...InterpolatedStringCodeFixProviderTests.cs} | 54 +++++++++---------- src/EditorFeatures/TestUtilities/Traits.cs | 2 +- .../CSharpFeaturesResources.Designer.cs | 12 ++--- .../Portable/CSharpFeaturesResources.resx | 4 +- ...ionInInterpolatedStringCodeFixProvider.cs} | 39 +++++++++++++- ...sionInInterpolatedStringCodeFixProvider.cs | 37 ------------- .../xlf/CSharpFeaturesResources.cs.xlf | 6 +-- .../xlf/CSharpFeaturesResources.de.xlf | 6 +-- .../xlf/CSharpFeaturesResources.es.xlf | 6 +-- .../xlf/CSharpFeaturesResources.fr.xlf | 6 +-- .../xlf/CSharpFeaturesResources.it.xlf | 6 +-- .../xlf/CSharpFeaturesResources.ja.xlf | 6 +-- .../xlf/CSharpFeaturesResources.ko.xlf | 6 +-- .../xlf/CSharpFeaturesResources.pl.xlf | 6 +-- .../xlf/CSharpFeaturesResources.pt-BR.xlf | 6 +-- .../xlf/CSharpFeaturesResources.ru.xlf | 6 +-- .../xlf/CSharpFeaturesResources.tr.xlf | 6 +-- .../xlf/CSharpFeaturesResources.zh-Hans.xlf | 6 +-- .../xlf/CSharpFeaturesResources.zh-Hant.xlf | 6 +-- .../PredefinedCodeFixProviderNames.cs | 2 +- 20 files changed, 114 insertions(+), 114 deletions(-) rename src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/{CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs => CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs} (87%) rename src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/{AddParenthesisCodeAction.cs => CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs} (54%) delete mode 100644 src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs diff --git a/src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs b/src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs similarity index 87% rename from src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs rename to src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs index acbb0878904..513b6fa2b6b 100644 --- a/src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs +++ b/src/EditorFeatures/CSharpTest/Diagnostics/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests.cs @@ -9,10 +9,10 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.ConditionalExpressionInStringInterpolation { - public class CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest + public class CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProviderTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest { internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace) - => (null, new CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider()); + => (null, new CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider()); private async Task TestInMethodAsync(string initialMethodBody, string expectedMethodBody) { @@ -29,7 +29,7 @@ public static M() string.Format(template, expectedMethodBody)).ConfigureAwait(false); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisSimpleConditionalExpression() { await TestInMethodAsync( @@ -37,7 +37,7 @@ public async Task TestAddParenthesisSimpleConditionalExpression() @"var s = $""{ (true ? 1 : 2)}"";"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisMultiLineConditionalExpression1() { await TestInMethodAsync(@" @@ -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() { await TestInMethodAsync(@" @@ -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() { await TestInMethodAsync( @@ -81,7 +81,7 @@ public async Task TestAddParenthesisWithTrivia() @"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() { await TestInMethodAsync( @@ -89,7 +89,7 @@ public async Task TestAddParenthesisClosingBracketInFalseCondition() @"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() { await TestInMethodAsync( @@ -97,7 +97,7 @@ public async Task TestAddParenthesisStringLiteralInFalseCondition() @"var s = $""{ (true ? ""1"" : ""2"")}"";"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition() { await TestInMethodAsync( @@ -105,7 +105,7 @@ public async Task TestAddParenthesisVerbatimStringLiteralInFalseCondition() @"var s = $""{ (true ? ""1"" : @""""""2"""""")}"";"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingParenthesisInLiteral() { await TestInMethodAsync( @@ -113,7 +113,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithClosingPare @"var s = $""{ (true ? ""1"" : ""2)"")}"";"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoubleQuotes() { await TestInMethodAsync( @@ -121,7 +121,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithEscapedDoub @"var s = $""{ (true ? ""1"" : ""2\"""")}"";"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeContent() { await TestInMethodAsync( @@ -129,7 +129,7 @@ public async Task TestAddParenthesisStringLiteralInFalseConditionWithCodeLikeCon @"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() { await TestInMethodAsync( @@ -137,7 +137,7 @@ public async Task TestAddParenthesisNestedConditionalExpression1() @"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() { await TestInMethodAsync( @@ -145,7 +145,7 @@ public async Task TestAddParenthesisNestedConditionalExpression2() @"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() { await TestInMethodAsync( @@ -153,7 +153,7 @@ public async Task TestAddParenthesisNestedConditionalWithNestedInterpolatedStrin @"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() { await TestInMethodAsync( @@ -161,7 +161,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections1() @"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() { await TestInMethodAsync( @@ -169,7 +169,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections2() @"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() { await TestInMethodAsync( @@ -177,7 +177,7 @@ public async Task TestAddParenthesisMultipleInterpolatedSections3() @"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() { await TestInMethodAsync( @@ -191,7 +191,7 @@ public async Task TestAddParenthesisWhileTyping1() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWhileTyping2() { await TestInMethodAsync( @@ -205,7 +205,7 @@ public async Task TestAddParenthesisWhileTyping2() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWhileTyping3() { await TestInMethodAsync( @@ -219,7 +219,7 @@ public async Task TestAddParenthesisWhileTyping3() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWhileTyping4() { await TestInMethodAsync( @@ -233,7 +233,7 @@ public async Task TestAddParenthesisWhileTyping4() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWhileTyping5() { await TestInMethodAsync( @@ -247,7 +247,7 @@ public async Task TestAddParenthesisWhileTyping5() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1() { await TestInMethodAsync( @@ -261,7 +261,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied1() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2() { await TestInMethodAsync( @@ -275,7 +275,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied2() NextLineOfCode("); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3() { await TestInMethodAsync( @@ -289,7 +289,7 @@ public async Task TestAddParenthesisWithCS1026PresentBeforeFixIsApplied3() NextLineOfCode();"); } - [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesisAroundConditionalExpressionInInterpolatedString)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddParenthesesAroundConditionalExpressionInInterpolatedString)] public async Task TestAddParenthesisAddOpeningParenthesisOnly() { await TestInMethodAsync( diff --git a/src/EditorFeatures/TestUtilities/Traits.cs b/src/EditorFeatures/TestUtilities/Traits.cs index 60765f83f37..8c6cab85256 100644 --- a/src/EditorFeatures/TestUtilities/Traits.cs +++ b/src/EditorFeatures/TestUtilities/Traits.cs @@ -40,7 +40,7 @@ public static class Features public const string CodeActionsAddNew = "CodeActions.AddNew"; public const string CodeActionsAddOverload = "CodeActions.AddOverloads"; 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 CodeActionsChangeToIEnumerable = "CodeActions.ChangeToIEnumerable"; public const string CodeActionsChangeToYield = "CodeActions.ChangeToYield"; diff --git a/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs b/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs index d80e6a9808a..1d7a4f1c93c 100644 --- a/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs +++ b/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs @@ -71,20 +71,20 @@ internal class CSharpFeaturesResources { } /// - /// Sucht eine lokalisierte Zeichenfolge, die Add 'this.' ähnelt. + /// Sucht eine lokalisierte Zeichenfolge, die Add parentheses ähnelt. /// - internal static string Add_this { + internal static string Add_parentheses_around_conditional_expression_in_interpolated_string { get { - return ResourceManager.GetString("Add_this", resourceCulture); + return ResourceManager.GetString("Add_parentheses_around_conditional_expression_in_interpolated_string", resourceCulture); } } /// - /// Sucht eine lokalisierte Zeichenfolge, die Add parenthesis ähnelt. + /// Sucht eine lokalisierte Zeichenfolge, die Add 'this.' ähnelt. /// - internal static string AddParenthesisAroundConditionalExpressionInInterpolatedString { + internal static string Add_this { get { - return ResourceManager.GetString("AddParenthesisAroundConditionalExpressionInInterpolatedString", resourceCulture); + return ResourceManager.GetString("Add_this", resourceCulture); } } diff --git a/src/Features/CSharp/Portable/CSharpFeaturesResources.resx b/src/Features/CSharp/Portable/CSharpFeaturesResources.resx index 0782bb38981..86a50c4d982 100644 --- a/src/Features/CSharp/Portable/CSharpFeaturesResources.resx +++ b/src/Features/CSharp/Portable/CSharpFeaturesResources.resx @@ -524,7 +524,7 @@ Use 'is null' check - - Add parenthesis + + Add parentheses \ No newline at end of file diff --git a/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/AddParenthesisCodeAction.cs b/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs similarity index 54% rename from src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/AddParenthesisCodeAction.cs rename to src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs index 41c729d257c..45df68cec09 100644 --- a/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/AddParenthesisCodeAction.cs +++ b/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesesAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs @@ -1,14 +1,40 @@ // 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.Tasks; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; 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 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(); + if (conditionalExpression != null) + { + var documentChangeAction = new MyCodeAction( + cancellationToken => GetChangedDocumentAsync(context.Document, conditionalExpression.SpanStart, cancellationToken)); + context.RegisterCodeFix(documentChangeAction, diagnostic); + } + } private static async Task GetChangedDocumentAsync(Document document, int conditionalExpressionSyntaxStartPosition, CancellationToken cancellationToken) { @@ -39,5 +65,16 @@ private static async Task GetChangedDocumentAsync(Document document, i return documentWithOpenParenthesis; } + + private class MyCodeAction : CodeAction.DocumentChangeAction + { + public MyCodeAction(Func> createChangedDocument) + : base( + CSharpFeaturesResources.Add_parentheses_around_conditional_expression_in_interpolated_string, + createChangedDocument, + CSharpFeaturesResources.Add_parentheses_around_conditional_expression_in_interpolated_string) + { + } + } } } diff --git a/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs b/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs deleted file mode 100644 index 6ab5c593ff1..00000000000 --- a/src/Features/CSharp/Portable/CodeFixes/ConditionalExpressionInStringInterpolation/CSharpAddParenthesisAroundConditionalExpressionInInterpolatedStringCodeFixProvider.cs +++ /dev/null @@ -1,37 +0,0 @@ -// 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 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(); - if (conditionalExpression != null) - { - var documentChangeAction = new CodeAction.DocumentChangeAction( - CSharpFeaturesResources.AddParenthesisAroundConditionalExpressionInInterpolatedString, - cancellationToken => GetChangedDocumentAsync(context.Document, conditionalExpression.SpanStart, cancellationToken)); - context.RegisterCodeFix(documentChangeAction, diagnostic); - } - } - } -} diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.cs.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.cs.xlf index 089102a8423..41452422e43 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.cs.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.cs.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.de.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.de.xlf index cd3505377e9..ecd5c99f589 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.de.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.de.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.es.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.es.xlf index f2172c41646..6b1af612cf6 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.es.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.es.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.fr.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.fr.xlf index 6f57b1a4e1f..2ab4e520eaa 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.fr.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.fr.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.it.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.it.xlf index e2efc61f306..43292cb31e0 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.it.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.it.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ja.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ja.xlf index 0a1bd61d02b..0c29af84e5c 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ja.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ja.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ko.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ko.xlf index 6e8d8d291b8..5f7573bf73e 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ko.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ko.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pl.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pl.xlf index 9803d89dd35..1bb12ba5563 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pl.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pl.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pt-BR.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pt-BR.xlf index ff0649547e4..89a79ab2485 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pt-BR.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.pt-BR.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ru.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ru.xlf index 47a2908b78b..d20bee94cfa 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ru.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.ru.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.tr.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.tr.xlf index d99e2b366a2..8a566340f16 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.tr.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.tr.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hans.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hans.xlf index 5577502f158..196d287439e 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hans.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hans.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hant.xlf b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hant.xlf index 6aca7057765..dfceae04de9 100644 --- a/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hant.xlf +++ b/src/Features/CSharp/Portable/xlf/CSharpFeaturesResources.zh-Hant.xlf @@ -637,9 +637,9 @@ Use 'is null' check - - Add parenthesis - Add parenthesis + + Add parentheses + Add parentheses diff --git a/src/Features/Core/Portable/CodeFixes/PredefinedCodeFixProviderNames.cs b/src/Features/Core/Portable/CodeFixes/PredefinedCodeFixProviderNames.cs index a802596b763..69adbf08fd9 100644 --- a/src/Features/Core/Portable/CodeFixes/PredefinedCodeFixProviderNames.cs +++ b/src/Features/Core/Portable/CodeFixes/PredefinedCodeFixProviderNames.cs @@ -8,7 +8,7 @@ internal static class PredefinedCodeFixProviderNames public const string AddAwait = nameof(AddAwait); public const string AddAsync = nameof(AddAsync); 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 AddBraces = nameof(AddBraces); public const string ChangeReturnType = nameof(ChangeReturnType); -- GitLab