Example #1
0
        protected override void Run(object tool)
        {
            Document doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null)
            {
                return;
            }
            string mt        = DesktopService.GetMimeTypeForUri(doc.FileName);
            var    formatter = CodeFormatterService.GetFormatter(mt);

            if (formatter == null)
            {
                return;
            }
            using (var undo = doc.Editor.OpenUndoGroup()) {
                var loc  = doc.Editor.Caret.Location;
                var text = formatter.FormatText(doc.Project != null ? doc.Project.Policies : null, doc.Editor.Text);
                if (text != null)
                {
                    doc.Editor.Replace(0, doc.Editor.Length, text);
                    doc.Editor.Caret.Location = loc;
                }
            }
        }
Example #2
0
        protected override void Run(object tool)
        {
            Document doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null)
            {
                return;
            }
            string mt        = DesktopService.GetMimeTypeForUri(doc.FileName);
            var    formatter = CodeFormatterService.GetFormatter(mt);

            if (formatter == null || !doc.Editor.IsSomethingSelected)
            {
                return;
            }
            var selection = doc.Editor.SelectionRange;

            using (var undo = doc.Editor.OpenUndoGroup()) {
                if (formatter.SupportsOnTheFlyFormatting)
                {
                    formatter.OnTheFlyFormat(doc, selection.Offset, selection.EndOffset);
                }
                else
                {
                    var    pol  = doc.Project != null ? doc.Project.Policies : null;
                    string text = formatter.FormatText(pol, doc.Editor.Text, selection.Offset, selection.EndOffset);
                    if (text != null)
                    {
                        doc.Editor.Replace(selection.Offset, selection.Length, text);
                        doc.Editor.SetSelection(selection.Offset, selection.Offset + text.Length - 1);
                    }
                }
            }
        }
        protected override void Run(object tool)
        {
            Document doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null)
            {
                return;
            }
            string mt        = DesktopService.GetMimeTypeForUri(doc.FileName);
            var    formatter = CodeFormatterService.GetFormatter(mt);

            if (formatter == null)
            {
                return;
            }
            if (formatter.SupportsOnTheFlyFormatting)
            {
                using (var undo = doc.Editor.OpenUndoGroup()) {
                    formatter.OnTheFlyFormat(doc, 0, doc.Editor.Length);
                }
            }
            else
            {
                doc.Editor.Text = formatter.FormatText(doc.Project.Policies, doc.Editor.Text);
            }
        }
        protected override void Run(object tool)
        {
            Document doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null)
            {
                return;
            }
            string mt        = DesktopService.GetMimeTypeForUri(doc.FileName);
            var    formatter = CodeFormatterService.GetFormatter(mt);

            if (formatter == null)
            {
                return;
            }
            Mono.TextEditor.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 {
                        string text = formatter.FormatText(pol, editor.Text, selection.Offset, selection.EndOffset);
                        if (text != null)
                        {
                            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);
                }
            }
        }
		internal static CodeFormatter GetFormatter (out Document doc)
		{
			doc = IdeApp.Workbench.ActiveDocument;
			if (doc == null)
				return null;
			var editor = doc.Editor;
			if (editor == null)
				return null;
			return editor == null ? null : CodeFormatterService.GetFormatter (editor.MimeType);
		}
Example #6
0
 protected override void Update(CommandInfo info)
 {
     if (IdeApp.Workbench.ActiveDocument != null && IdeApp.Workbench.ActiveDocument.IsFile)
     {
         string mt        = DesktopService.GetMimeTypeForUri(IdeApp.Workbench.ActiveDocument.FileName);
         var    formatter = CodeFormatterService.GetFormatter(mt);
         if (formatter != null)
         {
             return;
         }
     }
     info.Enabled = false;
 }
        internal static CodeFormatter GetFormatter(out Document doc)
        {
            doc = IdeApp.Workbench.ActiveDocument;
            if (doc == null)
            {
                return(null);
            }
            var editor = doc.Editor;

            if (editor == null)
            {
                return(null);
            }
            return(CodeFormatterService.GetFormatter(editor.MimeType));
        }