Skip to content

Commit

Permalink
Now forcing shortcut for IncreaseSelection (Ctrl+W) iff Ctrl+W curren…
Browse files Browse the repository at this point in the history
…tly boud to Edit.SelectCurrentWord.
  • Loading branch information
Justin Clareburt committed Oct 29, 2016
1 parent db96a40 commit 28faf19
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
25 changes: 17 additions & 8 deletions HotCommands/Commands/ExpandSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
// </copyright>
//------------------------------------------------------------------------------

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;

Expand All @@ -29,6 +21,23 @@ namespace HotCommands
/// </summary>
internal sealed class ExpandSelection : Command<ExpandSelection>
{
public new static void Initialize(Package package)
{
Command<ExpandSelection>.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)
Expand Down
3 changes: 1 addition & 2 deletions HotCommands/HotCommandsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using HotCommands.Commands;
Expand Down Expand Up @@ -68,7 +67,7 @@ protected override void Initialize()
FormatCode.Initialize(this);
DuplicateSelection.Initialize(this);
MoveMemberUp.Initialize(this);
MoveMemberDown.Initialize(this);
MoveMemberDown.Initialize(this);
}

#endregion
Expand Down
30 changes: 30 additions & 0 deletions HotCommands/KeyBindingUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.Shell;
using EnvDTE;

Expand Down Expand Up @@ -36,6 +37,35 @@ private IServiceProvider ServiceProvider
}
}

/// <summary>
/// Adds (appends) a binding for the given shortcut
/// </summary>
/// <param name="commandName"></param>
/// <param name="shortcutDef"></param>
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);
}

/// <summary>
/// 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.
Expand Down

0 comments on commit 28faf19

Please sign in to comment.