コード例 #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));

            if (dte.ActiveDocument is null)
            {
                ShowMessage("Oops!", "Please select some text in active document.");
                return;
            }

            Indenter columnIndenter;
            string   fileExtension = Path.GetExtension(dte.ActiveDocument.FullName).ToLower().Remove(0, 1);

            switch (fileExtension)
            {
            case "cs":
                columnIndenter = new Indenter(new CSharpLanguage());
                break;

            default:
                columnIndenter = new Indenter(new BaseLanguage());
                break;
            }

            var textDocument = dte.ActiveDocument.Object() as TextDocument;

            if (textDocument is null || textDocument.Selection is null || string.IsNullOrWhiteSpace(textDocument.Selection.Text))
            {
                ShowMessage("Oops!", "Please select some text in active document.");
                return;
            }
            var selectedText = textDocument.Selection.Text;
            var editPoint    = textDocument.CreateEditPoint(textDocument.Selection.TopPoint);

            editPoint.ReplaceText(textDocument.Selection.BottomPoint, columnIndenter.Apply(selectedText), 0);
        }