Example #1
0
        protected override void Initialize()
        {
            base.Initialize();

            _SSMSHelper = new GenericVSHelper(true, null, null, null);

            // Add our command handlers for the menu commands defined in the in the .vsct file, and enable them
            if (GetService(typeof(IMenuCommandService)) is OleMenuCommandService mcs)
            {
                CommandID      menuCommandID;
                OleMenuCommand menuCommand;

                // Create the formatting command / menu item.
                menuCommandID = new CommandID(guidPoorMansTSqlFormatterSSMSPackageCmdSet, (int)cmdidPoorMansFormatSQL);
                menuCommand   = new OleMenuCommand(FormatSqlCallback, menuCommandID);
                mcs.AddCommand(menuCommand);
                menuCommand.BeforeQueryStatus += new EventHandler(QueryFormatButtonStatus);

                // Create the options command / menu item.
                menuCommandID       = new CommandID(guidPoorMansTSqlFormatterSSMSPackageCmdSet, (int)cmdidPoorMansSqlOptions);
                menuCommand         = new OleMenuCommand(SqlOptionsCallback, menuCommandID);
                menuCommand.Enabled = true;
                mcs.AddCommand(menuCommand);
            }
        }
        protected override async System.Threading.Tasks.Task InitializeAsync(System.Threading.CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await base.InitializeAsync(cancellationToken, progress);

            _SSMSHelper = new GenericVSHelper(true, null, null, null);

            //Switch to UI thread, so that we're allowed to get services
            await JoinableTaskFactory.SwitchToMainThreadAsync();

            // Add our command handlers for the menu commands defined in the in the .vsct file, and enable them
            if (await GetServiceAsync(typeof(IMenuCommandService)).ConfigureAwait(false) is OleMenuCommandService mcs)
            {
                CommandID      menuCommandID;
                OleMenuCommand menuCommand;

                // Create the formatting command / menu item.
                menuCommandID = new CommandID(guidPoorMansTSqlFormatterSSMSPackageCmdSet, (int)cmdidPoorMansFormatSQL);
                menuCommand   = new OleMenuCommand(FormatSqlCallback, menuCommandID);
                mcs.AddCommand(menuCommand);
                menuCommand.BeforeQueryStatus += new EventHandler(QueryFormatButtonStatus);

                // Create the options command / menu item.
                menuCommandID       = new CommandID(guidPoorMansTSqlFormatterSSMSPackageCmdSet, (int)cmdidPoorMansSqlOptions);
                menuCommand         = new OleMenuCommand(SqlOptionsCallback, menuCommandID);
                menuCommand.Enabled = true;
                mcs.AddCommand(menuCommand);
            }

            return;
        }
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _addInInstance     = (AddIn)addInInst;
            _applicationObject = (DTE2)_addInInstance.DTE;

            if (connectMode == ext_ConnectMode.ext_cm_UISetup || connectMode == ext_ConnectMode.ext_cm_Startup)
            {
                _SSMSHelper = new GenericVSHelper(_applicationObject.RegistryRoot.StartsWith(@"Software\Microsoft\VisualStudio"), new GenericVSHelper.GetTextEditorKeyBindingScopeName(GetTextEditorKeyBindingScopeName), new GenericVSHelper.GetKeyBinding(GetFormatCommandKeyBinding), new GenericVSHelper.SetKeyBinding(SetFormatCommandKeyBinding));

                object []       contextGUIDS = new object[] { };
                Commands2       commandsList = (Commands2)_applicationObject.Commands;
                CommandBarPopup targetPopup  = GetMainMenuPopup("Tools");

                //remove old commands
                List <string> oldCommandNames = new List <string>();
                oldCommandNames.Add("PoorMansTSqlFormatterSSMSAddIn.Connect.PoorMansTSqlFormatterSSMSAddIn");
                oldCommandNames.Add("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormatSelectionOrActiveWindow");
                oldCommandNames.Add("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormattingOptions");
                RemoveCommands(commandsList, oldCommandNames);

                //add new commands
                Command formatCommand = commandsList.AddNamedCommand2(
                    _addInInstance,
                    "FormatSelectionOrActiveWindow",
                    _SSMSHelper.GeneralResourceManager.GetString("FormatButtonText"),
                    _SSMSHelper.GeneralResourceManager.GetString("FormatButtonToolTip"),
                    true,
                    59,
                    ref contextGUIDS,
                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                    (int)vsCommandStyle.vsCommandStyleText,
                    vsCommandControlType.vsCommandControlTypeButton
                    );
                if ((formatCommand != null) && (targetPopup != null))
                {
                    formatCommand.AddControl(targetPopup.CommandBar, 1);
                }
                _formatCommand = formatCommand;
                _SSMSHelper.UpdateSettingsHotkeyIntoVS();

                Command optionsCommand = commandsList.AddNamedCommand2(
                    _addInInstance,
                    "FormattingOptions",
                    _SSMSHelper.GeneralResourceManager.GetString("OptionsButtonText"),
                    _SSMSHelper.GeneralResourceManager.GetString("OptionsButtonToolTip"),
                    true,
                    59,
                    ref contextGUIDS,
                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                    (int)vsCommandStyle.vsCommandStyleText,
                    vsCommandControlType.vsCommandControlTypeButton
                    );
                if ((optionsCommand != null) && (targetPopup != null))
                {
                    optionsCommand.AddControl(targetPopup.CommandBar, 2);
                }
            }
        }