Exemplo n.º 1
0
        /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
        /// <param term='commandName'>The name of the command to execute.</param>
        /// <param term='executeOption'>Describes how the command should be run.</param>
        /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
        /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
        /// <param term='handled'>Informs the caller if the command was handled or not.</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormatSelectionOrActiveWindow"))
                {
                    string fileExtension = System.IO.Path.GetExtension(_applicationObject.ActiveDocument.FullName);
                    bool   isSqlFile     = fileExtension.ToUpper().Equals(".SQL");

                    if (isSqlFile ||
                        MessageBox.Show(_generalResourceManager.GetString("FileTypeWarningMessage"), _generalResourceManager.GetString("FileTypeWarningMessageTitle"), MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        string        fullText  = SelectAllCodeFromDocument(_applicationObject.ActiveDocument);
                        TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
                        if (!selection.IsActiveEndGreater)
                        {
                            selection.SwapAnchor();
                        }
                        if (selection.Text.EndsWith(Environment.NewLine) || selection.Text.EndsWith(" "))
                        {
                            selection.CharLeft(true, 1); //newline counts as a distance of one.
                        }
                        string selectionText       = selection.Text;
                        bool   formatSelectionOnly = selectionText.Length > 0 && selectionText.Length != fullText.Length;


                        string textToFormat  = formatSelectionOnly ? selectionText : fullText;
                        bool   errorsFound   = false;
                        string formattedText = _formattingManager.Format(textToFormat, ref errorsFound);

                        bool abortFormatting = false;
                        if (errorsFound)
                        {
                            abortFormatting = MessageBox.Show(_generalResourceManager.GetString("ParseErrorWarningMessage"), _generalResourceManager.GetString("ParseErrorWarningMessageTitle"), MessageBoxButtons.YesNo) != DialogResult.Yes;
                        }

                        if (!abortFormatting)
                        {
                            if (formatSelectionOnly)
                            {
                                selection.Delete(1);
                                selection.Insert(formattedText, (int)EnvDTE.vsInsertFlags.vsInsertFlagsContainNewText);
                            }
                            else
                            {
                                ReplaceAllCodeInDocument(_applicationObject.ActiveDocument, formattedText);
                            }
                        }
                    }

                    handled = true;
                    return;
                }
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormattingOptions"))
                {
                    SettingsForm settings = new SettingsForm(Properties.Settings.Default, Assembly.GetExecutingAssembly(), _generalResourceManager.GetString("ProjectAboutDescription"), new SettingsForm.GetTextEditorKeyBindingScopeName(GetTextEditorKeyBindingScopeName));
                    if (settings.ShowDialog() == DialogResult.OK)
                    {
                        SetFormatHotkey();
                        _formattingManager = Utils.GetFormattingManager(Properties.Settings.Default);
                    }
                    settings.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
        /// <param term='commandName'>The name of the command to execute.</param>
        /// <param term='executeOption'>Describes how the command should be run.</param>
        /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
        /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
        /// <param term='handled'>Informs the caller if the command was handled or not.</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormatSelectionOrActiveWindow"))
                {
                    string fileExtension = System.IO.Path.GetExtension(_applicationObject.ActiveDocument.FullName);
                    bool   isSqlFile     = fileExtension.ToUpper().Equals(".SQL");

                    if (isSqlFile ||
                        MessageBox.Show(_generalResourceManager.GetString("FileTypeWarningMessage"), _generalResourceManager.GetString("FileTypeWarningMessageTitle"), MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        string        fullText  = SelectAllCodeFromDocument(_applicationObject.ActiveDocument);
                        TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
                        if (!selection.IsActiveEndGreater)
                        {
                            selection.SwapAnchor();
                        }
                        if (selection.Text.EndsWith(Environment.NewLine) || selection.Text.EndsWith(" "))
                        {
                            selection.CharLeft(true, 1); //newline counts as a distance of one.
                        }
                        string selectionText       = selection.Text;
                        bool   formatSelectionOnly = selectionText.Length > 0 && selectionText.Length != fullText.Length;


                        string textToFormat = formatSelectionOnly ? selectionText : fullText;
                        var    formatter    = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(
                            Properties.Settings.Default.IndentString.Replace("\\t", "\t"),
                            Properties.Settings.Default.SpacesPerTab,
                            Properties.Settings.Default.MaxLineWidth,
                            Properties.Settings.Default.ExpandCommaLists,
                            Properties.Settings.Default.TrailingCommas,
                            Properties.Settings.Default.SpaceAfterExpandedComma,
                            Properties.Settings.Default.ExpandBooleanExpressions,
                            Properties.Settings.Default.ExpandCaseStatements,
                            Properties.Settings.Default.ExpandBetweenConditions,
                            Properties.Settings.Default.BreakJoinOnSections,
                            Properties.Settings.Default.UppercaseKeywords,
                            false,
                            Properties.Settings.Default.KeywordStandardization
                            );
                        formatter.ErrorOutputPrefix = _generalResourceManager.GetString("ParseErrorWarningPrefix");
                        var    formattingManager = new PoorMansTSqlFormatterLib.SqlFormattingManager(formatter);
                        bool   errorsFound       = false;
                        string formattedText     = formattingManager.Format(textToFormat, ref errorsFound);

                        bool abortFormatting = false;
                        if (errorsFound)
                        {
                            abortFormatting = MessageBox.Show(_generalResourceManager.GetString("ParseErrorWarningMessage"), _generalResourceManager.GetString("ParseErrorWarningMessageTitle"), MessageBoxButtons.YesNo) != DialogResult.Yes;
                        }

                        if (!abortFormatting)
                        {
                            if (formatSelectionOnly)
                            {
                                selection.Delete(1);
                                selection.Insert(formattedText, (int)EnvDTE.vsInsertFlags.vsInsertFlagsContainNewText);
                            }
                            else
                            {
                                ReplaceAllCodeInDocument(_applicationObject.ActiveDocument, formattedText);
                            }
                        }
                    }

                    handled = true;
                    return;
                }
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormattingOptions"))
                {
                    SettingsForm settings = new SettingsForm();
                    if (settings.ShowDialog() == DialogResult.OK)
                    {
                        SetFormatHotkey();
                    }
                    settings.Dispose();
                }
            }
        }
        /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
        /// <param term='commandName'>The name of the command to execute.</param>
        /// <param term='executeOption'>Describes how the command should be run.</param>
        /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
        /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
        /// <param term='handled'>Informs the caller if the command was handled or not.</param>
        /// <seealso class='Exec' />
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormatSelectionOrActiveWindow"))
                {
                    string fileExtension = System.IO.Path.GetExtension(_applicationObject.ActiveDocument.FullName);
                    bool isSqlFile = fileExtension.ToUpper().Equals(".SQL");

                    if (isSqlFile ||
                        MessageBox.Show(_generalResourceManager.GetString("FileTypeWarningMessage"), _generalResourceManager.GetString("FileTypeWarningMessageTitle"), MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        string fullText = SelectAllCodeFromDocument(_applicationObject.ActiveDocument);
                        TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
                        if (!selection.IsActiveEndGreater)
                            selection.SwapAnchor();
                        if (selection.Text.EndsWith(Environment.NewLine) || selection.Text.EndsWith(" "))
                            selection.CharLeft(true, 1); //newline counts as a distance of one.
                        string selectionText = selection.Text;
                        bool formatSelectionOnly = selectionText.Length > 0 && selectionText.Length != fullText.Length;

                        string textToFormat = formatSelectionOnly ? selectionText : fullText;
                        var formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(
                            Properties.Settings.Default.IndentString.Replace("\\t", "\t"),
                            Properties.Settings.Default.SpacesPerTab,
                            Properties.Settings.Default.MaxLineWidth,
                            Properties.Settings.Default.ExpandCommaLists,
                            Properties.Settings.Default.TrailingCommas,
                            Properties.Settings.Default.SpaceAfterExpandedComma,
                            Properties.Settings.Default.ExpandBooleanExpressions,
                            Properties.Settings.Default.ExpandCaseStatements,
                            Properties.Settings.Default.ExpandBetweenConditions,
                            Properties.Settings.Default.BreakJoinOnSections,
                            Properties.Settings.Default.UppercaseKeywords,
                            false,
                            Properties.Settings.Default.KeywordStandardization
                            );
                        formatter.ErrorOutputPrefix = _generalResourceManager.GetString("ParseErrorWarningPrefix");
                        var formattingManager = new PoorMansTSqlFormatterLib.SqlFormattingManager(formatter);
                        bool errorsFound = false;
                        string formattedText = formattingManager.Format(textToFormat, ref errorsFound);

                        bool abortFormatting = false;
                        if (errorsFound)
                            abortFormatting = MessageBox.Show(_generalResourceManager.GetString("ParseErrorWarningMessage"), _generalResourceManager.GetString("ParseErrorWarningMessageTitle"), MessageBoxButtons.YesNo) != DialogResult.Yes;

                        if (!abortFormatting)
                        {
                            if (formatSelectionOnly)
                            {
                                selection.Delete(1);
                                selection.Insert(formattedText, (int)EnvDTE.vsInsertFlags.vsInsertFlagsContainNewText);
                            }
                            else
                                ReplaceAllCodeInDocument(_applicationObject.ActiveDocument, formattedText);
                        }
                    }

                    handled = true;
                    return;
                }
                if (commandName.Equals("PoorMansTSqlFormatterSSMSAddIn.AddinConnector.FormattingOptions"))
                {
                    SettingsForm settings = new SettingsForm();
                    if (settings.ShowDialog() == DialogResult.OK)
                    {
                        SetFormatHotkey();
                    }
                    settings.Dispose();
                }
            }
        }