/// <summary>
        /// Initializes a new instance of the <see cref="VSShortcutsManager"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private VSShortcutsManager(Package package)
        {
            this.package = package ?? throw new ArgumentNullException("package");

            // Intialize the VSShortcutQueryEngine
            this.queryEngine = VSShortcutQueryEngine.GetInstance(ServiceProvider);

            // Register all the command handlers with the Global Command Service
            RegisterCommandHandlers();

            //ShellSettingsManager = new ShellSettingsManager(package);
            userShortcutsManager = UserShortcutsManager.Instance;

            //// Initialise path for AppDataRoaming and AppDataLocal (Optional - alternative method)
            //_RoamingAppDataVSPath = Path.Combine(ShellSettingsManager.GetApplicationDataFolder(ApplicationDataFolder.ApplicationExtensions), "Extensions");
            //_LocalUserExtensionsPath = Path.Combine(ShellSettingsManager.GetApplicationDataFolder(ApplicationDataFolder.LocalSettings), "Extensions");

            // Load user shortcut registries
            //userShortcutsManager.DeleteUserShortcutsDef("WindowHideShortcuts");
            //UserShortcutsRegistry = userShortcutsManager.GetUserShortcutsRegistry();
            // Load imported VSKs registry
            //VskImportsRegistry = userShortcutsManager.GetVskImportsRegistry();

            if (ShortcutsScanner.Instance.ExtensionsNeedRescan())
            {
                ShortcutsScanner.Instance.ScanForAllExtensionShortcuts();
            }
        }
        private List <VSShortcut> ParseVSSettingsFile(XDocument vsSettingsFile)
        {
            VSShortcutQueryEngine engine = new VSShortcutQueryEngine(ServiceProvider);
            var userShortcuts            = vsSettingsFile.Descendants("UserShortcuts");
            var shortcutList             = new List <VSShortcut>();

            foreach (var userShortcut in userShortcuts)
            {
                foreach (var shortcut in userShortcut.Descendants("Shortcut"))
                {
                    var scope         = engine.GetScopeByName(shortcut.Attribute("Scope").Value);
                    var sequences     = engine.GetBindingSequencesFromBindingString(shortcut.Value);
                    var conflictTexts = new List <string>();
                    ThreadHelper.JoinableTaskFactory.Run(async() =>
                    {
                        var conflicts = await engine.GetConflictsAsync(scope, sequences);
                        foreach (var conflict in conflicts)
                        {
                            foreach (var binding in conflict.AffectedBindings)
                            {
                                conflictTexts.Add($"[{binding.Item1.Scope.Name}] {binding.Item2.CanonicalName} ({binding.Item1.OriginalDTEString})");
                            }
                        }
                    });

                    shortcutList.Add(new VSShortcut {
                        Command = shortcut.Attribute("Command").Value, Scope = shortcut.Attribute("Scope").Value, Shortcut = shortcut.Value, Conflicts = conflictTexts
                    });
                }
            }
            return(shortcutList);
        }
 private void InitializeShortcutEngine(IServiceProvider serviceProvider)
 {
     if (ShortcutQueryEngine == null)
     {
         ShortcutQueryEngine = new VSShortcutQueryEngine(serviceProvider);
     }
 }
Example #4
0
        public CommandShortcutsControlDataContext(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;

            this.queryEngine = VSShortcutQueryEngine.GetInstance(this.serviceProvider);

            this.PopulateCommands();
        }
Example #5
0
 public static VSShortcutQueryEngine GetInstance(IServiceProvider serviceProvider)
 {
     if (instance == null)
     {
         instance = new VSShortcutQueryEngine(serviceProvider);
     }
     return(instance);
 }
Example #6
0
 private async void RefreshShortcuts()
 {
     var modifierKey = GetSelectedModifierKey();
     VSShortcutQueryEngine engine    = new VSShortcutQueryEngine(ServiceProvider);
     var             selectedscope   = (Scope)cmbScopeList.SelectedItem;
     Guid            scopeGuid       = Guid.Parse(selectedscope.ID); // Get Guid Scope
     BindingSequence bindingSequence = BindingSequence.Empty;        // TODO: This is if there is a Chord, otherwise BindingSequence.EMPTY
     const bool      includeGlobals  = true;
     IDictionary <string, IEnumerable <Tuple <CommandBinding, Command> > > bindingsMap = await engine.GetBindingsForModifiersAsync(scopeGuid, ModifierKeys.None, bindingSequence, includeGlobals);
 }
        private void PopulateCommands(IServiceProvider serviceProvider)
        {
            var queryEngine = new VSShortcutQueryEngine(serviceProvider);

            queryEngine.GetAllCommandsAsync().ContinueWith((task) =>
            {
                var allCommands = task.Result;

                var allCommandShortcuts = allCommands
                                          .Where(command => !string.IsNullOrWhiteSpace(command?.CanonicalName))
                                          .SelectMany(command => GenerateCommandsShortcuts(command));

                this.allCommands = new VSCommandShortcuts(allCommandShortcuts);
                this.Commands    = this.allCommands.Clone();
            });
        }
        public IDictionary <string, IEnumerable <Tuple <CommandBinding, Command> > > GetBindingsForModifiers(Guid scope, ModifierKeys modifiers, BindingSequence chordStart, bool includeGlobals)
        {
            IEnumerable <Command> allCommands = AllCommands;

            var bindingMap = new Dictionary <string, IEnumerable <Tuple <CommandBinding, Command> > >();

            foreach (Command command in allCommands)
            {
                // Each command can have zero-many bindings. We'll look at each binding to see if it should be returned (matching scope and modifier)
                foreach (CommandBinding binding in command.Bindings)
                {
                    // Ensure the binding is in the desired scope (or Global if includeGlobals is true)
                    if (!VSShortcutQueryEngine.ScopeMatches(scope, binding.Scope.Guid) && !(VSShortcutQueryEngine.ScopeIsGlobal(binding.Scope.Guid) && includeGlobals))
                    {
                        continue;
                    }

                    // If the user passed in a starting chord (chordStart is not empty), only return this binding if it is a chord and starts with the chordStart
                    if (chordStart != BindingSequence.Empty &&
                        (binding.Sequences.Count < 2 || !VSShortcutQueryEngine.SameBindingSequence(binding.Sequences[0], chordStart)))
                    {
                        continue;
                    }

                    // Does the binding have the right modifiers? Two cases: chordStart is empty / chordStart is not empty
                    BindingSequence sequenceOfInterest = (chordStart != BindingSequence.Empty) ? binding.Sequences[1] : binding.Sequences[0];
                    if (sequenceOfInterest.Modifiers != modifiers)
                    {
                        continue;
                    }

                    // Found a command with matching modifiers for the given scope (with matching starting chord).
                    // Add it to the relevant entry in the dictionary.
                    VSShortcutQueryEngine.AddCommandBindingToBindingMap(bindingMap, command, binding, sequenceOfInterest.Key);
                }
            }

            return(bindingMap);
        }
        private async void RefreshShortcuts()
        {
            try
            {
                var modifierKeys                = GetSelectedModifierKeys();
                VSShortcutQueryEngine engine    = new VSShortcutQueryEngine(ServiceProvider);
                var             selectedscope   = (Scope)cmbScopeList.SelectedItem;
                Guid            scopeGuid       = (selectedscope != null) ? Guid.Parse(selectedscope.ID) : Guid.Empty;
                BindingSequence bindingSequence = BindingSequence.Empty; // TODO: This is if there is a Chord, otherwise BindingSequence.EMPTY
                const bool      includeGlobals  = true;
                IDictionary <string, IEnumerable <Tuple <CommandBinding, Command> > > matchingShortcuts = await engine.GetBindingsForModifiersAsync(scopeGuid, modifierKeys, bindingSequence, includeGlobals);

                foreach (var matchingShortcut in matchingShortcuts)
                {
                    var key             = matchingShortcut.Key;
                    var commandBindings = matchingShortcut.Value;
                    var commandString   = String.Join(",", commandBindings.Select(commandBinding => commandBinding.Item2.DisplayName));
                    UpdateShortcutValue(key, commandString);
                }
            }
            catch (Exception ex)
            {
            }
        }
        private async void RefreshShortcuts()
        {
            try
            {
                //Reset the old commands to blank before assigning new commands
                viewModel.ResetShortCutKeystoDefaultValue();

                var modifierKeys                = GetSelectedModifierKeys();
                VSShortcutQueryEngine engine    = new VSShortcutQueryEngine(ServiceProvider);
                var             selectedscope   = (Scope)cmbScopeList.SelectedItem;
                Guid            scopeGuid       = (selectedscope != null) ? Guid.Parse(selectedscope.ID) : Guid.Empty;
                BindingSequence bindingSequence = BindingSequence.Empty; // TODO: This is if there is a Chord, otherwise BindingSequence.EMPTY
                const bool      includeGlobals  = true;
                IDictionary <string, IEnumerable <Tuple <CommandBinding, Command> > > matchingShortcuts = await engine.GetBindingsForModifiersAsync(scopeGuid, modifierKeys, bindingSequence, includeGlobals);

                foreach (var matchingShortcut in matchingShortcuts)
                {
                    string key = matchingShortcut.Key;
                    IEnumerable <Tuple <CommandBinding, Command> > commandBindings = matchingShortcut.Value;
                    int?   sequenceCountofFirstCommandBinding = commandBindings.FirstOrDefault()?.Item1.Sequences.Count;
                    string commandString = "";
                    if (sequenceCountofFirstCommandBinding == 1)
                    {
                        commandString = commandBindings.FirstOrDefault().Item2.DisplayName;
                    }
                    else if (sequenceCountofFirstCommandBinding > 1)
                    {
                        commandString = "<Chord>";
                    }
                    UpdateShortcutValue(key, commandString);
                }
            }
            catch (Exception ex)
            {
            }
        }