diff --git a/src/EditorFeatures/Core/Shared/Options/FeatureOnOffOptions.cs b/src/EditorFeatures/Core/Shared/Options/FeatureOnOffOptions.cs index 12149f1d4bf9fd36203c17d992c077d31f9489c7..93a82ad2d3779fd195609b3454ea12a6f8c433e6 100644 --- a/src/EditorFeatures/Core/Shared/Options/FeatureOnOffOptions.cs +++ b/src/EditorFeatures/Core/Shared/Options/FeatureOnOffOptions.cs @@ -87,9 +87,9 @@ internal static class FeatureOnOffOptions nameof(FeatureOnOffOptions), nameof(UseEnhancedColors), defaultValue: 1, storageLocations: new RoamingProfileStorageLocation("WindowManagement.Options.UseEnhancedColorsForManagedLanguages")); - public static readonly Option UseNullableReferenceTypes = new Option( - nameof(FeatureOnOffOptions), nameof(UseNullableReferenceTypes), defaultValue: 0, - storageLocations: new RoamingProfileStorageLocation($"TextEditor.CSharp.{nameof(UseNullableReferenceTypes)}")); + public static readonly Option UseNullableReferenceTypeAnalysis = new Option( + nameof(FeatureOnOffOptions), nameof(UseNullableReferenceTypeAnalysis), defaultValue: 0, + storageLocations: new RoamingProfileStorageLocation($"TextEditor.CSharp.{nameof(UseNullableReferenceTypeAnalysis)}")); // Note: no storage location since this is intentionally a session variable public static readonly Option AcceptedDecompilerDisclaimer = new Option( diff --git a/src/VisualStudio/CSharp/Impl/Options/AdvancedOptionPageControl.xaml b/src/VisualStudio/CSharp/Impl/Options/AdvancedOptionPageControl.xaml index 3942420491a62f2ecb4e18502ea11b47213506e8..99b584f7554b08a8050a22262352f378d25c151c 100644 --- a/src/VisualStudio/CSharp/Impl/Options/AdvancedOptionPageControl.xaml +++ b/src/VisualStudio/CSharp/Impl/Options/AdvancedOptionPageControl.xaml @@ -20,7 +20,7 @@ + Content="{x:Static local:AdvancedOptionPageStrings.Option_use_nullable_reference_type_analysis}" /> ServicesVSResources.Enable_navigation_to_decompiled_sources; - public static string Option_Enable_use_nullable_reference_types - => ServicesVSResources.Enable_use_nullable_reference_types; + public static string Option_use_nullable_reference_type_analysis + => ServicesVSResources.Enable_nullable_reference_type_analysis; public static string Option_RenameTrackingPreview => CSharpVSResources.Show_preview_for_rename_tracking; public static string Option_Split_string_literals_on_enter => CSharpVSResources.Split_string_literals_on_enter; diff --git a/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.OptionsProcessor.cs b/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.OptionsProcessor.cs index 49df3fcfc2e5e5cad0fa306503b0b8bdc5384112..e35f0c98b3fdea01a5abf3f16fe8cb663f89ecae 100644 --- a/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.OptionsProcessor.cs +++ b/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.OptionsProcessor.cs @@ -5,7 +5,9 @@ using System.IO; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Editor.Shared.Options; using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Options; using Microsoft.VisualStudio.LanguageServices.CSharp.ProjectSystemShim.Interop; using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem; using Roslyn.Utilities; @@ -21,11 +23,25 @@ private class OptionsProcessor : VisualStudioProjectOptionsProcessor private readonly object[] _options = new object[(int)CompilerOptions.LARGEST_OPTION_ID]; private string _mainTypeName; private OutputKind _outputKind; + private IOptionService _optionService; public OptionsProcessor(VisualStudioProject visualStudioProject, HostWorkspaceServices workspaceServices) : base(visualStudioProject, workspaceServices) { _visualStudioProject = visualStudioProject; + _optionService = workspaceServices.GetRequiredService(); + + // For C#, we need to listen to the options for NRT analysis + // that can change in VS through tools > options + _optionService.OptionChanged += OptionService_OptionChanged; + } + + private void OptionService_OptionChanged(object sender, OptionChangedEventArgs e) + { + if (e.Option.Feature == FeatureOnOffOptions.UseNullableReferenceTypeAnalysis.Feature) + { + UpdateProjectForNewHostValues(); + } } public object this[CompilerOptions compilerOption] @@ -216,6 +232,15 @@ private string GetStringOption(CompilerOptions optionID, string defaultValue) protected override ParseOptions ComputeParseOptionsWithHostValues(ParseOptions parseOptions) { + var useNullableReferenceAnalysisOption = _optionService.GetOption(FeatureOnOffOptions.UseNullableReferenceTypeAnalysis); + + parseOptions = useNullableReferenceAnalysisOption switch + { + -1 => parseOptions.WithFeatures(new[] { KeyValuePairUtil.Create("run-nullable-analysis", "false") }), + 1 => parseOptions.WithFeatures(new[] { KeyValuePairUtil.Create("run-nullable-analysis", "true") }), + _ => parseOptions + }; + var symbols = GetStringOption(CompilerOptions.OPTID_CCSYMBOLS, defaultValue: "").Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // The base implementation of OptionsProcessor already tried this, but it didn't have the real documentation diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioProjectOptionsProcessor.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioProjectOptionsProcessor.cs index 759af655e631f3bbb9487d368c29a8f7ca1564da..ea8a08b80278ea92e86904aebcf2144776b25f18 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioProjectOptionsProcessor.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioProjectOptionsProcessor.cs @@ -2,9 +2,7 @@ using System.Collections.Immutable; using System.IO; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Editor.Shared.Options; using Microsoft.CodeAnalysis.Host; -using Microsoft.CodeAnalysis.Options; using Roslyn.Utilities; namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem @@ -14,7 +12,6 @@ internal class VisualStudioProjectOptionsProcessor : IDisposable private readonly VisualStudioProject _project; private readonly HostWorkspaceServices _workspaceServices; private readonly ICommandLineParserService _commandLineParserService; - private IOptionService _optionService; /// /// Gate to guard all mutable fields in this class. @@ -31,27 +28,11 @@ public VisualStudioProjectOptionsProcessor(VisualStudioProject project, HostWork _project = project ?? throw new ArgumentNullException(nameof(project)); _workspaceServices = workspaceServices; _commandLineParserService = workspaceServices.GetLanguageServices(project.Language).GetRequiredService(); - _optionService = workspaceServices.GetRequiredService(); - - if (project.Language == LanguageNames.CSharp) - { - // For C#, we need to listen to the options for NRT analysis - // that can change in VS through tools > options - _optionService.OptionChanged += OptionService_OptionChanged; - } // Set up _commandLineArgumentsForCommandLine to a default. No lock taken since we're in the constructor so nothing can race. ReparseCommandLine_NoLock(); } - private void OptionService_OptionChanged(object sender, OptionChangedEventArgs e) - { - if (e.Option.Feature == FeatureOnOffOptions.UseNullableReferenceTypes.Feature) - { - UpdateProjectForNewHostValues(); - } - } - public string CommandLine { get @@ -178,18 +159,6 @@ private void UpdateProjectOptions_NoLock() compilationOptions = ComputeCompilationOptionsWithHostValues(compilationOptions, this._ruleSetFile?.Target.Value); parseOptions = ComputeParseOptionsWithHostValues(parseOptions); - if (_project.Language == LanguageNames.CSharp) - { - var useNullableReferenceTypesOption = _optionService.GetOption(FeatureOnOffOptions.UseNullableReferenceTypes); - - parseOptions = useNullableReferenceTypesOption switch - { - -1 => parseOptions.WithFeatures(new[] { KeyValuePairUtil.Create("run-nullable-analysis", "false") }), - 1 => parseOptions.WithFeatures(new[] { KeyValuePairUtil.Create("run-nullable-analysis", "true") }), - _ => parseOptions - }; - } - // For managed projects, AssemblyName has to be non-null, but the command line we get might be a partial command line // and not contain the existing value. Only update if we have one. _project.AssemblyName = _commandLineArgumentsForCommandLine.CompilationName ?? _project.AssemblyName; @@ -256,12 +225,6 @@ public void Dispose() lock (_gate) { DisposeOfRuleSetFile_NoLock(); - - if (_optionService != null) - { - _optionService.OptionChanged -= OptionService_OptionChanged; - _optionService = null; - } } } } diff --git a/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs b/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs index 5b2871e697811cc929f0107e9114e9a1b9e01837..dc75114017df59e6570ace06a5041b58a344c57c 100644 --- a/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs +++ b/src/VisualStudio/Core/Def/ServicesVSResources.Designer.cs @@ -906,11 +906,11 @@ internal class ServicesVSResources { } /// - /// Looks up a localized string similar to Enable use nullable reference types. + /// Looks up a localized string similar to Enable nullable reference type analysis. /// - internal static string Enable_use_nullable_reference_types { + internal static string Enable_nullable_reference_type_analysis { get { - return ResourceManager.GetString("Enable_use_nullable_reference_types", resourceCulture); + return ResourceManager.GetString("Enable_nullable_reference_type_analysis", resourceCulture); } } diff --git a/src/VisualStudio/Core/Def/ServicesVSResources.resx b/src/VisualStudio/Core/Def/ServicesVSResources.resx index c768c631a84ac1812e78e9ebb55b71e6c52ffe4d..b74760fa484fae617fbd7fa7f7c223f847146b07 100644 --- a/src/VisualStudio/Core/Def/ServicesVSResources.resx +++ b/src/VisualStudio/Core/Def/ServicesVSResources.resx @@ -1201,7 +1201,7 @@ I agree to all of the foregoing: Use enhanced colors for C# and Basic - - Enable use nullable reference types + + Enable nullable reference type analysis \ No newline at end of file diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf index 0b828edc6f73bd38cb6069defa1b3235d1a81682..680aac6807fa5aab7276a25f3db40ec4d4e29a31 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf @@ -72,9 +72,9 @@ Element není platný. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf index 011b624040cee272e3dbe02da5b21740d489afe0..354c50bef4315fe26da6fd020541ccd878a2a507 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf @@ -72,9 +72,9 @@ Das Element ist ungültig. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf index 9243aaacf341f1291f35ef54f6a1f01deaa7832b..8e1448d4877f3fa839f5987966133c16f1e3b69d 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf @@ -72,9 +72,9 @@ El elemento no es válido. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf index 01baba92d188b9b1ce9ca1b3684ad556869aeacc..48435653cf1721bd43bef909abfaa7db0c72a9e6 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf @@ -72,9 +72,9 @@ L'élément n'est pas valide. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf index b55857e106f1702312938ed3345977c63626d267..47f8703d027d4cbf8024e219bb3c5b441578acf4 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf @@ -72,9 +72,9 @@ L'elemento non è valido. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf index 35a55abe94d088a1ad035f64bf0ed87d82831ae7..6c5e1eb3e02e23860a43abe74a3a47c07da117e4 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf @@ -72,9 +72,9 @@ 要素が有効ではありません。 - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf index 93538150137c97dc0e0f59fb3b886e34e77cdd6c..9e1dc00c386ea9583488565c709530e021cf1448 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf @@ -72,9 +72,9 @@ 요소가 잘못되었습니다. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf index ddbbad4a94f137c2ab3a3ff29639daab4e60fa56..ac5bb681ea5ac7b91d86121d8dba29f27857372e 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf @@ -72,9 +72,9 @@ Element jest nieprawidłowy. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf index 0a21fca208f98424096930e87aeb36483a3ebfbe..19481fc3339361b52a138915114527cb4892836e 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf @@ -72,9 +72,9 @@ O elemento é inválido. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf index 870fe3a087c04d5fd81591f3beac15105160f710..29450030bbdc6eebaae0234600e25ca22e1fcb68 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf @@ -72,9 +72,9 @@ Элемент недопустим. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf index fc596679318e385cb79c3255afc4be890e738183..ae12ada99860b71f77606a3ee3a9c9c1786c7aec 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf @@ -72,9 +72,9 @@ Öğe geçerli değil. - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf index 5d0140a4bb637a04d3f29bcb6a5d10885d2bee50..19cbd22b9568e6df51d637983465bfa6d1fe1d47 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf @@ -72,9 +72,9 @@ 元素无效。 - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis diff --git a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf index 283755e41a161f76083344d0689dd1c275e4b5e1..1962f5d2bf8b12bdd981d2baf69117919c06f2a2 100644 --- a/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf +++ b/src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf @@ -72,9 +72,9 @@ 元素無效。 - - Enable use nullable reference types - Enable use nullable reference types + + Enable nullable reference type analysis + Enable nullable reference type analysis