diff --git a/HotCommands/Commands/ExpandSelection.cs b/HotCommands/Commands/ExpandSelection.cs index 5604ebd..96d7914 100644 --- a/HotCommands/Commands/ExpandSelection.cs +++ b/HotCommands/Commands/ExpandSelection.cs @@ -4,21 +4,13 @@ // //------------------------------------------------------------------------------ -using System; -using System.Globalization; using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio; using Microsoft.VisualStudio.Text.Editor; using Microsoft.CodeAnalysis.Text; using System.Linq; -using System.Collections; -using System.Windows; -using System.ComponentModel.Composition; using Microsoft.CodeAnalysis; using Microsoft.VisualStudio.Text; -using Microsoft.VisualStudio.Text.Classification; -using Microsoft.VisualStudio.Utilities; using System.Threading.Tasks; using System.Collections.Generic; @@ -29,6 +21,23 @@ namespace HotCommands /// internal sealed class ExpandSelection : Command { + public new static void Initialize(Package package) + { + Command.Initialize(package); + ForceKeyboardBindings(package); + } + + private static void ForceKeyboardBindings(Package package) + { + KeyBindingUtil.Initialize(package); + // We only want to force this keyboard binding if it is currently set to the default VS shortcut + // ie. Ctrl+W bound to Edit.SelectCurrentWord + if (KeyBindingUtil.BindingExists("Edit.SelectCurrentWord", "Text Editor::Ctrl+W")) + { + KeyBindingUtil.BindShortcut("Edit.IncreaseSelection", "Text Editor::Ctrl+W"); + } + } + public int HandleCommand(IWpfTextView textView, bool expand) { if (expand) diff --git a/HotCommands/HotCommandsPackage.cs b/HotCommands/HotCommandsPackage.cs index d40f44a..757ea18 100644 --- a/HotCommands/HotCommandsPackage.cs +++ b/HotCommands/HotCommandsPackage.cs @@ -4,7 +4,6 @@ // //------------------------------------------------------------------------------ -using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using HotCommands.Commands; @@ -68,7 +67,7 @@ protected override void Initialize() FormatCode.Initialize(this); DuplicateSelection.Initialize(this); MoveMemberUp.Initialize(this); - MoveMemberDown.Initialize(this); + MoveMemberDown.Initialize(this); } #endregion diff --git a/HotCommands/KeyBindingUtil.cs b/HotCommands/KeyBindingUtil.cs index 3b7fe6c..8c86792 100644 --- a/HotCommands/KeyBindingUtil.cs +++ b/HotCommands/KeyBindingUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Microsoft.VisualStudio.Shell; using EnvDTE; @@ -36,6 +37,35 @@ private IServiceProvider ServiceProvider } } + /// + /// Adds (appends) a binding for the given shortcut + /// + /// + /// + public static void BindShortcut(string commandName, string shortcutDef) + { + // Make sure we're not using the Default keyboard mapping scheme + DTE dte = (DTE)((IServiceProvider)package).GetService(typeof(DTE)); + EnvDTE.Commands cmds = dte.Commands; + Command cmd = cmds.Item(commandName); + object[] newBindings = AppendKeyboardBinding(cmd, shortcutDef); + cmd.Bindings = newBindings; + } + + internal static bool BindingExists(string commandName, string shortcutDef) + { + DTE dte = (DTE)((IServiceProvider)package).GetService(typeof(DTE)); + EnvDTE.Commands cmds = dte.Commands; + // Find command + Command cmd = cmds.Item(commandName); + if (cmd == null) return false; + // Check if the binding is attached to it + object[] existingBindings = (object[])cmd.Bindings; + if (existingBindings == null) return false; + // Check if the keyboard binding is already there + return existingBindings.Contains(shortcutDef); + } + /// /// Note: Calling this is redundant if the original KeyBinding works in the vsct file. /// However, sometimes there is another command bound to the desired keybinding.