Exemple #1
0
        public async Task Rename(ISymbol symbol)
        {
            var ws = IdeApp.Workbench.ActiveDocument.DocumentContext.RoslynWorkspace;

            if (!symbol.IsDefinedInSource())
            {
                return;
            }
            foreach (var location in symbol.Locations)
            {
                if (location.IsInSource && ws.CurrentSolution.GetDocument(location.SourceTree) == null)
                {
                    LoggingService.LogError("Error location.SourceTree document not found.");
                    MessageService.ShowError(GettextCatalog.GetString("Can't rename '{0}'. If a retry doesn't work please file a bug.", symbol.Name));
                    return;
                }
            }

            var currentSolution = ws.CurrentSolution;
            var cts             = new CancellationTokenSource();
            var newSolution     = await MessageService.ExecuteTaskAndShowWaitDialog(Task.Run(() => Renamer.RenameSymbolAsync(currentSolution, symbol, "_" + symbol.Name + "_", ws.Options, cts.Token)), GettextCatalog.GetString("Looking for all references"), cts);

            var projectChanges   = currentSolution.GetChanges(newSolution).GetProjectChanges().ToList();
            var changedDocuments = new List <string> ();

            foreach (var change in projectChanges)
            {
                foreach (var changedDoc in change.GetChangedDocuments())
                {
                    changedDocuments.Add(ws.CurrentSolution.GetDocument(changedDoc).FilePath);
                }
            }

            if (changedDocuments.Count > 1)
            {
                using (var dlg = new RenameItemDialog(symbol, this)) {
                    dlg.ChangedDocuments = changedDocuments;
                    MessageService.ShowCustomDialog(dlg);
                }
                return;
            }

            var projectChange = projectChanges [0];
            var changes       = projectChange.GetChangedDocuments().ToList();

            if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }
            var doc        = IdeApp.Workbench.ActiveDocument;
            var editor     = doc.Editor;
            var oldVersion = editor.Version;

            var links = await GetTextLinksAsync(doc, editor.CaretOffset, symbol);

            editor.StartTextLinkMode(new TextLinkModeOptions(links, (arg) => {
                //If user cancel renaming revert changes
                if (!arg.Success)
                {
                    var textChanges = editor.Version.GetChangesTo(oldVersion).ToList();
                    for (int i = textChanges.Count - 1; i >= 0; i--)
                    {
                        var change          = textChanges [i];
                        var rollbackChanges = new List <Microsoft.CodeAnalysis.Text.TextChange> ();
                        for (int j = 0; j < change.TextChanges.Count; j++)
                        {
                            var textChange = change.TextChanges [j];
                            rollbackChanges.Add(new Microsoft.CodeAnalysis.Text.TextChange(new TextSpan(textChange.Offset, textChange.InsertionLength), textChange.RemovedText.Text));
                        }
                        editor.ApplyTextChanges(rollbackChanges);
                    }
                }
            })
            {
                TextLinkPurpose = TextLinkPurpose.Rename
            });
        }
		public void Rename (ISymbol symbol)
		{

			var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
			var ws = TypeSystemService.GetWorkspace (solution);

			var currentSolution = ws.CurrentSolution;
			var newSolution = Renamer.RenameSymbolAsync (currentSolution, symbol, "_" + symbol.Name + "_", ws.Options).Result;
			var projectChanges = currentSolution.GetChanges (newSolution).GetProjectChanges ().ToList ();

			if (projectChanges.Count != 1) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}

			var projectChange = projectChanges [0];
			var changes = projectChange.GetChangedDocuments ().ToList ();
			if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}
			var doc = IdeApp.Workbench.ActiveDocument;
			var editor = doc.Editor;
			
			var links = new List<TextLink> ();
			var link = new TextLink ("name");

			var cd = changes [0];
			var oldDoc = projectChange.OldProject.GetDocument (cd);
			var newDoc = projectChange.NewProject.GetDocument (cd);
			var oldVersion = editor.Version;
			foreach (var textChange in oldDoc.GetTextChangesAsync (newDoc).Result) {
				var segment = new TextSegment (textChange.Span.Start, textChange.Span.Length);
				if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset) {
					link.Links.Insert (0, segment); 
				} else {
					link.AddLink (segment);
				}
			}
			
			links.Add (link);
			editor.StartTextLinkMode (new TextLinkModeOptions (links, args => {
				if (!args.Success)
					return;

				var version = editor.Version;
				var span = symbol.Locations.First ().SourceSpan;
				var newName = link.CurrentText;
				var textChanges = version.GetChangesTo (oldVersion).ToList ();
				foreach (var v in textChanges) {
					editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
				}
				var parsedDocument = doc.UpdateParseDocument ().Result;
				if (parsedDocument == null) {
					Rollback (editor, textChanges);
					return;
				}
				var model = parsedDocument.GetAst<SemanticModel> ();
				if (model == null) {
					Rollback (editor, textChanges);
					return;
				}
				var node = model.SyntaxTree.GetRoot ().FindNode (span);
				if (node == null) {
					Rollback (editor, textChanges);
					return;
				}
				var sym = model.GetDeclaredSymbol (node);
				if (sym == null) {
					Rollback (editor, textChanges);
					return;
				}
				if (!Rename (sym, newName))
					Rollback (editor, textChanges);
			}));
		}
        public async Task Rename(ISymbol symbol)
        {
            var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
            var ws       = TypeSystemService.GetWorkspace(solution);

            var currentSolution = ws.CurrentSolution;
            var cts             = new CancellationTokenSource();
            var newSolution     = await MessageService.ExecuteTaskAndShowWaitDialog(Task.Run(() => Renamer.RenameSymbolAsync(currentSolution, symbol, "_" + symbol.Name + "_", ws.Options, cts.Token)), GettextCatalog.GetString("Looking for all references"), cts);

            var projectChanges   = currentSolution.GetChanges(newSolution).GetProjectChanges().ToList();
            var changedDocuments = new HashSet <string> ();

            foreach (var change in projectChanges)
            {
                foreach (var changedDoc in change.GetChangedDocuments())
                {
                    changedDocuments.Add(ws.CurrentSolution.GetDocument(changedDoc).FilePath);
                }
            }

            if (changedDocuments.Count > 1)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }

            var projectChange = projectChanges [0];
            var changes       = projectChange.GetChangedDocuments().ToList();

            if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }
            var doc        = IdeApp.Workbench.ActiveDocument;
            var editor     = doc.Editor;
            var oldVersion = editor.Version;

            var links = new List <TextLink> ();
            var link  = new TextLink("name");

            var documents = ImmutableHashSet.Create(doc.AnalysisDocument);

            foreach (var loc in symbol.Locations)
            {
                if (loc.IsInSource && FilePath.PathComparer.Equals(loc.SourceTree.FilePath, doc.FileName))
                {
                    link.AddLink(new TextSegment(loc.SourceSpan.Start, loc.SourceSpan.Length));
                }
            }

            foreach (var mref in await SymbolFinder.FindReferencesAsync(symbol, TypeSystemService.Workspace.CurrentSolution, documents, default(CancellationToken)))
            {
                foreach (var loc in mref.Locations)
                {
                    TextSpan span   = loc.Location.SourceSpan;
                    var      root   = loc.Location.SourceTree.GetRoot();
                    var      node   = root.FindNode(loc.Location.SourceSpan);
                    var      trivia = root.FindTrivia(loc.Location.SourceSpan.Start);
                    if (!trivia.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia))
                    {
                        span = node.Span;
                    }
                    if (span.Start != loc.Location.SourceSpan.Start)
                    {
                        span = loc.Location.SourceSpan;
                    }
                    var segment = new TextSegment(span.Start, span.Length);
                    if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset)
                    {
                        link.Links.Insert(0, segment);
                    }
                    else
                    {
                        link.AddLink(segment);
                    }
                }
            }
            links.Add(link);

            editor.StartTextLinkMode(new TextLinkModeOptions(links, (arg) => {
                //If user cancel renaming revert changes
                if (!arg.Success)
                {
                    var textChanges = editor.Version.GetChangesTo(oldVersion).ToList();
                    foreach (var v in textChanges)
                    {
                        editor.ReplaceText(v.Offset, v.RemovalLength, v.InsertedText);
                    }
                }
            })
            {
                TextLinkPurpose = TextLinkPurpose.Rename
            });
        }
Exemple #4
0
        public void Rename(ISymbol symbol)
        {
            var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
            var ws       = TypeSystemService.GetWorkspace(solution);

            var currentSolution = ws.CurrentSolution;
            var newSolution     = Renamer.RenameSymbolAsync(currentSolution, symbol, "_" + symbol.Name + "_", ws.Options).Result;
            var projectChanges  = currentSolution.GetChanges(newSolution).GetProjectChanges().ToList();

            if (projectChanges.Count != 1)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }

            var projectChange = projectChanges [0];
            var changes       = projectChange.GetChangedDocuments().ToList();

            if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }
            var doc    = IdeApp.Workbench.ActiveDocument;
            var editor = doc.Editor;

            var links = new List <TextLink> ();
            var link  = new TextLink("name");

            var cd         = changes [0];
            var oldDoc     = projectChange.OldProject.GetDocument(cd);
            var newDoc     = projectChange.NewProject.GetDocument(cd);
            var oldVersion = editor.Version;

            foreach (var textChange in oldDoc.GetTextChangesAsync(newDoc).Result)
            {
                var segment = new TextSegment(textChange.Span.Start, textChange.Span.Length);
                if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset)
                {
                    link.Links.Insert(0, segment);
                }
                else
                {
                    link.AddLink(segment);
                }
            }

            links.Add(link);
            editor.StartTextLinkMode(new TextLinkModeOptions(links, args => {
                if (!args.Success)
                {
                    return;
                }

                var version     = editor.Version;
                var span        = symbol.Locations.First().SourceSpan;
                var newName     = link.CurrentText;
                var textChanges = version.GetChangesTo(oldVersion).ToList();
                foreach (var v in textChanges)
                {
                    editor.ReplaceText(v.Offset, v.RemovalLength, v.InsertedText);
                }
                var parsedDocument = doc.UpdateParseDocument().Result;
                if (parsedDocument == null)
                {
                    Rollback(editor, textChanges);
                    return;
                }
                var model = parsedDocument.GetAst <SemanticModel> ();
                if (model == null)
                {
                    Rollback(editor, textChanges);
                    return;
                }
                var node = model.SyntaxTree.GetRoot().FindNode(span);
                if (node == null)
                {
                    Rollback(editor, textChanges);
                    return;
                }
                var sym = model.GetDeclaredSymbol(node);
                if (sym == null)
                {
                    Rollback(editor, textChanges);
                    return;
                }
                if (!Rename(sym, newName))
                {
                    Rollback(editor, textChanges);
                }
            }));
        }
Exemple #5
0
        public async Task Rename(ISymbol symbol)
        {
            var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
            var ws       = TypeSystemService.GetWorkspace(solution);

            var currentSolution = ws.CurrentSolution;
            var cts             = new CancellationTokenSource();
            var newSolution     = await MessageService.ExecuteTaskAndShowWaitDialog(Task.Run(() => Renamer.RenameSymbolAsync(currentSolution, symbol, "_" + symbol.Name + "_", ws.Options, cts.Token)), GettextCatalog.GetString("Looking for all references"), cts);

            var projectChanges   = currentSolution.GetChanges(newSolution).GetProjectChanges().ToList();
            var changedDocuments = new HashSet <string> ();

            foreach (var change in projectChanges)
            {
                foreach (var changedDoc in change.GetChangedDocuments())
                {
                    changedDocuments.Add(ws.CurrentSolution.GetDocument(changedDoc).FilePath);
                }
            }

            if (changedDocuments.Count > 1)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }

            var projectChange = projectChanges [0];
            var changes       = projectChange.GetChangedDocuments().ToList();

            if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType)
            {
                using (var dlg = new RenameItemDialog(symbol, this))
                    MessageService.ShowCustomDialog(dlg);
                return;
            }
            var doc    = IdeApp.Workbench.ActiveDocument;
            var editor = doc.Editor;

            var links = new List <TextLink> ();
            var link  = new TextLink("name");

            var cd         = changes [0];
            var oldDoc     = projectChange.OldProject.GetDocument(cd);
            var newDoc     = projectChange.NewProject.GetDocument(cd);
            var oldVersion = editor.Version;

            foreach (var textChange in await oldDoc.GetTextChangesAsync(newDoc))
            {
                var segment = new TextSegment(textChange.Span.Start, textChange.Span.Length);
                if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset)
                {
                    link.Links.Insert(0, segment);
                }
                else
                {
                    link.AddLink(segment);
                }
            }

            links.Add(link);
            editor.StartTextLinkMode(new TextLinkModeOptions(links, (arg) => {
                //If user cancel renaming revert changes
                if (!arg.Success)
                {
                    var textChanges = editor.Version.GetChangesTo(oldVersion).ToList();
                    foreach (var v in textChanges)
                    {
                        editor.ReplaceText(v.Offset, v.RemovalLength, v.InsertedText);
                    }
                }
            }));
        }
		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 void Rename (ISymbol symbol)
		{

			var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
			var ws = TypeSystemService.GetWorkspace (solution);

			var currentSolution = ws.CurrentSolution;
			var newSolution = Renamer.RenameSymbolAsync (currentSolution, symbol, "_" + symbol.Name + "_", ws.Options).Result;
			var projectChanges = currentSolution.GetChanges (newSolution).GetProjectChanges ().ToList ();
			var changedDocuments = new HashSet<string> ();
			foreach (var change in projectChanges)
				foreach (var changedDoc in change.GetChangedDocuments ()) {
					changedDocuments.Add (ws.CurrentSolution.GetDocument (changedDoc).FilePath);
				}
			if (changedDocuments.Count > 1) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}

			var projectChange = projectChanges [0];
			var changes = projectChange.GetChangedDocuments ().ToList ();
			if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}
			var doc = IdeApp.Workbench.ActiveDocument;
			var editor = doc.Editor;
			
			var links = new List<TextLink> ();
			var link = new TextLink ("name");

			var cd = changes [0];
			var oldDoc = projectChange.OldProject.GetDocument (cd);
			var newDoc = projectChange.NewProject.GetDocument (cd);
			var oldVersion = editor.Version;
			foreach (var textChange in oldDoc.GetTextChangesAsync (newDoc).Result) {
				var segment = new TextSegment (textChange.Span.Start, textChange.Span.Length);
				if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset) {
					link.Links.Insert (0, segment); 
				} else {
					link.AddLink (segment);
				}
			}

			links.Add (link);
			editor.StartTextLinkMode (new TextLinkModeOptions (links, (arg) => {
				//If user cancel renaming revert changes
				if (!arg.Success) {
					var textChanges = editor.Version.GetChangesTo (oldVersion).ToList ();
					foreach (var v in textChanges) {
						editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
					}
				}
			}));
		}
		public async Task Rename (ISymbol symbol)
		{
			var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
			var ws = TypeSystemService.GetWorkspace (solution);

			var currentSolution = ws.CurrentSolution;
			var cts = new CancellationTokenSource ();
			var newSolution = await MessageService.ExecuteTaskAndShowWaitDialog (Task.Run (() => Renamer.RenameSymbolAsync (currentSolution, symbol, "_" + symbol.Name + "_", ws.Options, cts.Token)), GettextCatalog.GetString ("Looking for all references"), cts);
			var projectChanges = currentSolution.GetChanges (newSolution).GetProjectChanges ().ToList ();
			var changedDocuments = new HashSet<string> ();
			foreach (var change in projectChanges) {
				foreach (var changedDoc in change.GetChangedDocuments ()) {
					changedDocuments.Add (ws.CurrentSolution.GetDocument (changedDoc).FilePath);
				}
			}

			if (changedDocuments.Count > 1) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}

			var projectChange = projectChanges [0];
			var changes = projectChange.GetChangedDocuments ().ToList ();
			if (changes.Count != 1 || symbol.Kind == SymbolKind.NamedType) {
				using (var dlg = new RenameItemDialog (symbol, this))
					MessageService.ShowCustomDialog (dlg);
				return;
			}
			var doc = IdeApp.Workbench.ActiveDocument;
			var editor = doc.Editor;
			var oldVersion = editor.Version;
			
			var links = new List<TextLink> ();
			var link = new TextLink ("name");

			var documents = ImmutableHashSet.Create (doc.AnalysisDocument);

			foreach (var loc in symbol.Locations) {
				if (loc.IsInSource && FilePath.PathComparer.Equals (loc.SourceTree.FilePath, doc.FileName)) {
					link.AddLink (new TextSegment (loc.SourceSpan.Start, loc.SourceSpan.Length));
				}
			}

			foreach (var mref in await SymbolFinder.FindReferencesAsync (symbol, TypeSystemService.Workspace.CurrentSolution, documents, default(CancellationToken))) {
				foreach (var loc in mref.Locations) {
					TextSpan span = loc.Location.SourceSpan;
					var root = loc.Location.SourceTree.GetRoot ();
					var node = root.FindNode (loc.Location.SourceSpan);
					var trivia = root.FindTrivia (loc.Location.SourceSpan.Start);
					if (!trivia.IsKind (SyntaxKind.SingleLineDocumentationCommentTrivia)) {
						span = node.Span;
					}
					if (span.Start != loc.Location.SourceSpan.Start) {
						span = loc.Location.SourceSpan;
					}
					var segment = new TextSegment (span.Start, span.Length);
					if (segment.Offset <= editor.CaretOffset && editor.CaretOffset <= segment.EndOffset) {
						link.Links.Insert (0, segment);
					} else {
						link.AddLink (segment);
					}
				}
			}
			links.Add (link);

			editor.StartTextLinkMode (new TextLinkModeOptions (links, (arg) => {
				//If user cancel renaming revert changes
				if (!arg.Success) {
					var textChanges = editor.Version.GetChangesTo (oldVersion).ToList ();
					foreach (var v in textChanges) {
						editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
					}
				}
			}));
		}
        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);
            }
        }