コード例 #1
0
            async Task TryStartRenameSession(Workspace workspace, Solution oldSolution, Solution newSolution, CancellationToken cancellationToken)
            {
                var changedDocuments = GetChangedDocuments(newSolution, oldSolution);

                foreach (var documentId in changedDocuments)
                {
                    var document = newSolution.GetDocument(documentId);
                    var root     = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                    SyntaxToken?renameTokenOpt = root.GetAnnotatedNodesAndTokens(RenameAnnotation.Kind)
                                                 .Where(s => s.IsToken)
                                                 .Select(s => s.AsToken())
                                                 .Cast <SyntaxToken?> ()
                                                 .FirstOrDefault();
                    if (renameTokenOpt.HasValue)
                    {
                        var latestDocument = workspace.CurrentSolution.GetDocument(documentId);
                        var latestModel    = await latestDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                        var latestRoot = await latestDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                        await Runtime.RunInMainThread(async delegate {
                            try {
                                var node = latestRoot.FindNode(renameTokenOpt.Value.Parent.Span, false, false);
                                if (node == null)
                                {
                                    return;
                                }
                                var info = latestModel.GetSymbolInfo(node);
                                var sym  = info.Symbol ?? latestModel.GetDeclaredSymbol(node);
                                if (sym != null)
                                {
                                    await new Refactoring.Rename.RenameRefactoring().Rename(sym);
                                }
                                else
                                {
                                    var links = new List <TextLink> ();
                                    var link  = new TextLink("name");
                                    link.AddLink(new TextSegment(node.Span.Start, node.Span.Length));
                                    links.Add(link);
                                    var oldVersion = editor.Version;
                                    editor.StartTextLinkMode(new TextLinkModeOptions(links, (arg) => {
                                        //If user cancel renaming revert changes
                                        if (!arg.Success)
                                        {
                                            var textChanges = editor.Version.GetChangesTo(oldVersion).ToList();
                                            foreach (var change in textChanges)
                                            {
                                                foreach (var v in change.TextChanges.Reverse())
                                                {
                                                    editor.ReplaceText(v.Offset, v.RemovalLength, v.InsertedText);
                                                }
                                            }
                                        }
                                    })
                                    {
                                        TextLinkPurpose = TextLinkPurpose.Rename
                                    });
                                }
                            } catch (Exception ex) {
                                LoggingService.LogError("Error while renaming " + renameTokenOpt.Value.Parent, ex);
                            }
                        });

                        return;
                    }
                }
            }