diff --git a/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs b/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs index bfab6ec680dcef1a05c9af2ed56744c0f4bbce54..689c5a288313d2126bd2a040215c72fba4c2f7e2 100644 --- a/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs +++ b/src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs @@ -907,6 +907,42 @@ internal class CSharpFeaturesResources { } } + /// + /// Looks up a localized string similar to Use explicit typing instead of 'var'. + /// + internal static string UseExplicitTyping { + get { + return ResourceManager.GetString("UseExplicitTyping", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Use explicit typing. + /// + internal static string UseExplicitTypingDiagnosticTitle { + get { + return ResourceManager.GetString("UseExplicitTypingDiagnosticTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to use 'var' instead of typename. + /// + internal static string UseImplicitTyping { + get { + return ResourceManager.GetString("UseImplicitTyping", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Use implicit typing. + /// + internal static string UseImplicitTypingDiagnosticTitle { + get { + return ResourceManager.GetString("UseImplicitTypingDiagnosticTitle", resourceCulture); + } + } + /// /// Looks up a localized string similar to using directive. /// diff --git a/src/Features/CSharp/Portable/CSharpFeaturesResources.resx b/src/Features/CSharp/Portable/CSharpFeaturesResources.resx index d62911f44593d8b0829128410a03a93a6dbd7d9c..aa9a0c5704585286d29ae2f09c627f7a67f74d03 100644 --- a/src/Features/CSharp/Portable/CSharpFeaturesResources.resx +++ b/src/Features/CSharp/Portable/CSharpFeaturesResources.resx @@ -437,4 +437,16 @@ Delegate invocation can be simplified. + + Use explicit typing instead of 'var' + + + Use explicit typing + + + use 'var' instead of typename + + + Use implicit typing + \ No newline at end of file diff --git a/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseExplicitTypingCodeFixProvider.cs b/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseExplicitTypingCodeFixProvider.cs index add06154a2abfdb00c433791b644c4693c7e08b9..75108ff76ecc3316b67f1e691c70e9cef394043c 100644 --- a/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseExplicitTypingCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseExplicitTypingCodeFixProvider.cs @@ -46,7 +46,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) } var codeAction = new MyCodeAction( - "Use typename instead of var", // TODO: use localizable strings. + CSharpFeaturesResources.UseExplicitTyping, c => HandleDeclaration(document, root, node, context.CancellationToken)); context.RegisterCodeFix(codeAction, context.Diagnostics.First()); diff --git a/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseImplicitTypingCodeFixProvider.cs b/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseImplicitTypingCodeFixProvider.cs index 23a3d79edcedea1b11e055005dab0882323a13d3..60e108a5eb35bd208883a187ed350e98601c8fae 100644 --- a/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseImplicitTypingCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/CodeFixes/UseImplicitTyping/UseImplicitTypingCodeFixProvider.cs @@ -44,7 +44,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) } var codeAction = new MyCodeAction( - "Use var", // TODO: use localizable strings. + CSharpFeaturesResources.UseImplicitTyping, c => ReplaceTypeWithVar(context, document, root, node)); context.RegisterCodeFix(codeAction, context.Diagnostics.First()); diff --git a/src/Features/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs b/src/Features/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs index 4017beabc4b4d17a285d9a6177374239c3e64ce0..885e5503bbd4ad1793561d50915b64e913424f28 100644 --- a/src/Features/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs +++ b/src/Features/CSharp/Portable/CodeStyle/CSharpCodeStyleOptions.cs @@ -13,7 +13,7 @@ internal static class CSharpCodeStyleOptions { internal const string FeatureName = "CSharpCodeStyle"; - // TODO (BalajiK): repurpose this instead of adding a new one below? + // TODO: get sign off on public api changes. public static readonly Option UseVarWhenDeclaringLocals = new Option(FeatureName, "UseVarWhenDeclaringLocals", defaultValue: true); public static readonly Option UseVarForIntrinsicTypes = new Option(FeatureName, "UseImplicitTypingForIntrinsics", defaultValue: false); diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypingStyleDiagnosticAnalyzerBase.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypingStyleDiagnosticAnalyzerBase.cs index f96117c19926984794acda8419ee6b756ce485fe..c6e3183b84589476e63c3ef0b8aaad9fd5d6953c 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypingStyleDiagnosticAnalyzerBase.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpTypingStyleDiagnosticAnalyzerBase.cs @@ -43,9 +43,6 @@ public CSharpTypingStyleDiagnosticAnalyzerBase(DiagnosticDescriptor descriptor) public override void Initialize(AnalysisContext context) { - // TODO: check for generatedcode and bail. - // context.ConfigureGeneratedCodeAnalysis() See https://github.com/dotnet/roslyn/pull/7526 - context.RegisterSyntaxNodeAction(HandleVariableDeclaration, SyntaxKind.VariableDeclaration, SyntaxKind.ForEachStatement); } diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypingDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypingDiagnosticAnalyzer.cs index 79f3327638942c5ce6f5bd1efab91c107879d2dd..3eb450b5af6b0c01df23d4460cb12bcd0eeacae7 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypingDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseExplicitTypingDiagnosticAnalyzer.cs @@ -14,18 +14,19 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypingStyles [DiagnosticAnalyzer(LanguageNames.CSharp)] internal sealed class CSharpUseExplicitTypingDiagnosticAnalyzer : CSharpTypingStyleDiagnosticAnalyzerBase { - // TODO: - // 1. localize title and message - // 2. tweak severity and custom tags - // a. need to have various levels of diagnostics to report based on option settings. + private static readonly LocalizableString s_Title = + new LocalizableResourceString(nameof(CSharpFeaturesResources.UseExplicitTypingDiagnosticTitle), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + + private static readonly LocalizableString s_Message = + new LocalizableResourceString(nameof(CSharpFeaturesResources.UseExplicitTyping), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + private static readonly DiagnosticDescriptor s_descriptorUseImplicitTyping = new DiagnosticDescriptor( id: IDEDiagnosticIds.UseExplicitTypingDiagnosticId, - title: "Use explicit typing", - messageFormat: "Use type name instead of var", + title: s_Title, + messageFormat: s_Message, category: DiagnosticCategory.Style, defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - customTags: DiagnosticCustomTags.Unnecessary); + isEnabledByDefault: true); public CSharpUseExplicitTypingDiagnosticAnalyzer() : base(s_descriptorUseImplicitTyping) { diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypingDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypingDiagnosticAnalyzer.cs index 5c0833218f6d3120cd6642056d6f925d5048a4f7..f3d94046ff27d2e69c7f7b5674a9f8b21bb53a25 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypingDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypingDiagnosticAnalyzer.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.CodeStyle; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; @@ -16,18 +15,20 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypingStyles [DiagnosticAnalyzer(LanguageNames.CSharp)] internal sealed class CSharpUseImplicitTypingDiagnosticAnalyzer : CSharpTypingStyleDiagnosticAnalyzerBase { - // TODO: - // 1. localize title and message - // 2. tweak severity and custom tags - // a. need to have various levels of diagnostics to report based on option settings. + + private static readonly LocalizableString s_Title = + new LocalizableResourceString(nameof(CSharpFeaturesResources.UseImplicitTypingDiagnosticTitle), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + + private static readonly LocalizableString s_Message = + new LocalizableResourceString(nameof(CSharpFeaturesResources.UseImplicitTyping), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + private static readonly DiagnosticDescriptor s_descriptorUseImplicitTyping = new DiagnosticDescriptor( id: IDEDiagnosticIds.UseImplicitTypingDiagnosticId, - title: "Use implicit typing", - messageFormat: "Use var instead of explicit type name", + title: s_Title, + messageFormat: s_Message, category: DiagnosticCategory.Style, defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - customTags: DiagnosticCustomTags.Unnecessary); + isEnabledByDefault: true); public CSharpUseImplicitTypingDiagnosticAnalyzer() : base(s_descriptorUseImplicitTyping) { diff --git a/src/VisualStudio/CSharp/Impl/CSharpVSResources.Designer.cs b/src/VisualStudio/CSharp/Impl/CSharpVSResources.Designer.cs index 114fcf28a9130a03091ba3e3996d88f0f7c313ed..6d24ff2fd6b63b2d7eeac2493901ea3447d85761 100644 --- a/src/VisualStudio/CSharp/Impl/CSharpVSResources.Designer.cs +++ b/src/VisualStudio/CSharp/Impl/CSharpVSResources.Designer.cs @@ -699,6 +699,15 @@ internal class CSharpVSResources { } } + /// + /// Looks up a localized string similar to Type Inference preferences:. + /// + internal static string SetTypeInferencePreferences { + get { + return ResourceManager.GetString("SetTypeInferencePreferences", resourceCulture); + } + } + /// /// Looks up a localized string similar to Insert space after cast. /// @@ -915,6 +924,15 @@ internal class CSharpVSResources { } } + /// + /// Looks up a localized string similar to Use 'var' for intrinsic types. + /// + internal static string UseVarForIntrinsicTypes { + get { + return ResourceManager.GetString("UseVarForIntrinsicTypes", resourceCulture); + } + } + /// /// Looks up a localized string similar to Use 'var' when generating locals. /// @@ -924,6 +942,24 @@ internal class CSharpVSResources { } } + /// + /// Looks up a localized string similar to Use 'var' in all other places as well. + /// + internal static string UseVarWhenPossible { + get { + return ResourceManager.GetString("UseVarWhenPossible", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Use 'var' when type information is apparent. + /// + internal static string UseVarWhenTypeIsApparent { + get { + return ResourceManager.GetString("UseVarWhenTypeIsApparent", resourceCulture); + } + } + /// /// Looks up a localized string similar to Leave statements and member declarations on the same line. /// diff --git a/src/VisualStudio/CSharp/Impl/CSharpVSResources.resx b/src/VisualStudio/CSharp/Impl/CSharpVSResources.resx index ff977606cf486d3d50f61d7ac4576e58a3a90348..54095d4418835cab42e09f26a18a8afb38ba8c53 100644 --- a/src/VisualStudio/CSharp/Impl/CSharpVSResources.resx +++ b/src/VisualStudio/CSharp/Impl/CSharpVSResources.resx @@ -411,4 +411,16 @@ Place open brace on new line for properties, indexers, and events + + Type Inference preferences: + + + Use 'var' for intrinsic types + + + Use 'var' in all other places as well + + + Use 'var' when type information is apparent + \ 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 037cf6d945d8cd23b970285e2aca968a64e9f4bb..765737dd637032ba9dccea84e2d8a61686404587 100644 --- a/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs +++ b/src/VisualStudio/CSharp/Impl/Options/Formatting/StyleViewModel.cs @@ -179,12 +179,11 @@ internal StyleViewModel(OptionSet optionSet, IServiceProvider serviceProvider) : Items.Add(new CheckBoxOptionViewModel(SimplificationOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, CSharpVSResources.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, s_intrinsicPreviewMemberAccessTrue, s_intrinsicPreviewMemberAccessFalse, this, optionSet)); Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWhenDeclaringLocals, CSharpVSResources.UseVarWhenGeneratingLocals, s_varPreviewTrue, s_varPreviewFalse, this, optionSet)); - // TODO (BalajiK): Localize all strings from here. - Items.Add(new HeaderItemViewModel() { Header = "Type Inference preferences:" }); + Items.Add(new HeaderItemViewModel() { Header = CSharpVSResources.SetTypeInferencePreferences }); - Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarForIntrinsicTypes, "use var for intrinsic types", s_varForIntrinsicsPreviewTrue, s_varForIntrinsicsPreviewFalse, this, optionSet)); - Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWhenTypeIsApparent, "use var where typing is apparent", s_varWhereApparentPreviewTrue, s_varWhereApparentPreviewFalse, this, optionSet)); - Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWherePossible, "use var where possible", s_varWherePossiblePreviewTrue, s_varWherePossiblePreviewFalse, this, optionSet)); + Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarForIntrinsicTypes, CSharpVSResources.UseVarForIntrinsicTypes, s_varForIntrinsicsPreviewTrue, s_varForIntrinsicsPreviewFalse, this, optionSet)); + Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWhenTypeIsApparent, CSharpVSResources.UseVarWhenTypeIsApparent, s_varWhereApparentPreviewTrue, s_varWhereApparentPreviewFalse, this, optionSet)); + Items.Add(new CheckBoxOptionViewModel(CSharpCodeStyleOptions.UseVarWherePossible, CSharpVSResources.UseVarWhenPossible, s_varWherePossiblePreviewTrue, s_varWherePossiblePreviewFalse, this, optionSet)); } } }