Esempio n. 1
0
        /// <param name="textSpan">If <see langword="null"/> the current caret position will be used.</param>
        public async Task <CodeAction> TryGetCodeActionAsync(TextSpan?textSpan = null)
        {
            var span = textSpan ?? new TextSpan(_view.Caret.Position.BufferPosition, 0);

            var document = this.Workspace.CurrentSolution.GetDocument(_hostDocument.Id);

            var actions = new List <CodeAction>();
            var context = new CodeRefactoringContext(
                document, span, actions.Add, CancellationToken.None);
            await _codeRefactoringProvider.ComputeRefactoringsAsync(context);

            return(actions.SingleOrDefault());
        }
        public async Task AssertTag(string expectedFromName, string expectedToName, bool invokeAction = false)
        {
            await WaitForAsyncOperationsAsync();

            var tags = _tagger.GetTags(_view.TextBuffer.CurrentSnapshot.GetSnapshotSpanCollection());

            // There should only ever be one tag
            Assert.Equal(1, tags.Count());

            var tag = tags.Single();

            var document   = this.Workspace.CurrentSolution.GetDocument(_hostDocument.Id);
            var diagnostic = await TryGetDocumentDiagnosticAsync(document);

            // There should be a single rename tracking diagnostic
            Assert.NotNull(diagnostic);
            Assert.Equal(RenameTrackingCodeRefactoringProvider.DiagnosticId, diagnostic.Id);

            var actions = new List <CodeAction>();
            var context = new CodeRefactoringContext(
                document, diagnostic.Location.SourceSpan, actions.Add, CancellationToken.None);
            await _codeRefactoringProvider.ComputeRefactoringsAsync(context);

            // There should only be one code action
            Assert.Equal(1, actions.Count);

            Assert.Equal(string.Format(EditorFeaturesResources.Rename_0_to_1, expectedFromName, expectedToName), actions[0].Title);

            if (invokeAction)
            {
                var operations = (await actions[0].GetOperationsAsync(CancellationToken.None)).ToArray();
                Assert.Equal(1, operations.Length);

                operations[0].TryApply(this.Workspace, new ProgressTracker(), CancellationToken.None);
            }
        }