Skip to content

Commit

Permalink
v1.6.0 - Sticky lightbulb (#19)
Browse files Browse the repository at this point in the history
Partial fix to issue #14

Most recent lightbulb setting now applies to all new editor windows, and all windows reopened upon restart.
  • Loading branch information
justcla authored Jan 10, 2023
1 parent d646d5e commit ebb2d42
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
8 changes: 8 additions & 0 deletions HotSettings/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
26 changes: 23 additions & 3 deletions HotSettings/HotSettingsCommandFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -15,15 +20,17 @@ internal sealed class HotSettingsCommandFilter : IOleCommandTarget
private Guid languageServiceGuid;

private IVsTextManager6 TextManager;
private WritableSettingsStore UserSettingsStore;

// Map<LanguageGuid, RestoreActions>
private static Dictionary<Guid, Dictionary<string, object>> RestoreSettingsMap = new Dictionary<Guid, Dictionary<string, object>>();

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; }
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 36 additions & 2 deletions HotSettings/HotSettingsCommandsTextViewCreationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
}
}
}
}
3 changes: 2 additions & 1 deletion HotSettings/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 2 additions & 15 deletions HotSettings/TrackActiveItemsCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.ComponentModel.Design;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using static HotSettings.Constants;


namespace HotSettings
{
Expand All @@ -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";

/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly Package package;

private SettingsStore SettingsStore;
private IEditorOptionsFactoryService OptionsService;
private IVsTextManager2 TextManager;
//private IVsEditorAdaptersFactoryService EditorAdaptersFactoryService;

/// <summary>
/// Gets the instance of the command.
/// </summary>
Expand Down Expand Up @@ -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<IEditorOptionsFactoryService>(this.ServiceProvider);
TextManager = (IVsTextManager2)ServiceProvider.GetService(typeof(SVsTextManager));

RegisterGlobalCommands();
}

Expand Down
2 changes: 1 addition & 1 deletion HotSettings/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="HotSettings.Justin Clareburt.9d7d4a40-2b7c-4177-82bf-1876951e6b24" Version="1.5.0" Language="en-US" Publisher="Justin Clareburt" />
<Identity Id="HotSettings.Justin Clareburt.9d7d4a40-2b7c-4177-82bf-1876951e6b24" Version="1.6.0" Language="en-US" Publisher="Justin Clareburt" />
<DisplayName>Hot Settings</DisplayName>
<Description xml:space="preserve">Menus and Toolbars that expose Visual Studio settings</Description>
<MoreInfo>https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotSettings</MoreInfo>
Expand Down

0 comments on commit ebb2d42

Please sign in to comment.