示例#1
0
        public void RenameCommand()
        {
            if (!renameProvider.HasSegments)
                return; //nothing to rename

            var textSegments = renameProvider.SegmentsToRename;
            TextSegment primarySegment = renameProvider.SelectedTextSegment;

            var link = new TextLink ("name"); //not sure why the link has to be named
            var links = new List<TextLink> ();

            //make sure that the segment where the cursor is is first in the list
            // othewise the document will jump to the location of the first link
            link.AddLink (primarySegment);

            foreach(TextSegment segment in textSegments)
            {
                if (segment != primarySegment)
                    link.AddLink (segment);
            }
            links.Add (link);

            var mode = new TextLinkEditMode (Editor.Parent, 0, links);
            mode.SetCaretPosition = false;
            mode.SelectPrimaryLink = true;

            mode.OldMode = Editor.Parent.CurrentMode;
            mode.Cancel += textLinkEditorCanceled;
            mode.HelpWindow = getHelpWindowFor (renameProvider.SelectedSymbolType);
            mode.StartMode ();
            Editor.Parent.CurrentMode = mode;
        }
        public override void Run(RefactoringOptions options)
        {
            var data = options.GetTextEditorData();

            SearchMatchingExpressions(options);

            base.Run(options);
            if (selectionEnd >= 0)
            {
                options.Document.Editor.Caret.Offset = selectionEnd;
                options.Document.Editor.SetSelection(selectionStart, selectionEnd);
            }
            else
            {
                Mono.TextEditor.TextEditor editor = data.Parent;
                TextLink link = new TextLink("name");
                if (varName != null)
                {
                    foreach (var offset in offsets)
                    {
                        link.AddLink(new Segment(offset - selectionStart, varName.Length));
                    }
                }
                List <TextLink> links = new List <TextLink> ();
                links.Add(link);
                TextLinkEditMode tle = new TextLinkEditMode(editor, selectionStart, links);
                tle.SetCaretPosition = false;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = data.CurrentMode;
                    tle.StartMode();
                    data.CurrentMode = tle;
                }
            }
        }
示例#3
0
        public override Task Link(params AstNode[] nodes)
        {
            var tcs      = new TaskCompletionSource <object> ();
            var segments = new List <TextSegment> (nodes.Select(node => new TextSegment(GetSegment(node))).OrderBy(s => s.Offset));

            var link = new TextLink("name");

            segments.ForEach(s => link.AddLink(s));
            var links = new List <TextLink> ();

            links.Add(link);
            var tle = new TextLinkEditMode(context.TextEditor.Parent, 0, links);

            tle.SetCaretPosition = false;
            if (tle.ShouldStartTextLinkMode)
            {
                operationsRunning++;
                context.TextEditor.Caret.Offset = segments [0].EndOffset;
                tle.OldMode = context.TextEditor.CurrentMode;
                tle.Cancel += (sender, e) => Rollback();
                tle.Exited += (sender, e) => DisposeOnClose();
                tle.StartMode();
                context.TextEditor.CurrentMode = tle;
                if (IdeApp.Workbench.ActiveDocument != null)
                {
                    IdeApp.Workbench.ActiveDocument.ReparseDocument();
                }
            }
            return(tcs.Task);
        }
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Editor.TextEditor editor, MonoDevelop.Ide.Editor.DocumentContext context)
        {
            using (var undo = editor.OpenUndoGroup()) {
                var result = template.InsertTemplateContents(editor, context);

                var links = result.TextLinks.Select(l => new Mono.TextEditor.TextLink(l.Name)
                {
                    Links         = l.Links.Select(s => (ISegment) new TextSegment(s.Offset, s.Length)).ToList(),
                    IsEditable    = l.IsEditable,
                    IsIdentifier  = l.IsIdentifier,
                    GetStringFunc = l.GetStringFunc != null ? (Func <Func <string, string>, Mono.TextEditor.PopupWindow.IListDataProvider <string> >)(arg => new ListDataProviderWrapper(l.GetStringFunc(arg))) : null
                }).ToList();
                var tle = new TextLinkEditMode(this, result.InsertPosition, links);
                tle.TextLinkMode = TextLinkMode.General;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = CurrentMode;
                    tle.StartMode();
                    CurrentMode = tle;
                    GLib.Timeout.Add(10, delegate {
                        tle.UpdateTextLinks();
                        return(false);
                    });
                }
            }
        }
		public override void Run (RefactoringOptions options)
		{
			base.Run (options);
			
			TextEditorData data = options.GetTextEditorData ();
			Mono.TextEditor.TextEditor editor = data.Parent;
			
			List<TextLink> links = new List<TextLink> ();
			TextLink link = new TextLink ("name");
			int referenceCount = 1;
			MemberResolveResult resolveResult = options.ResolveResult as MemberResolveResult;
			IProperty property = resolveResult.ResolvedMember as IProperty;
			if (property.HasGet)
				referenceCount++;
			if (property.HasSet)
				referenceCount++;
			for (int i = refactoringStartOffset; i < data.Document.Length - backingStoreName.Length; i++) {
				if (data.Document.GetTextAt (i, backingStoreName.Length) == backingStoreName) {
					link.AddLink (new Segment (i - refactoringStartOffset, backingStoreName.Length));
					if (link.Count == referenceCount)
						break;
				}
			}
			
			links.Add (link);
			TextLinkEditMode tle = new TextLinkEditMode (editor, refactoringStartOffset, links);
			tle.SetCaretPosition = false;
			if (tle.ShouldStartTextLinkMode) {
				tle.OldMode = data.CurrentMode;
				tle.StartMode ();
				data.CurrentMode = tle;
			}
		}
示例#6
0
        public void TestLinkRemoval()
        {
            var editor = CreateEditorWithLinks(@"[0foo] [0foo] [0foo]", out var links);

            editor.Options.IndentStyle = IndentStyle.Auto;
            var oldLocation = editor.Caret.Location;

            var tle = new TextLinkEditMode(editor, 0, links);

            tle.StartMode();
            DeleteActions.Backspace(editor.GetTextEditorData());
            tle.ExitTextLinkMode();
            Assert.AreEqual("  ", editor.Text);
        }
示例#7
0
        public void TestLinkReplace()
        {
            var editor = CreateEditorWithLinks(@"[0foo] [0foo] [0foo]", out var links);

            editor.Options.IndentStyle = IndentStyle.Auto;
            var oldLocation = editor.Caret.Location;

            var tle = new TextLinkEditMode(editor, 0, links);

            tle.StartMode();
            editor.InsertAtCaret("bar");
            tle.ExitTextLinkMode();
            Assert.AreEqual("bar bar bar", editor.Text);
        }
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
        {
            using (var undo = Document.OpenUndoGroup()) {
                var result = template.InsertTemplateContents(document);

                var links = result.TextLinks;

                var tle = new TextLinkEditMode(this, result.InsertPosition, links);
                tle.TextLinkMode = TextLinkMode.General;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = CurrentMode;
                    tle.StartMode();
                    CurrentMode = tle;
                }
            }
        }
示例#9
0
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
        {
            Document.BeginAtomicUndo();
            var result = template.InsertTemplateContents(document);
            var tle    = new TextLinkEditMode(this, result.InsertPosition, result.TextLinks);

            if (PropertyService.Get("OnTheFlyFormatting", false))
            {
                var prettyPrinter = CodeFormatterService.GetFormatter(Document.MimeType);
                if (prettyPrinter != null)
                {
                    int    endOffset = result.InsertPosition + result.Code.Length;
                    string oldText   = Document.GetTextAt(result.InsertPosition, result.Code.Length);
                    var    policies  = document.Project != null ? document.Project.Policies : null;
                    string text      = prettyPrinter.FormatText(policies, Document.Text, result.InsertPosition, endOffset);

                    if (text != null)
                    {
                        Replace(result.InsertPosition, result.Code.Length, text);
                    }
                    else
                    {
                        //if formatting failed, just use the unformatted text
                        text = oldText;
                    }

                    Caret.Offset = result.InsertPosition + TranslateOffset(oldText, text, Caret.Offset - result.InsertPosition);
                    foreach (TextLink textLink in tle.Links)
                    {
                        foreach (ISegment segment in textLink.Links)
                        {
                            segment.Offset = TranslateOffset(oldText, text, segment.Offset);
                        }
                    }
                }
            }

            if (tle.ShouldStartTextLinkMode)
            {
                tle.OldMode = CurrentMode;
                tle.StartMode();
                CurrentMode = tle;
            }
            Document.EndAtomicUndo();
        }
示例#10
0
        public override void Run(RefactoringOptions options)
        {
            base.Run(options);

            TextEditorData data = options.GetTextEditorData();

            Mono.TextEditor.TextEditor editor = data.Parent;

            List <TextLink>     links          = new List <TextLink> ();
            TextLink            link           = new TextLink("name");
            int                 referenceCount = 1;
            MemberResolveResult resolveResult  = options.ResolveResult as MemberResolveResult;
            IProperty           property       = resolveResult.ResolvedMember as IProperty;

            if (property.HasGet)
            {
                referenceCount++;
            }
            if (property.HasSet)
            {
                referenceCount++;
            }
            for (int i = refactoringStartOffset; i < data.Document.Length - backingStoreName.Length; i++)
            {
                if (data.Document.GetTextAt(i, backingStoreName.Length) == backingStoreName)
                {
                    link.AddLink(new Segment(i - refactoringStartOffset, backingStoreName.Length));
                    if (link.Count == referenceCount)
                    {
                        break;
                    }
                }
            }

            links.Add(link);
            TextLinkEditMode tle = new TextLinkEditMode(editor, refactoringStartOffset, links);

            tle.SetCaretPosition = false;
            if (tle.ShouldStartTextLinkMode)
            {
                tle.OldMode = data.CurrentMode;
                tle.StartMode();
                data.CurrentMode = tle;
            }
        }
示例#11
0
        public void TestVSTS627497()
        {
            var editor      = CreateEditorWithLinks(@"[0class] Test
{
	void TestMethod ()
	{
		$
	}
}", out var links);
            var oldLocation = editor.Caret.Location;

            var tle = new TextLinkEditMode(editor, 0, links);

            tle.StartMode();
            DeleteActions.Backspace(editor.GetTextEditorData());
            editor.InsertAtCaret("struct");
            tle.ExitTextLinkMode();

            Assert.AreEqual(oldLocation, editor.Caret.Location);
        }
            public override void Perform(Script script)
            {
                List <Segment> segments = new List <Segment> ();

                foreach (var action in script.Actions)
                {
                    if (action == this)
                    {
                        break;
                    }
                    var noa = action as NodeOutputAction;
                    if (noa == null)
                    {
                        continue;
                    }
                    foreach (var astNode in Linked)
                    {
                        NodeOutput.Segment segment;
                        if (noa.NodeOutput.NodeSegments.TryGetValue(astNode, out segment))
                        {
                            segments.Add(new Segment(noa.Offset + segment.Offset, segment.Length));
                        }
                    }
                }
                segments.Sort((x, y) => x.Offset.CompareTo(y.Offset));
                var link = new TextLink("name");

                segments.ForEach(s => link.AddLink(s));
                var links = new List <TextLink> ();

                links.Add(link);
                var tle = new TextLinkEditMode(ctx.Document.Editor.Parent, 0, links);

                tle.SetCaretPosition = false;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = ctx.Document.Editor.CurrentMode;
                    tle.StartMode();
                    ctx.Document.Editor.CurrentMode = tle;
                }
            }
示例#13
0
        public override void Link(params AstNode[] nodes)
        {
            var segments = new List <TextSegment> (nodes.Select(node => new TextSegment(GetSegment(node))).OrderBy(s => s.Offset));

            var link = new TextLink("name");

            segments.ForEach(s => link.AddLink(s));
            var links = new List <TextLink> ();

            links.Add(link);
            var tle = new TextLinkEditMode(document.Editor.Parent, 0, links);

            tle.SetCaretPosition = false;
            if (tle.ShouldStartTextLinkMode)
            {
                document.Editor.Caret.Offset = segments [0].EndOffset;
                tle.OldMode = document.Editor.CurrentMode;
                tle.StartMode();
                document.Editor.CurrentMode = tle;
            }
        }
示例#14
0
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Editor.TextEditor editor, MonoDevelop.Ide.Editor.DocumentContext context)
        {
            using (var undo = editor.OpenUndoGroup()) {
                var result = template.InsertTemplateContents(editor, context);

                var links = result.TextLinks.Select(l => new Mono.TextEditor.TextLink(l.Name)
                {
                    Links        = l.Links.Select(s => new TextSegment(s.Offset, s.Length)).ToList(),
                    IsEditable   = l.IsEditable,
                    IsIdentifier = l.IsIdentifier
                }).ToList();
                var tle = new TextLinkEditMode(this, result.InsertPosition, links);
                tle.TextLinkMode = TextLinkMode.General;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = CurrentMode;
                    tle.StartMode();
                    CurrentMode = tle;
                }
            }
        }
 public override void Run(RefactoringOptions options)
 {
     base.Run(options);
     if (selectionEnd >= 0)
     {
         options.Document.TextEditor.CursorPosition = selectionEnd;
         options.Document.TextEditor.Select(selectionStart, selectionEnd);
     }
     else
     {
         TextEditorData             data   = options.GetTextEditorData();
         Mono.TextEditor.TextEditor editor = data.Parent;
         TextLink link = new TextLink("name");
         if (varName != null)
         {
             if (insertOffset >= 0)
             {
                 link.AddLink(new Segment(insertOffset - selectionStart, varName.Length));
             }
             else
             {
                 LoggingService.LogWarning("insert offset not found.");
             }
             if (replaceOffset >= 0)
             {
                 link.AddLink(new Segment(replaceOffset - selectionStart, varName.Length));
             }
         }
         List <TextLink> links = new List <TextLink> ();
         links.Add(link);
         TextLinkEditMode tle = new TextLinkEditMode(editor, selectionStart, links);
         tle.SetCaretPosition = false;
         if (tle.ShouldStartTextLinkMode)
         {
             tle.OldMode = data.CurrentMode;
             tle.StartMode();
             data.CurrentMode = tle;
         }
     }
 }
示例#16
0
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
        {
            Document.BeginAtomicUndo();
            var result           = template.InsertTemplateContents(document);
            TextLinkEditMode tle = new TextLinkEditMode(this,
                                                        result.InsertPosition,
                                                        result.TextLinks);

            if (PropertyService.Get("OnTheFlyFormatting", false))
            {
                Formatter prettyPrinter = TextFileService.GetFormatter(Document.MimeType);
                if (prettyPrinter != null && prettyPrinter.SupportsOnTheFlyFormatting)
                {
                    int    endOffset = result.InsertPosition + result.Code.Length;
                    string text      = prettyPrinter.FormatText(document.Project.Policies, Document.MimeType, Document.Text, result.InsertPosition, endOffset);
                    string oldText   = Document.GetTextAt(result.InsertPosition, result.Code.Length);
                    //					Console.WriteLine (result.InsertPosition);
                    //					Console.WriteLine ("old:" + oldText);
                    //					Console.WriteLine ("new:" + text);
                    Replace(result.InsertPosition, result.Code.Length, text);
                    Caret.Offset = result.InsertPosition + TranslateOffset(oldText, text, Caret.Offset - result.InsertPosition);
                    foreach (TextLink textLink in tle.Links)
                    {
                        foreach (ISegment segment in textLink.Links)
                        {
                            segment.Offset = TranslateOffset(oldText, text, segment.Offset);
                        }
                    }
                }
            }

            if (tle.ShouldStartTextLinkMode)
            {
                tle.OldMode = CurrentMode;
                tle.StartMode();
                CurrentMode = tle;
            }
            Document.EndAtomicUndo();
        }
示例#17
0
        internal void InsertTemplate(CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
        {
            using (var undo = Document.OpenUndoGroup()) {
                var result = template.InsertTemplateContents(document);

                var links = result.TextLinks;
                if (PropertyService.Get("OnTheFlyFormatting", true))
                {
                    var prettyPrinter = CodeFormatterService.GetFormatter(Document.MimeType);
                    if (prettyPrinter != null)
                    {
                        int endOffset = result.InsertPosition + result.Code.Length;
                        //						string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
                        //						var policies = document.Project != null ? document.Project.Policies : null;
                        var oldVersion = Document.Version;
                        prettyPrinter.OnTheFlyFormat(document, result.InsertPosition, endOffset);
                        foreach (var textLink in links)
                        {
                            for (int i = 0; i < textLink.Links.Count; i++)
                            {
                                var segment          = textLink.Links [i];
                                var translatedOffset = oldVersion.MoveOffsetTo(Document.Version, result.InsertPosition + segment.Offset) - result.InsertPosition;
                                textLink.Links [i] = new TextSegment(translatedOffset, segment.Length);
                            }
                        }
                    }
                }

                var tle = new TextLinkEditMode(this, result.InsertPosition, links);
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.OldMode = CurrentMode;
                    tle.StartMode();
                    CurrentMode = tle;
                }
            }
        }
		public override void InsertCompletionText (CompletionListWindow window, ref KeyActions ka, Gdk.Key closeChar, char keyChar, Gdk.ModifierType modifier)
		{
			// insert add/remove event handler code after +=/-=
			editor.Replace (initialOffset, editor.Caret.Offset - initialOffset, this.DisplayText + (AddSemicolon ? ";" : ""));
			
			// Search opening bracket of member
			int pos = callingMember != null ? editor.Document.LocationToOffset (callingMember.BodyRegion.BeginLine, callingMember.BodyRegion.BeginColumn) : initialOffset;
			while (pos < editor.Document.TextLength && editor.Document.GetCharAt (pos) != '{') {
				pos++;
			}
			
			// Search closing bracket of member
			pos = editor.Document.GetMatchingBracketOffset (pos) + 1;
			
			pos = Math.Max (0, Math.Min (pos, editor.Document.TextLength - 1));
			
			// Insert new event handler after closing bracket
			var line = callingMember != null ? editor.Document.GetLine (callingMember.Region.BeginLine) : editor.Document.GetLineByOffset (initialOffset);
			string indent = line.GetIndentation (editor.Document);
			
			StringBuilder sb = new StringBuilder ();
			sb.Append (editor.EolMarker);
			sb.Append (editor.EolMarker);
			sb.Append (indent);
			if (callingMember != null && callingMember.IsStatic)
				sb.Append ("static ");
			sb.Append ("void ");
			int pos2 = sb.Length;
			sb.Append (this.DisplayText);
			sb.Append (' ');
			sb.Append (this.parameterList);
			sb.Append (editor.EolMarker);
			sb.Append (indent);
			sb.Append ("{");
			sb.Append (editor.EolMarker);
			sb.Append (indent);
			sb.Append (editor.Options.IndentationString);
			int cursorPos = pos + sb.Length;
			sb.Append (editor.EolMarker);
			sb.Append (indent);
			sb.Append ("}");
			editor.Insert (pos, sb.ToString ());
			editor.Caret.Offset = cursorPos;
			
			// start text link mode after insert
			List<TextLink> links = new List<TextLink> ();
			TextLink link = new TextLink ("name");
			
			link.AddLink (new TextSegment (0, this.DisplayText.Length));
			link.AddLink (new TextSegment (pos - initialOffset + pos2, this.DisplayText.Length));
			links.Add (link);
			
			var tle = new TextLinkEditMode (editor.Parent, initialOffset, links);
			tle.TextLinkMode = TextLinkMode.EditIdentifier;
			tle.SetCaretPosition = true;
			tle.SelectPrimaryLink = true;
			tle.OldMode = editor.CurrentMode;
			tle.StartMode ();
			editor.CurrentMode = tle;
		}
		public override void Run (RefactoringOptions options)
		{
			if (options.SelectedItem is IVariable) {
				var field = options.SelectedItem as IField;
				if (field != null && field.Accessibility != Accessibility.Private) {
					MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
					return;
				}

				var col = ReferenceFinder.FindReferences (options.SelectedItem, true);
				if (col == null)
					return;
				var data = options.Document != null ? options.GetTextEditorData () : IdeApp.Workbench.ActiveDocument.Editor;
				var editor = data.Parent;
				if (editor == null) {
					MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
					return;
				}
				
				var links = new List<TextLink> ();
				var link = new TextLink ("name");
				int baseOffset = Int32.MaxValue;
				foreach (var r in col) {
					baseOffset = Math.Min (baseOffset, r.Offset);
				}
				foreach (MemberReference r in col) {
					var segment = new TextSegment (r.Offset - baseOffset, r.Length);
					if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) {
						link.Links.Insert (0, segment); 
					} else {
						link.AddLink (segment);
					}
				}
				
				links.Add (link);
				if (editor.CurrentMode is TextLinkEditMode)
					((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode ();
				TextLinkEditMode tle = new TextLinkEditMode (editor, baseOffset, links);
				tle.SetCaretPosition = false;
				tle.SelectPrimaryLink = true;
				if (tle.ShouldStartTextLinkMode) {
					var helpWindow = new TableLayoutModeHelpWindow ();
					helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
					helpWindow.TitleText = options.SelectedItem is IVariable ? GettextCatalog.GetString ("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString ("<b>Parameter -- Renaming</b>");
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Key</b>"), GettextCatalog.GetString ("<b>Behavior</b>")));
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Return</b>"), GettextCatalog.GetString ("<b>Accept</b> this refactoring.")));
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Esc</b>"), GettextCatalog.GetString ("<b>Cancel</b> this refactoring.")));
					tle.HelpWindow = helpWindow;
					tle.Cancel += delegate {
						if (tle.HasChangedText)
							editor.Document.Undo ();
					};
					tle.OldMode = data.CurrentMode;
					tle.StartMode ();
					data.CurrentMode = tle;
				}
			} else {
				MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
			}
		}
示例#20
0
		public override void Run (RefactoringOptions options)
		{
			if (options.SelectedItem is LocalVariable || options.SelectedItem is IParameter) {
				MemberReferenceCollection col = GetReferences (options);
				if (col == null)
					return;
				TextEditorData data = options.GetTextEditorData ();
				Mono.TextEditor.TextEditor editor = data.Parent;
				if (editor == null) {
					MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
					return;
				}

				
				List<TextLink> links = new List<TextLink> ();
				TextLink link = new TextLink ("name");
				int baseOffset = Int32.MaxValue;
				foreach (MemberReference r in col) {
					baseOffset = Math.Min (baseOffset, data.Document.LocationToOffset (r.Line, r.Column));
				}
				foreach (MemberReference r in col) {
					Segment segment = new Segment (data.Document.LocationToOffset (r.Line, r.Column) - baseOffset, r.Name.Length);
					if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) {
						link.Links.Insert (0, segment); 
					} else {
						link.AddLink (segment);
					}
				}
				
				links.Add (link);
				TextLinkEditMode tle = new TextLinkEditMode (editor, baseOffset, links);
				tle.SetCaretPosition = false;
				tle.SelectPrimaryLink = true;
				if (tle.ShouldStartTextLinkMode) {
					ModeHelpWindow helpWindow = new ModeHelpWindow ();
					helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
					helpWindow.TitleText = options.SelectedItem is LocalVariable ? GettextCatalog.GetString ("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString ("<b>Parameter -- Renaming</b>");
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Key</b>"), GettextCatalog.GetString ("<b>Behavior</b>")));
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Return</b>"), GettextCatalog.GetString ("<b>Accept</b> this refactoring.")));
					helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Esc</b>"), GettextCatalog.GetString ("<b>Cancel</b> this refactoring.")));
					tle.HelpWindow = helpWindow;
					tle.Cancel += delegate {
						editor.Document.Undo ();
					};
					tle.OldMode = data.CurrentMode;
					tle.StartMode ();
					data.CurrentMode = tle;
				}
			} else {
				MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
			}
		}
		void ITextEditorImpl.StartTextLinkMode (TextLinkModeOptions textLinkModeOptions)
		{
			var convertedLinks = new List<Mono.TextEditor.TextLink> ();
			foreach (var link in textLinkModeOptions.Links) {
				var convertedLink = new Mono.TextEditor.TextLink (link.Name);
				convertedLink.IsEditable = link.IsEditable;
				convertedLink.IsIdentifier = link.IsIdentifier;
				var func = link.GetStringFunc;
				if (func != null) {
					convertedLink.GetStringFunc = delegate(Func<string, string> arg) {
						return new ListDataProviderWrapper (func (arg));
					};
				}
				foreach (var segment in link.Links) {
					convertedLink.AddLink (new Mono.TextEditor.TextSegment (segment.Offset, segment.Length)); 
				}
				convertedLinks.Add (convertedLink); 
			}

			var tle = new TextLinkEditMode (TextEditor, 0, convertedLinks);
			tle.SetCaretPosition = false;
			if (tle.ShouldStartTextLinkMode) {
				tle.OldMode = TextEditor.CurrentMode;
				if (textLinkModeOptions.ModeExitedAction != null) {
					tle.Cancel += (sender, e) => textLinkModeOptions.ModeExitedAction (new TextLinkModeEventArgs (false));
					tle.Exited += (sender, e) => {
						for (int i = 0; i < convertedLinks.Count; i++) {
							textLinkModeOptions.Links[i].CurrentText = convertedLinks[i].CurrentText;
						}
						textLinkModeOptions.ModeExitedAction (new TextLinkModeEventArgs (true));
						
					};
				}
				var undoOperation = TextEditor.OpenUndoGroup ();
				tle.Exited += (object sender, EventArgs e) => undoOperation.Dispose ();
				tle.StartMode ();
				TextEditor.CurrentMode = tle;
			}
		}
示例#22
0
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
		{
			Document.BeginAtomicUndo ();
			var result = template.InsertTemplateContents (document);
			TextLinkEditMode tle = new TextLinkEditMode (this, 
			                                             result.InsertPosition,
			                                             result.TextLinks);
			
			if (PropertyService.Get ("OnTheFlyFormatting", false)) {
				Formatter prettyPrinter = TextFileService.GetFormatter (Document.MimeType);
				if (prettyPrinter != null && prettyPrinter.SupportsOnTheFlyFormatting) {
					int endOffset = result.InsertPosition + result.Code.Length;
					string text = prettyPrinter.FormatText (document.Project.Policies, Document.MimeType, Document.Text, result.InsertPosition, endOffset);
					string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
					//					Console.WriteLine (result.InsertPosition);
					//					Console.WriteLine ("old:" + oldText);
					//					Console.WriteLine ("new:" + text);
					Replace (result.InsertPosition, result.Code.Length, text);
					Caret.Offset = result.InsertPosition + TranslateOffset (oldText, text, Caret.Offset - result.InsertPosition);
					foreach (TextLink textLink in tle.Links) {
						foreach (ISegment segment in textLink.Links) {
							segment.Offset = TranslateOffset (oldText, text, segment.Offset);
						}
					}
				}
			}
			
			if (tle.ShouldStartTextLinkMode) {
				tle.OldMode = CurrentMode;
				tle.StartMode ();
				CurrentMode = tle;
			}
			Document.EndAtomicUndo ();
		}
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
		{
			Document.BeginAtomicUndo ();
			var result = template.InsertTemplateContents (document);
			var tle = new TextLinkEditMode (this, result.InsertPosition, result.TextLinks);
			
			if (PropertyService.Get ("OnTheFlyFormatting", false)) {
				var prettyPrinter = CodeFormatterService.GetFormatter (Document.MimeType);
				if (prettyPrinter != null) {
					int endOffset = result.InsertPosition + result.Code.Length;
					string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
					var policies = document.Project != null ? document.Project.Policies : null;
					string text = prettyPrinter.FormatText (policies, Document.Text, result.InsertPosition, endOffset);
					
					if (text != null)
						Replace (result.InsertPosition, result.Code.Length, text);
					else
						//if formatting failed, just use the unformatted text
						text = oldText;
					
					Caret.Offset = result.InsertPosition + TranslateOffset (oldText, text, Caret.Offset - result.InsertPosition);
					foreach (TextLink textLink in tle.Links) {
						foreach (ISegment segment in textLink.Links) {
							segment.Offset = TranslateOffset (oldText, text, segment.Offset);
						}
					}
				}
			}
			
			if (tle.ShouldStartTextLinkMode) {
				tle.OldMode = CurrentMode;
				tle.StartMode ();
				CurrentMode = tle;
			}
			Document.EndAtomicUndo ();
		}
示例#24
0
        public override void Run(RefactoringOptions options)
        {
            if (options.SelectedItem is IVariable)
            {
                var field = options.SelectedItem as IField;
                if (field != null && (field.Accessibility != Accessibility.Private || field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Parts.Count > 1))
                {
                    MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
                    return;
                }

                var par = options.SelectedItem as IParameter;
                if (par != null && par.Owner != null && (par.Owner.Accessibility != Accessibility.Private || par.Owner.DeclaringTypeDefinition != null && par.Owner.DeclaringTypeDefinition.Parts.Count > 1))
                {
                    MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
                    return;
                }

                var col = ReferenceFinder.FindReferences(options.SelectedItem, true);
                if (col == null)
                {
                    return;
                }
                var data = options.Document != null?options.GetTextEditorData() : IdeApp.Workbench.ActiveDocument.Editor;

                var editor = data.Parent;
                if (editor == null)
                {
                    MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
                    return;
                }

                var links      = new List <TextLink> ();
                var link       = new TextLink("name");
                int baseOffset = Int32.MaxValue;
                foreach (var r in col)
                {
                    baseOffset = Math.Min(baseOffset, r.Offset);
                }
                foreach (MemberReference r in col)
                {
                    var segment = new TextSegment(r.Offset - baseOffset, r.Length);
                    if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset)
                    {
                        link.Links.Insert(0, segment);
                    }
                    else
                    {
                        link.AddLink(segment);
                    }
                }

                links.Add(link);
                if (editor.CurrentMode is TextLinkEditMode)
                {
                    ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode();
                }
                TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links);
                tle.SetCaretPosition  = false;
                tle.SelectPrimaryLink = true;
                if (tle.ShouldStartTextLinkMode)
                {
                    var helpWindow = new TableLayoutModeHelpWindow();
                    helpWindow.TitleText = options.SelectedItem is IVariable?GettextCatalog.GetString("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString("<b>Parameter -- Renaming</b>");

                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Return</b>"), GettextCatalog.GetString("<b>Accept</b> this refactoring.")));
                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
                    tle.HelpWindow = helpWindow;
                    tle.Cancel    += delegate {
                        if (tle.HasChangedText)
                        {
                            editor.Document.Undo();
                        }
                    };
                    tle.OldMode = data.CurrentMode;
                    tle.StartMode();
                    data.CurrentMode = tle;
                }
            }
            else
            {
                MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
            }
        }
示例#25
0
        public override void InsertCompletionText(CompletionListWindow window, ref KeyActions ka, Gdk.Key closeChar, char keyChar, Gdk.ModifierType modifier)
        {
            // insert add/remove event handler code after +=/-=
            editor.Replace(initialOffset, editor.Caret.Offset - initialOffset, this.DisplayText + (AddSemicolon ? ";" : ""));

            // Search opening bracket of member
            int pos = callingMember != null?editor.Document.LocationToOffset(callingMember.BodyRegion.BeginLine, callingMember.BodyRegion.BeginColumn) : initialOffset;

            while (pos < editor.Document.TextLength && editor.Document.GetCharAt(pos) != '{')
            {
                pos++;
            }

            // Search closing bracket of member
            pos = editor.Document.GetMatchingBracketOffset(pos) + 1;

            pos = Math.Max(0, Math.Min(pos, editor.Document.TextLength - 1));

            // Insert new event handler after closing bracket
            string indent = editor.Document.GetLine(callingMember.Region.BeginLine).GetIndentation(editor.Document);

            StringBuilder sb = new StringBuilder();

            sb.Append(editor.EolMarker);
            sb.Append(editor.EolMarker);
            sb.Append(indent);
            if (callingMember.IsStatic)
            {
                sb.Append("static ");
            }
            sb.Append("void ");
            int pos2 = sb.Length;

            sb.Append(this.DisplayText);
            sb.Append(' ');
            sb.Append(this.parameterList);
            sb.Append(editor.EolMarker);
            sb.Append(indent);
            sb.Append("{");
            sb.Append(editor.EolMarker);
            sb.Append(indent);
            sb.Append(editor.Options.IndentationString);
            int cursorPos = pos + sb.Length;

            sb.Append(editor.EolMarker);
            sb.Append(indent);
            sb.Append("}");
            editor.Insert(pos, sb.ToString());
            editor.Caret.Offset = cursorPos;

            // start text link mode after insert
            List <TextLink> links = new List <TextLink> ();
            TextLink        link  = new TextLink("name");

            link.AddLink(new TextSegment(0, this.DisplayText.Length));
            link.AddLink(new TextSegment(pos - initialOffset + pos2, this.DisplayText.Length));
            links.Add(link);

            var tle = new TextLinkEditMode(editor.Parent, initialOffset, links);

            tle.TextLinkMode      = TextLinkMode.EditIdentifier;
            tle.SetCaretPosition  = true;
            tle.SelectPrimaryLink = true;
            tle.OldMode           = editor.CurrentMode;
            tle.StartMode();
            editor.CurrentMode = tle;
        }
示例#26
0
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Editor.TextEditor editor, MonoDevelop.Ide.Editor.DocumentContext context)
		{
			using (var undo = editor.OpenUndoGroup ()) {
				var result = template.InsertTemplateContents (editor, context);

				var links = result.TextLinks.Select (l => new Mono.TextEditor.TextLink (l.Name) {
					Links = l.Links.Select (s => new TextSegment (s.Offset, s.Length)).ToList (),
					IsEditable = l.IsEditable,
					IsIdentifier = l.IsIdentifier
				}).ToList ();
				var tle = new TextLinkEditMode (this, result.InsertPosition, links);
				tle.TextLinkMode = TextLinkMode.General;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = CurrentMode;
					tle.StartMode ();
					CurrentMode = tle;
				}
			}
		}
示例#27
0
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Editor.TextEditor editor, MonoDevelop.Ide.Editor.DocumentContext context)
		{
			using (var undo = editor.OpenUndoGroup ()) {
				var result = template.InsertTemplateContents (editor, context);

				var links = result.TextLinks.Select (l => new Mono.TextEditor.TextLink (l.Name) {
					Links = l.Links.Select (s => new TextSegment (s.Offset, s.Length)).ToList (),
					IsEditable = l.IsEditable,
					IsIdentifier = l.IsIdentifier,
					GetStringFunc = l.GetStringFunc != null ? (Func<Func<string, string>, Mono.TextEditor.PopupWindow.IListDataProvider<string>>)(arg => new ListDataProviderWrapper (l.GetStringFunc (arg))) : null
				}).ToList ();
				var tle = new TextLinkEditMode (this, result.InsertPosition, links);
				tle.TextLinkMode = TextLinkMode.General;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = CurrentMode;
					tle.StartMode ();
					CurrentMode = tle;
					GLib.Timeout.Add (10, delegate {
						tle.UpdateTextLinks ();
						return false;
					}); 
				}
			}
		}
示例#28
0
        public override void Run(RefactoringOptions options)
        {
            if (options.SelectedItem is LocalVariable || options.SelectedItem is IParameter)
            {
                var col = ReferenceFinder.FindReferences(options.SelectedItem);
                if (col == null)
                {
                    return;
                }
                TextEditorData             data   = options.GetTextEditorData();
                Mono.TextEditor.TextEditor editor = data.Parent;
                if (editor == null)
                {
                    MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
                    return;
                }

                List <TextLink> links      = new List <TextLink> ();
                TextLink        link       = new TextLink("name");
                int             baseOffset = Int32.MaxValue;
                foreach (MemberReference r in col)
                {
                    baseOffset = Math.Min(baseOffset, data.Document.LocationToOffset(r.Line, r.Column));
                }
                foreach (MemberReference r in col)
                {
                    Segment segment = new Segment(data.Document.LocationToOffset(r.Line, r.Column) - baseOffset, r.Name.Length);
                    if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset)
                    {
                        link.Links.Insert(0, segment);
                    }
                    else
                    {
                        link.AddLink(segment);
                    }
                }

                links.Add(link);
                if (editor.CurrentMode is TextLinkEditMode)
                {
                    ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode();
                }
                TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links);
                tle.SetCaretPosition  = false;
                tle.SelectPrimaryLink = true;
                if (tle.ShouldStartTextLinkMode)
                {
                    ModeHelpWindow helpWindow = new ModeHelpWindow();
                    helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
                    helpWindow.TitleText    = options.SelectedItem is LocalVariable?GettextCatalog.GetString("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString("<b>Parameter -- Renaming</b>");

                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>")));
                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Return</b>"), GettextCatalog.GetString("<b>Accept</b> this refactoring.")));
                    helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring.")));
                    tle.HelpWindow = helpWindow;
                    tle.Cancel    += delegate {
                        editor.Document.Undo();
                    };
                    tle.OldMode = data.CurrentMode;
                    tle.StartMode();
                    data.CurrentMode = tle;
                }
            }
            else
            {
                MessageService.ShowCustomDialog(new RenameItemDialog(options, this));
            }
        }
		public override Task Link (params AstNode[] nodes)
		{
			var tcs = new TaskCompletionSource<object> ();
			var segments = new List<TextSegment> (nodes.Select (node => new TextSegment (GetSegment (node))).OrderBy (s => s.Offset));
			
			var link = new TextLink ("name");
			segments.ForEach (s => link.AddLink (s));
			var links = new List<TextLink> ();
			links.Add (link);
			var tle = new TextLinkEditMode (document.Editor.Parent, 0, links);
			tle.SetCaretPosition = false;
			if (tle.ShouldStartTextLinkMode) {
				operationsRunning++;
				document.Editor.Caret.Offset = segments [0].EndOffset;
				tle.OldMode = document.Editor.CurrentMode;
				tle.Cancel += (sender, e) => Rollback ();
				tle.Exited += (sender, e) => DisposeOnClose (); 
				tle.StartMode ();
				document.Editor.CurrentMode = tle;
			}
			return tcs.Task;
		}
示例#30
0
		public override void Run (RefactoringOptions options)
		{
			if (options.SelectedItem is IVariable) {
				var field = options.SelectedItem as IField;
				if (field != null && (field.Accessibility != Accessibility.Private || field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Parts.Count > 1)) {
					using (var dlg = new RenameItemDialog (options, this))
						MessageService.ShowCustomDialog (dlg);
					return;
				}

				var par = options.SelectedItem as IParameter;
				if (par != null && par.Owner != null && (par.Owner.Accessibility != Accessibility.Private || par.Owner.DeclaringTypeDefinition != null && par.Owner.DeclaringTypeDefinition.Parts.Count > 1)) {
					using (var dlg = new RenameItemDialog (options, this))
						MessageService.ShowCustomDialog (dlg);
					return;
				}

				var col = ReferenceFinder.FindReferences (options.SelectedItem, true);
				if (col == null)
					return;
				var data = options.Document != null ? options.GetTextEditorData () : IdeApp.Workbench.ActiveDocument.Editor;
				var editor = data.Parent;
				if (editor == null) {
					using (var dlg = new RenameItemDialog (options, this))
						MessageService.ShowCustomDialog (dlg);
					return;
				}
				
				var links = new List<TextLink> ();
				var link = new TextLink ("name");
				int baseOffset = Int32.MaxValue;
				foreach (var r in col) {
					baseOffset = Math.Min (baseOffset, r.Offset);
				}
				foreach (MemberReference r in col) {
					var segment = new TextSegment (r.Offset - baseOffset, r.Length);
					if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) {
						link.Links.Insert (0, segment); 
					} else {
						link.AddLink (segment);
					}
				}
				
				links.Add (link);
				if (editor.CurrentMode is TextLinkEditMode)
					((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode ();
				TextLinkEditMode tle = new TextLinkEditMode (editor, baseOffset, links);
				tle.SetCaretPosition = false;
				tle.SelectPrimaryLink = true;
				if (tle.ShouldStartTextLinkMode) {
					tle.Cancel += delegate {
						if (tle.HasChangedText)
							editor.Document.Undo ();
					};
					tle.OldMode = data.CurrentMode;
					tle.StartMode ();
					data.CurrentMode = tle;
				}
			} else {
				using (var dlg = new RenameItemDialog (options, this))
					MessageService.ShowCustomDialog (dlg);
			}
		}
		public override void Run (RefactoringOptions options)
		{
			if (options.SelectedItem is LocalVariable || options.SelectedItem is IParameter) {
				MemberReferenceCollection col = GetReferences (options);
				if (col == null)
					return;
				TextEditorData data = options.GetTextEditorData ();
				Mono.TextEditor.TextEditor editor = data.Parent;
				if (editor == null) {
					MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
					return;
				}

				
				List<TextLink> links = new List<TextLink> ();
				TextLink link = new TextLink ("name");
				int baseOffset = Int32.MaxValue;
				foreach (MemberReference r in col) {
					baseOffset = Math.Min (baseOffset, data.Document.LocationToOffset (r.Line - 1, r.Column - 1));
				}
				foreach (MemberReference r in col) {
					Segment segment = new Segment (data.Document.LocationToOffset (r.Line - 1, r.Column - 1) - baseOffset, r.Name.Length);
					if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) {
						link.Links.Insert (0, segment); 
					} else {
						link.AddLink (segment);
					}
				}
				
				links.Add (link);
				TextLinkEditMode tle = new TextLinkEditMode (editor, baseOffset, links);
				tle.SetCaretPosition = false;
				tle.SelectPrimaryLink = true;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = data.CurrentMode;
					tle.StartMode ();
					data.CurrentMode = tle;
				}
			} else {
				MessageService.ShowCustomDialog (new RenameItemDialog (options, this));
			}
		}
			public override void Perform (Script script)
			{
				List<Segment> segments = new List<Segment> ();
				foreach (var action in script.Actions) {
					if (action == this)
						break;
					var noa = action as NodeOutputAction;
					if (noa == null)
						continue;
					foreach (var astNode in Linked) {
						NodeOutput.Segment segment;
						if (noa.NodeOutput.NodeSegments.TryGetValue (astNode, out segment))
							segments.Add (new Segment (noa.Offset + segment.Offset, segment.Length));
					}
				}
				segments.Sort ((x, y) => x.Offset.CompareTo (y.Offset));
				var link = new TextLink ("name");
				segments.ForEach (s => link.AddLink (s));
				var links = new List<TextLink> ();
				links.Add (link);
				var tle = new TextLinkEditMode (ctx.Document.Editor.Parent, 0, links);
				tle.SetCaretPosition = false;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = ctx.Document.Editor.CurrentMode;
					tle.StartMode ();
					ctx.Document.Editor.CurrentMode = tle;
				}
			}
		public override void Link (Action continuation, params AstNode[] nodes)
		{
			var segments = new List<TextSegment> (nodes.Select (node => new TextSegment (GetSegment (node))).OrderBy (s => s.Offset));
			
			var link = new TextLink ("name");
			segments.ForEach (s => link.AddLink (s));
			var links = new List<TextLink> ();
			links.Add (link);
			var tle = new TextLinkEditMode (context.TextEditor.Parent, 0, links);
			tle.SetCaretPosition = false;
			if (tle.ShouldStartTextLinkMode) {
				operationsRunning++;
				context.TextEditor.Caret.Offset = segments [0].EndOffset;
				tle.OldMode = context.TextEditor.CurrentMode;
				tle.Cancel += (sender, e) => Rollback ();
				tle.Exited += (sender, e) => DisposeOnClose (); 
				tle.StartMode ();
				context.TextEditor.CurrentMode = tle;
				if (IdeApp.Workbench.ActiveDocument != null)
					IdeApp.Workbench.ActiveDocument.ReparseDocument ();
			}
			if (continuation != null)
				continuation ();
		}
示例#34
0
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
		{
			using (var undo = Document.OpenUndoGroup ()) {
				var result = template.InsertTemplateContents (document);

				var links = result.TextLinks;
				if (PropertyService.Get ("OnTheFlyFormatting", true)) {
					var prettyPrinter = CodeFormatterService.GetFormatter (Document.MimeType);
					if (prettyPrinter != null) {
						int endOffset = result.InsertPosition + result.Code.Length;
						//						string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
						//						var policies = document.Project != null ? document.Project.Policies : null;
						var oldVersion = Document.Version;
						prettyPrinter.OnTheFlyFormat (document, result.InsertPosition, endOffset);
						foreach (var textLink in links) {
							for (int i = 0; i < textLink.Links.Count; i++) {
								var segment = textLink.Links [i];
								var translatedOffset = oldVersion.MoveOffsetTo (Document.Version, result.InsertPosition + segment.Offset) - result.InsertPosition;
								textLink.Links [i] = new TextSegment (translatedOffset, segment.Length);
							}
						}
					}
				}

				var tle = new TextLinkEditMode (this, result.InsertPosition, links);
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = CurrentMode;
					tle.StartMode ();
					CurrentMode = tle;
				}
			}
		}
		internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
		{
			using (var undo = Document.OpenUndoGroup ()) {
				var result = template.InsertTemplateContents (document);

				var links = result.TextLinks;

				var tle = new TextLinkEditMode (this, result.InsertPosition, links);
				tle.TextLinkMode = TextLinkMode.General;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = CurrentMode;
					tle.StartMode ();
					CurrentMode = tle;
				}
			}
		}
		public override void Run (RefactoringOptions options)
		{
			base.Run (options);
			if (selectionEnd >= 0) {
				options.Document.Editor.Caret.Offset = selectionEnd;
				options.Document.Editor.SetSelection (selectionStart, selectionEnd);
			} else {
				TextEditorData data = options.GetTextEditorData ();
				Mono.TextEditor.TextEditor editor = data.Parent;
				TextLink link = new TextLink ("name");
				if (varName != null) {
					if (insertOffset >= 0) {
						link.AddLink (new Segment (insertOffset - selectionStart, varName.Length));
					} else {
						LoggingService.LogWarning ("insert offset not found.");
					}
					if (replaceOffset >= 0)
						link.AddLink (new Segment (replaceOffset - selectionStart, varName.Length));
				}
				List<TextLink> links = new List<TextLink> ();
				links.Add (link);
				TextLinkEditMode tle = new TextLinkEditMode (editor, selectionStart, links);
				tle.SetCaretPosition = false;
				if (tle.ShouldStartTextLinkMode) {
					tle.OldMode = data.CurrentMode;
					tle.StartMode ();
					data.CurrentMode = tle;
				}
			}
		}
		public override void Link (params AstNode[] nodes)
		{
			var segments = new List<TextSegment> (nodes.Select (node => new TextSegment (GetSegment (node))).OrderBy (s => s.Offset));

			var link = new TextLink ("name");
			segments.ForEach (s => link.AddLink (s));
			var links = new List<TextLink> ();
			links.Add (link);
			var tle = new TextLinkEditMode (document.Editor.Parent, 0, links);
			tle.SetCaretPosition = false;
			if (tle.ShouldStartTextLinkMode) {
				document.Editor.Caret.Offset = segments [0].EndOffset;
				tle.OldMode = document.Editor.CurrentMode;
				tle.StartMode ();
				document.Editor.CurrentMode = tle;
			}
		}
示例#38
0
        public override void Run(RefactoringOptions options)
        {
            if (options.SelectedItem is IVariable)
            {
                var field = options.SelectedItem as IField;
                if (field != null && (field.Accessibility != Accessibility.Private || field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Parts.Count > 1))
                {
                    using (var dlg = new RenameItemDialog(options, this))
                        MessageService.ShowCustomDialog(dlg);
                    return;
                }

                var par = options.SelectedItem as IParameter;
                if (par != null && par.Owner != null && (par.Owner.Accessibility != Accessibility.Private || par.Owner.DeclaringTypeDefinition != null && par.Owner.DeclaringTypeDefinition.Parts.Count > 1))
                {
                    using (var dlg = new RenameItemDialog(options, this))
                        MessageService.ShowCustomDialog(dlg);
                    return;
                }

                var col = ReferenceFinder.FindReferences(options.SelectedItem, true);
                if (col == null)
                {
                    return;
                }
                var data = options.Document != null?options.GetTextEditorData() : IdeApp.Workbench.ActiveDocument.Editor;

                var editor = data.Parent;
                if (editor == null)
                {
                    using (var dlg = new RenameItemDialog(options, this))
                        MessageService.ShowCustomDialog(dlg);
                    return;
                }

                var links      = new List <TextLink> ();
                var link       = new TextLink("name");
                int baseOffset = Int32.MaxValue;
                foreach (var r in col)
                {
                    baseOffset = Math.Min(baseOffset, r.Offset);
                }
                foreach (MemberReference r in col)
                {
                    var segment = new TextSegment(r.Offset - baseOffset, r.Length);
                    if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset)
                    {
                        link.Links.Insert(0, segment);
                    }
                    else
                    {
                        link.AddLink(segment);
                    }
                }

                links.Add(link);
                if (editor.CurrentMode is TextLinkEditMode)
                {
                    ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode();
                }
                TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links);
                tle.SetCaretPosition  = false;
                tle.SelectPrimaryLink = true;
                if (tle.ShouldStartTextLinkMode)
                {
                    tle.Cancel += delegate {
                        if (tle.HasChangedText)
                        {
                            editor.Document.Undo();
                        }
                    };
                    tle.OldMode = data.CurrentMode;
                    tle.StartMode();
                    data.CurrentMode = tle;
                }
            }
            else
            {
                using (var dlg = new RenameItemDialog(options, this))
                    MessageService.ShowCustomDialog(dlg);
            }
        }