public async Task TestBug17765()
        {
            await Simulate(@"
namespace FormatSelectionTest
{
	public class EmptyClass
	{
		<-public EmptyClass ()
		{
		}->
	}
}", (content, ext) => {
                OnTheFlyFormatter.Format(ext.Editor, ext.DocumentContext, ext.Editor.SelectionRange.Offset, ext.Editor.SelectionRange.EndOffset);


                Assert.AreEqual(@"
namespace FormatSelectionTest
{
	public class EmptyClass
	{
		public EmptyClass()
		{
		}
	}
}", ext.Editor.Text);
            });
        }
Exemple #2
0
        public void TestBug17765()
        {
            TestViewContent content;
            var             ext = Setup(@"
namespace FormatSelectionTest
{
	public class EmptyClass
	{
		<-public EmptyClass ()
		{
		}->
	}
}", out content);

            OnTheFlyFormatter.Format(ext.document, ext.document.Editor.SelectionRange.Offset, ext.document.Editor.SelectionRange.EndOffset);


            Assert.AreEqual(@"
namespace FormatSelectionTest
{
	public class EmptyClass
	{
		public EmptyClass ()
		{
		}
	}
}", ext.document.Editor.Text);
        }
        public override async Task <KeyActions> InsertCompletionText(CompletionListWindow window, KeyActions ka, KeyDescriptor descriptor)
        {
            var  editor     = ext.Editor;
            bool isExplicit = false;
            //			if (member.DeclaringTypeDefinition.Kind == TypeKind.Interface) {
            //				foreach (var m in type.Members) {
            //					if (m.Name == member.Name && !m.ReturnType.Equals (member.ReturnType)) {
            //						isExplicit = true;
            //						break;
            //					}
            //				}
            //			}
            //			var resolvedType = type.Resolve (ext.Project).GetDefinition ();
            //			if (ext.Project != null)
            //				generator.PolicyParent = ext.Project.Policies;

            var    result    = CSharpCodeGenerator.CreatePartialMemberImplementation(ext.DocumentContext, ext.Editor, currentType, currentType.Locations.First(), Symbol, isExplicit, factory.SemanticModel);
            string sb        = result.Code.TrimStart();
            int    trimStart = result.Code.Length - sb.Length;

            sb = sb.TrimEnd();

            var lastRegion = result.BodyRegions.LastOrDefault();
            var region     = lastRegion == null? null
                                : new CodeGeneratorBodyRegion(lastRegion.StartOffset - trimStart, lastRegion.EndOffset - trimStart);

            int targetCaretPosition;
            int selectionEndPosition = -1;

            if (region != null && region.IsValid)
            {
                targetCaretPosition = declarationBegin + region.EndOffset;
            }
            else
            {
                targetCaretPosition = declarationBegin + sb.Length;
            }

            editor.ReplaceText(declarationBegin, editor.CaretOffset - declarationBegin, sb);
            if (selectionEndPosition > 0)
            {
                editor.CaretOffset = selectionEndPosition;
                editor.SetSelection(targetCaretPosition, selectionEndPosition);
            }
            else
            {
                editor.CaretOffset = targetCaretPosition;
            }

            await OnTheFlyFormatter.Format(editor, ext.DocumentContext, declarationBegin, declarationBegin + sb.Length);

            editor.CaretLine--;
            return(ka);
        }
Exemple #4
0
        public override void HandleSpecialSelectionKey(TextEditorData textEditorData, uint unicodeKey)
        {
            string start, end;

            GetSelectionSurroundings(textEditorData, unicodeKey, out start, out end);
            var selection = textEditorData.MainSelection;

            if (textEditorData.MainSelection.SelectionMode == SelectionMode.Block)
            {
                int startCol = System.Math.Min(selection.Anchor.Column, selection.Lead.Column) - 1;
                int endCol   = System.Math.Max(selection.Anchor.Column, selection.Lead.Column);
                for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++)
                {
                    DocumentLine lineSegment = textEditorData.GetLine(lineNumber);

                    if (lineSegment.Offset + startCol < lineSegment.EndOffset)
                    {
                        textEditorData.Insert(lineSegment.Offset + startCol, start);
                    }
                    if (lineSegment.Offset + endCol < lineSegment.EndOffset)
                    {
                        textEditorData.Insert(lineSegment.Offset + endCol, end);
                    }
                }

                textEditorData.MainSelection = new Selection(
                    new DocumentLocation(selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
                    new DocumentLocation(selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
                    Mono.TextEditor.SelectionMode.Block);
                textEditorData.Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine);
            }
            else
            {
                int anchorOffset = selection.GetAnchorOffset(textEditorData);
                int leadOffset   = selection.GetLeadOffset(textEditorData);
                if (leadOffset < anchorOffset)
                {
                    int tmp = anchorOffset;
                    anchorOffset = leadOffset;
                    leadOffset   = tmp;
                }
                textEditorData.Insert(anchorOffset, start);
                textEditorData.Insert(leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
                //	textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length);
                if (CSharpTextEditorIndentation.OnTheFlyFormatting)
                {
                    var l1 = textEditorData.GetLineByOffset(anchorOffset);
                    var l2 = textEditorData.GetLineByOffset(leadOffset);
                    OnTheFlyFormatter.Format(document, l1.Offset, l2.EndOffsetIncludingDelimiter);
                }
            }
        }
Exemple #5
0
        public override void HandleSpecialSelectionKey(uint unicodeKey)
        {
            string start, end;

            ((SelectionSurroundingProvider)this).GetSelectionSurroundings(unicodeKey, out start, out end);

            if (editor.SelectionMode == SelectionMode.Block)
            {
                var selection = editor.SelectionRegion;
                int startCol  = System.Math.Min(selection.Begin.Column, selection.End.Column) - 1;
                int endCol    = System.Math.Max(selection.Begin.Column, selection.End.Column);

                int minLine = System.Math.Min(selection.Begin.Line, selection.End.Line);
                int maxLine = System.Math.Max(selection.BeginLine, selection.End.Line);


                for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++)
                {
                    var lineSegment = editor.GetLine(lineNumber);

                    if (lineSegment.Offset + startCol < lineSegment.EndOffset)
                    {
                        editor.InsertText(lineSegment.Offset + startCol, start);
                    }
                    if (lineSegment.Offset + endCol < lineSegment.EndOffset)
                    {
                        editor.InsertText(lineSegment.Offset + endCol, end);
                    }
                }

//				textEditorData.MainSelection = new Selection (
//					new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
//					new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
//					Mono.TextEditor.SelectionMode.Block);
            }
            else
            {
                var selectionRange = editor.SelectionRange;
                int anchorOffset   = selectionRange.Offset;
                int leadOffset     = selectionRange.EndOffset;

                editor.InsertText(anchorOffset, start);
                editor.InsertText(leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
                //	textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length);
                if (CSharpTextEditorIndentation.OnTheFlyFormatting)
                {
                    var l1 = editor.GetLineByOffset(anchorOffset);
                    var l2 = editor.GetLineByOffset(leadOffset);
                    OnTheFlyFormatter.Format(editor, context, l1.Offset, l2.EndOffsetIncludingDelimiter);
                }
            }
        }
Exemple #6
0
        public void GenerateCode(Gtk.TreeView treeView)
        {
            TreeIter iter;

            if (!store.GetIterFirst(out iter))
            {
                return;
            }
            var includedMembers = new List <object> ();

            do
            {
                bool include = (bool)store.GetValue(iter, 0);
                if (include)
                {
                    includedMembers.Add(store.GetValue(iter, 3));
                }
            } while (store.IterNext(ref iter));
            if (includedMembers.Count == 0)
            {
                if (treeView.Selection.GetSelected(out iter))
                {
                    includedMembers.Add(store.GetValue(iter, 3));
                }
            }
            var    output = new StringBuilder();
            string indent = options.Editor.GetVirtualIndentationString(options.Editor.CaretLine);

            foreach (string nodeText in GenerateCode(includedMembers))
            {
                if (output.Length > 0)
                {
                    output.AppendLine();
                    output.AppendLine();
                }
                output.Append(AddIndent(nodeText, indent));
            }

            if (output.Length > 0)
            {
                var data = options.Editor;
                data.EnsureCaretIsNotVirtual();
                int offset = data.CaretOffset;
                var text   = output.ToString().TrimStart();
                using (var undo = data.OpenUndoGroup()) {
                    data.InsertAtCaret(text);
                    OnTheFlyFormatter.Format(data, options.DocumentContext, offset, offset + text.Length);
                }
            }
        }
        public async Task TestBug38954()
        {
            await Simulate(@"
class EmptyClass
{
	public EmptyClass()
	{
		$Console.WriteLine() ;
	}
}", (content, ext) => {
                var oldOffset = ext.Editor.CaretOffset;
                OnTheFlyFormatter.Format(ext.Editor, ext.DocumentContext);
                var newOffset = ext.Editor.CaretOffset;
                Assert.AreEqual(oldOffset, newOffset);
            });
        }
        public async Task TestBug51549()
        {
            await Simulate(@"
using System;

class MyContext
{
	public static void Main()
	{
		Console.WriteLine   $   (""Hello world!"");
	}
}", (content, ext) => {
                var oldOffset = ext.Editor.CaretOffset;
                OnTheFlyFormatter.Format(ext.Editor, ext.DocumentContext);
                var newOffset = ext.Editor.CaretOffset;
                Assert.AreEqual(oldOffset - 3, newOffset);
            });
        }
        public async Task TestBug514890()
        {
            var text = @"using System;
namespace TestConsole
{
#if
    public class Test
    {
        #region foo
        public Test()
        {
        }
        #endregion
    }
}
";

            await Simulate(text, (content, ext) => {
                OnTheFlyFormatter.Format(ext.Editor, ext.DocumentContext, 39, 41);
                Assert.AreEqual(text, content.Text);
            });
        }
 protected override void Format(TextEditor editor, Ide.Gui.Document document, int start, int end)
 {
     OnTheFlyFormatter.Format(editor, document, start, end);
 }
Exemple #11
0
        public override void HandleSpecialSelectionKey(uint unicodeKey)
        {
            string start, end;

            ((SelectionSurroundingProvider)this).GetSelectionSurroundings(unicodeKey, out start, out end);

            if (editor.SelectionMode == SelectionMode.Block)
            {
                var selection = editor.SelectionRegion;
                int startCol  = System.Math.Min(selection.Begin.Column, selection.End.Column) - 1;
                int endCol    = System.Math.Max(selection.Begin.Column, selection.End.Column);

                int minLine = System.Math.Min(selection.Begin.Line, selection.End.Line);
                int maxLine = System.Math.Max(selection.BeginLine, selection.End.Line);

                var changes = new List <TextChange> ();
                for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++)
                {
                    var lineSegment = editor.GetLine(lineNumber);

                    if (lineSegment.Offset + startCol < lineSegment.EndOffset)
                    {
                        changes.Add(new TextChange(new TextSpan(lineSegment.Offset + startCol, 0), start));
                    }
                    if (lineSegment.Offset + endCol < lineSegment.EndOffset)
                    {
                        changes.Add(new TextChange(new TextSpan(lineSegment.Offset + endCol, 0), end));
                    }
                }

                editor.ApplyTextChanges(changes);

//				textEditorData.MainSelection = new Selection (
//					new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
//					new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
//					MonoDevelop.Ide.Editor.SelectionMode.Block);
            }
            else
            {
                var selectionRange = editor.SelectionRange;
                int anchorOffset   = selectionRange.Offset;
                int leadOffset     = selectionRange.EndOffset;
                var text           = editor.GetTextAt(selectionRange);
                if (editor.Options.GenerateFormattingUndoStep)
                {
                    using (var undo = editor.OpenUndoGroup()) {
                        editor.ReplaceText(selectionRange, start);
                    }
                    using (var undo = editor.OpenUndoGroup()) {
                        editor.ReplaceText(anchorOffset, 1, start + text + end);
                        editor.SetSelection(anchorOffset + start.Length, leadOffset + start.Length + end.Length);
                    }
                    if (unicodeKey == '{')
                    {
                        using (var undo = editor.OpenUndoGroup()) {
                            OnTheFlyFormatter.Format(editor, context, anchorOffset + start.Length - 1, leadOffset + start.Length + end.Length);
                        }
                    }
                }
                else
                {
                    using (var undo = editor.OpenUndoGroup()) {
                        editor.InsertText(anchorOffset, start);
                        editor.InsertText(leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
                        if (unicodeKey == '{')
                        {
                            OnTheFlyFormatter.Format(editor, context, anchorOffset + start.Length, leadOffset + start.Length + end.Length);
                        }
                        else
                        {
                            editor.SetSelection(anchorOffset + start.Length, leadOffset + start.Length + end.Length);
                        }
                    }
                }
            }
        }