diff --git a/HotSettings/Constants.cs b/HotSettings/Constants.cs index 2d5d0cd..3e9e1e3 100644 --- a/HotSettings/Constants.cs +++ b/HotSettings/Constants.cs @@ -50,5 +50,13 @@ class Constants // Solution Explorer Commands public const int ToggleTrackActiveItemCmdId = 0x1210; + + // --- CollectionPath and Keys for the User Settings Store + // System properties + public const string SOLUTION_NAVIGATOR_GROUP = @"ApplicationPrivateSettings\SolutionNavigator"; + public const string TRACK_ACTIVE_ITEM_IN_SOLN_EXP = "TrackSelCtxInSlnExp"; + // User properties + public const string HOT_SETTINGS_GROUP = "HotSettings"; + public const string SHOW_LIGHTBLUB_MARGIN = "ShowLightbulbMargin"; } } diff --git a/HotSettings/HotSettingsCommandFilter.cs b/HotSettings/HotSettingsCommandFilter.cs index 1df5d43..5f7ef6b 100644 --- a/HotSettings/HotSettingsCommandFilter.cs +++ b/HotSettings/HotSettingsCommandFilter.cs @@ -6,6 +6,11 @@ using Microsoft.VisualStudio.TextManager.Interop; using System.Runtime.InteropServices; using System.Collections.Generic; +using Microsoft.VisualStudio.Settings; +using Microsoft.VisualStudio.Shell.Settings; +using static HotSettings.Constants; +using System.Collections; +using Microsoft.VisualStudio.Shell; namespace HotSettings { @@ -15,15 +20,17 @@ internal sealed class HotSettingsCommandFilter : IOleCommandTarget private Guid languageServiceGuid; private IVsTextManager6 TextManager; + private WritableSettingsStore UserSettingsStore; // Map private static Dictionary> RestoreSettingsMap = new Dictionary>(); - public HotSettingsCommandFilter(IWpfTextView textView, Guid languageServiceGuid, IVsTextManager6 textManager) + public HotSettingsCommandFilter(IWpfTextView textView, Guid languageServiceGuid, IVsTextManager6 textManager, WritableSettingsStore userSettingsStore) { this.textView = textView; this.languageServiceGuid = languageServiceGuid; - TextManager = textManager; + this.TextManager = textManager; + this.UserSettingsStore = userSettingsStore; } public IOleCommandTarget Next { get; internal set; } @@ -320,10 +327,23 @@ private void EnableTrackChanges() } } - private void ExecToggleLightbulbMargin(IWpfTextView textView) + public void ExecToggleLightbulbMargin(IWpfTextView textView) { bool enabled = (bool)textView.Options.GetOptionValue("TextViewHost/SuggestionMargin"); textView.Options.SetOptionValue("TextViewHost/SuggestionMargin", !enabled); + + // Make the setting sticky! + UpdateLightbulbMarginSetting(!enabled); + } + + private void UpdateLightbulbMarginSetting(bool isShowMargin) + { + const string collectionPath = HOT_SETTINGS_GROUP; + if (!UserSettingsStore.CollectionExists(collectionPath)) + { + UserSettingsStore.CreateCollection(collectionPath); + } + UserSettingsStore.SetBoolean(HOT_SETTINGS_GROUP, SHOW_LIGHTBLUB_MARGIN, isShowMargin); } public void ExecToggleSelectionMargin(IWpfTextView textView) diff --git a/HotSettings/HotSettingsCommandsTextViewCreationListener.cs b/HotSettings/HotSettingsCommandsTextViewCreationListener.cs index 3b8ad98..38c8950 100644 --- a/HotSettings/HotSettingsCommandsTextViewCreationListener.cs +++ b/HotSettings/HotSettingsCommandsTextViewCreationListener.cs @@ -8,6 +8,9 @@ using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Text.Operations; using System; +using Microsoft.VisualStudio.Settings; +using Microsoft.VisualStudio.Shell.Settings; +using static HotSettings.Constants; #pragma warning disable 0649 namespace HotSettings @@ -30,17 +33,29 @@ internal sealed class HotSettingsCommandsTextViewCreationListener : IVsTextViewC private IEditorOperationsFactoryService _editorOperationsFactory; private IVsTextManager6 TextManager; + IWpfTextView textView; + HotSettingsCommandFilter commandFilter; + + private ShellSettingsManager SettingsManager; + private WritableSettingsStore UserSettingsStore; + public void VsTextViewCreated(IVsTextView textViewAdapter) { - IWpfTextView textView = EditorAdaptersFactoryService.GetWpfTextView(textViewAdapter); + textView = EditorAdaptersFactoryService.GetWpfTextView(textViewAdapter); Guid langServiceGuid = GetLanguageServiceGuid(textView); TextManager = (IVsTextManager6)_globalServiceProvider.GetService(typeof(SVsTextManager)); - HotSettingsCommandFilter commandFilter = new HotSettingsCommandFilter(textView, langServiceGuid, TextManager); + SettingsManager = new ShellSettingsManager(_globalServiceProvider); + UserSettingsStore = SettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); + + commandFilter = new HotSettingsCommandFilter(textView, langServiceGuid, TextManager, UserSettingsStore); textViewAdapter.AddCommandFilter(commandFilter, out IOleCommandTarget next); + // Apply global settings to this editor window. ie. Sticky setting for Lightbulb margin + ApplyInitialEditorMarginSettings(); + commandFilter.Next = next; } @@ -55,5 +70,24 @@ private Guid GetLanguageServiceGuid(IWpfTextView textView) textBuffer.GetLanguageServiceID(out Guid langServiceGuid); return langServiceGuid; } + + private void ApplyInitialEditorMarginSettings() + { + ApplyLightbulbMarginSetting(); + } + + private void ApplyLightbulbMarginSetting() + { + // Get the user's current sticky lightbulb margin setting + // Note: First time fetch will be empty and should default to TRUE (ie. Show the lightbulb margin) + bool showLightbulbMargin = UserSettingsStore.GetBoolean(HOT_SETTINGS_GROUP, SHOW_LIGHTBLUB_MARGIN, true); + + // Turn off the lightbulb if user set it OFF. + // Note: Only worry about turning it OFF; it starts ON by default with a new editor. + if (!showLightbulbMargin) + { + textView.Options.SetOptionValue("TextViewHost/SuggestionMargin", false); + } + } } } diff --git a/HotSettings/ReleaseNotes.txt b/HotSettings/ReleaseNotes.txt index 8cc0eda..2659444 100644 --- a/HotSettings/ReleaseNotes.txt +++ b/HotSettings/ReleaseNotes.txt @@ -1,6 +1,7 @@ Hot Settings by Justin Clareburt -18-Jul-2021 v1.5.0: Upgraded for VS2021 +10-Jan-2023 v1.6.0: Lightbulb margin made sticky +18-Jul-2021 v1.5.0: Upgraded for VS2022 26-Dec-2020 v1.4.0: Removed feature: Error info on status bar (Extracted to Hot Status extension) 04-May-2020 v1.3.1: Fixed NullReferenceException when opening Diff window. 07-Jul-2019 v1.3.0: Upgraded to include VS2019 diff --git a/HotSettings/TrackActiveItemsCommandHandler.cs b/HotSettings/TrackActiveItemsCommandHandler.cs index 4d3a82e..9e66b2c 100644 --- a/HotSettings/TrackActiveItemsCommandHandler.cs +++ b/HotSettings/TrackActiveItemsCommandHandler.cs @@ -6,6 +6,8 @@ using System.ComponentModel.Design; using Microsoft.VisualStudio.TextManager.Interop; using Microsoft.VisualStudio.Shell.Interop; +using static HotSettings.Constants; + namespace HotSettings { @@ -18,20 +20,11 @@ internal sealed class TrackActiveItemsCommandHandler private readonly Guid HotSettingsPackageCmdSetGuid = Constants.HotSettingsCmdSetGuid; private const int ToggleTrackActiveItemCmdId = Constants.ToggleTrackActiveItemCmdId; - // UserSettingsStore properties - private const string SOLUTION_NAVIGATOR_GROUP = @"ApplicationPrivateSettings\SolutionNavigator"; - private const string TRACK_ACTIVE_ITEM_IN_SOLN_EXP = "TrackSelCtxInSlnExp"; - /// /// VS Package that provides this command, not null. /// private readonly Package package; - private SettingsStore SettingsStore; - private IEditorOptionsFactoryService OptionsService; - private IVsTextManager2 TextManager; - //private IVsEditorAdaptersFactoryService EditorAdaptersFactoryService; - /// /// Gets the instance of the command. /// @@ -80,12 +73,6 @@ private TrackActiveItemsCommandHandler(Package package) { this.package = package ?? throw new ArgumentNullException("package"); - ShellSettingsManager settingsManager = new ShellSettingsManager(package); - SettingsStore = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); - - OptionsService = ServicesUtil.GetMefService(this.ServiceProvider); - TextManager = (IVsTextManager2)ServiceProvider.GetService(typeof(SVsTextManager)); - RegisterGlobalCommands(); } diff --git a/HotSettings/source.extension.vsixmanifest b/HotSettings/source.extension.vsixmanifest index 8065a0a..d862999 100644 --- a/HotSettings/source.extension.vsixmanifest +++ b/HotSettings/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + Hot Settings Menus and Toolbars that expose Visual Studio settings https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotSettings