From a3124a7cfdd3c403d1674546648b2fa397542326 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Mon, 15 May 2017 13:01:07 -0700 Subject: [PATCH] Present feature to user as 'simplify default expression'. --- .../CSharpUseDefaultLiteralCodeFixProvider.cs | 2 +- ...harpUseDefaultLiteralDiagnosticAnalyzer.cs | 8 +++--- .../Portable/FeaturesResources.Designer.cs | 27 ++++++++++++------- .../Core/Portable/FeaturesResources.resx | 7 +++-- .../Impl/Options/Formatting/StyleViewModel.cs | 4 +-- .../Core/Def/ServicesVSResources.Designer.cs | 18 ++++++------- .../Core/Def/ServicesVSResources.resx | 4 +-- .../CodeStyle/CSharpCodeStyleOptions.cs | 10 +++---- .../DefaultExpressionSyntaxExtensions.cs | 2 +- 9 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralCodeFixProvider.cs b/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralCodeFixProvider.cs index db6f09af6e5..b5b2143ed62 100644 --- a/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralCodeFixProvider.cs @@ -79,7 +79,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private class MyCodeAction : CodeAction.DocumentChangeAction { public MyCodeAction(Func> createChangedDocument) - : base(FeaturesResources.Use_default_literal, createChangedDocument) + : base(FeaturesResources.Simplify_default_expression, createChangedDocument) { } } diff --git a/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralDiagnosticAnalyzer.cs index 377337f1209..d0c38348331 100644 --- a/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/UseDefaultLiteral/CSharpUseDefaultLiteralDiagnosticAnalyzer.cs @@ -1,11 +1,9 @@ // 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.Threading; using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.CSharp.CodeStyle; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.CSharp.Utilities; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Text; @@ -16,8 +14,8 @@ internal class CSharpUseDefaultLiteralDiagnosticAnalyzer : AbstractCodeStyleDiag { public CSharpUseDefaultLiteralDiagnosticAnalyzer() : base(IDEDiagnosticIds.UseDefaultLiteralDiagnosticId, - new LocalizableResourceString( - nameof(FeaturesResources.Use_default_literal), FeaturesResources.ResourceManager, typeof(FeaturesResources))) + new LocalizableResourceString(nameof(FeaturesResources.Simplify_default_expression), FeaturesResources.ResourceManager, typeof(FeaturesResources)), + new LocalizableResourceString(nameof(FeaturesResources.default_expression_can_be_simplified), FeaturesResources.ResourceManager, typeof(FeaturesResources))) { } @@ -53,7 +51,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context) // Create a normal diagnostic that covers the entire default expression. context.ReportDiagnostic( Diagnostic.Create(GetDescriptorWithSeverity( - optionSet.GetOption(CSharpCodeStyleOptions.PreferDefaultLiteral).Notification.Value), + optionSet.GetOption(CSharpCodeStyleOptions.PreferSimpleDefaultExpression).Notification.Value), defaultExpression.GetLocation())); // Also fade out the part of the default expression from the open paren through diff --git a/src/Features/Core/Portable/FeaturesResources.Designer.cs b/src/Features/Core/Portable/FeaturesResources.Designer.cs index 5b136db7f1f..c85759f7a6e 100644 --- a/src/Features/Core/Portable/FeaturesResources.Designer.cs +++ b/src/Features/Core/Portable/FeaturesResources.Designer.cs @@ -863,6 +863,15 @@ internal class FeaturesResources { } } + /// + /// Looks up a localized string similar to 'default' expression can be simplified. + /// + internal static string default_expression_can_be_simplified { + get { + return ResourceManager.GetString("default_expression_can_be_simplified", resourceCulture); + } + } + /// /// Looks up a localized string similar to delegate. /// @@ -2664,6 +2673,15 @@ internal class FeaturesResources { } } + /// + /// Looks up a localized string similar to Simplify 'default' expression. + /// + internal static string Simplify_default_expression { + get { + return ResourceManager.GetString("Simplify_default_expression", resourceCulture); + } + } + /// /// Looks up a localized string similar to Simplify Member Access. /// @@ -3280,15 +3298,6 @@ internal class FeaturesResources { } } - /// - /// Looks up a localized string similar to Use 'default' literal. - /// - internal static string Use_default_literal { - get { - return ResourceManager.GetString("Use_default_literal", resourceCulture); - } - } - /// /// Looks up a localized string similar to Use explicitly provided tuple name. /// diff --git a/src/Features/Core/Portable/FeaturesResources.resx b/src/Features/Core/Portable/FeaturesResources.resx index 0498a68122e..e68790cec07 100644 --- a/src/Features/Core/Portable/FeaturesResources.resx +++ b/src/Features/Core/Portable/FeaturesResources.resx @@ -1241,7 +1241,10 @@ This version used in: {2} Implement {0} - - Use 'default' literal + + Simplify 'default' expression + + + 'default' expression can be simplified \ No newline at end of file diff --git a/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs b/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs index f36f1a86e79..de58425e561 100644 --- a/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs +++ b/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs @@ -427,7 +427,7 @@ public Customer() }} "; - private static readonly string s_preferDefaultLiteral = $@" + private static readonly string s_preferSimpleDefaultExpression = $@" using System.Threading; class Customer @@ -708,7 +708,7 @@ internal StyleViewModel(OptionSet optionSet, IServiceProvider serviceProvider) : CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferPatternMatchingOverIsWithCastCheck, CSharpVSResources.Prefer_pattern_matching_over_is_with_cast_check, s_preferPatternMatchingOverIsWithCastCheck, s_preferPatternMatchingOverIsWithCastCheck, this, optionSet, expressionPreferencesGroupTitle)); CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferPatternMatchingOverAsWithNullCheck, CSharpVSResources.Prefer_pattern_matching_over_as_with_null_check, s_preferPatternMatchingOverAsWithNullCheck, s_preferPatternMatchingOverAsWithNullCheck, this, optionSet, expressionPreferencesGroupTitle)); CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CodeStyleOptions.PreferExplicitTupleNames, ServicesVSResources.Prefer_explicit_tuple_name, s_preferExplicitTupleName, s_preferExplicitTupleName, this, optionSet, expressionPreferencesGroupTitle)); - CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferDefaultLiteral, ServicesVSResources.Prefer_default_literal, s_preferDefaultLiteral, s_preferDefaultLiteral, this, optionSet, expressionPreferencesGroupTitle)); + CodeStyleItems.Add(new BooleanCodeStyleOptionViewModel(CSharpCodeStyleOptions.PreferSimpleDefaultExpression, ServicesVSResources.Prefer_simple_default_expression, s_preferSimpleDefaultExpression, s_preferSimpleDefaultExpression, this, optionSet, expressionPreferencesGroupTitle)); AddExpressionBodyOptions(optionSet, expressionPreferencesGroupTitle); diff --git a/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs b/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs index e5b777cdf34..e42e9d25e74 100644 --- a/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs +++ b/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs @@ -1502,15 +1502,6 @@ internal class ServicesVSResources { } } - /// - /// Looks up a localized string similar to Prefer 'default' literal. - /// - internal static string Prefer_default_literal { - get { - return ResourceManager.GetString("Prefer_default_literal", resourceCulture); - } - } - /// /// Looks up a localized string similar to Prefer explicit tuple name. /// @@ -1565,6 +1556,15 @@ internal class ServicesVSResources { } } + /// + /// Looks up a localized string similar to Prefer simple 'default' expression. + /// + internal static string Prefer_simple_default_expression { + get { + return ResourceManager.GetString("Prefer_simple_default_expression", resourceCulture); + } + } + /// /// Looks up a localized string similar to prefer throwing properties. /// diff --git a/src/VisualStudio/Core/Def/ServicesVSResources.resx b/src/VisualStudio/Core/Def/ServicesVSResources.resx index 0f2b08b225f..e0a430ab553 100644 --- a/src/VisualStudio/Core/Def/ServicesVSResources.resx +++ b/src/VisualStudio/Core/Def/ServicesVSResources.resx @@ -912,7 +912,7 @@ Additional information: {1} Options - - Prefer 'default' literal + + Prefer simple 'default' expression \ No newline at end of file diff --git a/src/Workspaces/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs b/src/Workspaces/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs index 3348b55b2df..4ff203410f6 100644 --- a/src/Workspaces/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs +++ b/src/Workspaces/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs @@ -134,11 +134,11 @@ internal static class CSharpCodeStyleOptions EditorConfigStorageLocation.ForBoolCodeStyleOption("csharp_prefer_braces"), new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{nameof(PreferBraces)}")}); - public static readonly Option> PreferDefaultLiteral = new Option>( - nameof(CodeStyleOptions), nameof(PreferDefaultLiteral), defaultValue: CodeStyleOptions.TrueWithSuggestionEnforcement, + public static readonly Option> PreferSimpleDefaultExpression = new Option>( + nameof(CodeStyleOptions), nameof(PreferSimpleDefaultExpression), defaultValue: CodeStyleOptions.TrueWithSuggestionEnforcement, storageLocations: new OptionStorageLocation[] { - EditorConfigStorageLocation.ForBoolCodeStyleOption("csharp_prefer_default_literal"), - new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{nameof(PreferDefaultLiteral)}")}); + EditorConfigStorageLocation.ForBoolCodeStyleOption("csharp_prefer_simple_default_expression"), + new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{nameof(PreferSimpleDefaultExpression)}")}); public static IEnumerable>> GetCodeStyleOptions() { @@ -149,7 +149,7 @@ public static IEnumerable>> GetCodeStyleOptions() yield return PreferPatternMatchingOverAsWithNullCheck; yield return PreferPatternMatchingOverIsWithCastCheck; yield return PreferBraces; - yield return PreferDefaultLiteral; + yield return PreferSimpleDefaultExpression; } public static IEnumerable>> GetExpressionBodyOptions() diff --git a/src/Workspaces/CSharp/Portable/Extensions/DefaultExpressionSyntaxExtensions.cs b/src/Workspaces/CSharp/Portable/Extensions/DefaultExpressionSyntaxExtensions.cs index 5b95f532ce0..45d867dbe75 100644 --- a/src/Workspaces/CSharp/Portable/Extensions/DefaultExpressionSyntaxExtensions.cs +++ b/src/Workspaces/CSharp/Portable/Extensions/DefaultExpressionSyntaxExtensions.cs @@ -21,7 +21,7 @@ internal static class DefaultExpressionSyntaxExtensions CancellationToken cancellationToken) { if (parseOptions.LanguageVersion < LanguageVersion.CSharp7_1 || - !options.GetOption(CSharpCodeStyleOptions.PreferDefaultLiteral).Value) + !options.GetOption(CSharpCodeStyleOptions.PreferSimpleDefaultExpression).Value) { return false; } -- GitLab