Inheritance: MonoDevelop.Components.Commands.CommandHandler
        protected override void Update(CommandInfo info)
        {
            Document doc;
            var      formatter = FormatBufferHandler.GetFormatter(out doc);

            info.Enabled = formatter != null && !formatter.IsDefault;
        }
        protected override void Run(object tool)
        {
            Document doc;
            var      formatter = FormatBufferHandler.GetFormatter(out doc);

            if (formatter == null)
            {
                return;
            }

            TextSegment selection;
            var         editor = doc.Editor;

            if (editor.IsSomethingSelected)
            {
                selection = editor.SelectionRange;
            }
            else
            {
                selection = editor.GetLine(editor.Caret.Line).Segment;
            }

            using (var undo = editor.OpenUndoGroup()) {
                var version = editor.Version;

                if (formatter.SupportsOnTheFlyFormatting)
                {
                    formatter.OnTheFlyFormat(doc, selection.Offset, selection.EndOffset);
                }
                else
                {
                    var pol = doc.Project != null ? doc.Project.Policies : null;
                    try {
                        var    editorText = editor.Text;
                        string text       = formatter.FormatText(pol, editorText, selection.Offset, selection.EndOffset);
                        if (text != null && editorText.Substring(selection.Offset, selection.Length) != text)
                        {
                            editor.Replace(selection.Offset, selection.Length, text);
                        }
                    } catch (Exception e) {
                        LoggingService.LogError("Error during format.", e);
                    }
                }

                if (editor.IsSomethingSelected)
                {
                    int newOffset    = version.MoveOffsetTo(editor.Version, selection.Offset);
                    int newEndOffset = version.MoveOffsetTo(editor.Version, selection.EndOffset);
                    editor.SetSelection(newOffset, newEndOffset);
                }
            }
        }