Ejemplo n.º 1
0
        public async Task TestPickTheRightPreview_NoPreference()
        {
            using (var workspace = await CreateWorkspaceFromFileAsync("class D {}", null, null))
            {
                Document document = null;
                SolutionPreviewResult previews = null;
                GetMainDocumentAndPreviews(workspace, out document, out previews);

                // The changed document comes first.
                var previewObjects = await previews.GetPreviewsAsync();

                var preview = previewObjects[0];
                Assert.NotNull(preview);
                Assert.True(preview is IWpfDifferenceViewer);
                var diffView = preview as IWpfDifferenceViewer;
                var text     = diffView.RightView.TextBuffer.AsTextContainer().CurrentText.ToString();
                Assert.Equal(ChangedDocumentText, text);
                diffView.Close();

                // Then comes the removed metadata reference.
                preview = previewObjects[1];
                Assert.NotNull(preview);
                Assert.True(preview is string);
                text = preview as string;
                Assert.Contains(s_removedMetadataReferenceDisplayName, text, StringComparison.Ordinal);

                // And finally the added project.
                preview = previewObjects[2];
                Assert.NotNull(preview);
                Assert.True(preview is string);
                text = preview as string;
                Assert.Contains(AddedProjectName, text, StringComparison.Ordinal);
            }
        }
Ejemplo n.º 2
0
        public async Task TestPickTheRightPreview_WithPreference()
        {
            using (var workspace = CreateWorkspaceFromFile("class D {}", null, null))
            {
                Document document = null;
                SolutionPreviewResult previews = null;
                GetMainDocumentAndPreviews(workspace, out document, out previews);

                // Should return preview that matches the preferred (added) project.
                var preview = await previews.TakeNextPreviewAsync(preferredProjectId : s_addedProjectId).ConfigureAwait(true);

                Assert.NotNull(preview);
                Assert.True(preview is string);
                var text = preview as string;
                Assert.Contains(AddedProjectName, text, StringComparison.Ordinal);

                // Should return preview that matches the preferred (changed) document.
                preview = await previews.TakeNextPreviewAsync(preferredDocumentId : document.Id).ConfigureAwait(true);

                Assert.NotNull(preview);
                Assert.True(preview is IWpfDifferenceViewer);
                var diffView = preview as IWpfDifferenceViewer;
                text = diffView.RightView.TextBuffer.AsTextContainer().CurrentText.ToString();
                Assert.Equal(ChangedDocumentText, text);
                diffView.Close();

                // There is no longer a preview for the preferred project. Should return the first remaining preview.
                preview = await previews.TakeNextPreviewAsync(preferredProjectId : s_addedProjectId).ConfigureAwait(true);

                Assert.NotNull(preview);
                Assert.True(preview is IWpfDifferenceViewer);
                diffView = preview as IWpfDifferenceViewer;
                text     = diffView.RightView.TextBuffer.AsTextContainer().CurrentText.ToString();
                Assert.Contains(AddedDocumentName, text, StringComparison.Ordinal);
                Assert.Contains(AddedDocumentText, text, StringComparison.Ordinal);
                diffView.Close();

                // There is no longer a preview for the  preferred document. Should return the first remaining preview.
                preview = await previews.TakeNextPreviewAsync(preferredDocumentId : document.Id).ConfigureAwait(true);

                Assert.NotNull(preview);
                Assert.True(preview is string);
                text = preview as string;
                Assert.Contains(s_removedMetadataReferenceDisplayName, text, StringComparison.Ordinal);

                // There are no more previews.
                preview = await previews.TakeNextPreviewAsync().ConfigureAwait(true);

                Assert.Null(preview);
                preview = await previews.TakeNextPreviewAsync().ConfigureAwait(true);

                Assert.Null(preview);
            }
        }
Ejemplo n.º 3
0
 private void GetMainDocumentAndPreviews(TestWorkspace workspace, out Document document, out SolutionPreviewResult previews)
 {
     document = GetDocument(workspace);
     var provider = CreateCodeRefactoringProvider(workspace) as CodeRefactoringProvider;
     var span = document.GetSyntaxRootAsync().Result.Span;
     var refactorings = new List<CodeAction>();
     var context = new CodeRefactoringContext(document, span, (a) => refactorings.Add(a), CancellationToken.None);
     provider.ComputeRefactoringsAsync(context).Wait();
     var action = refactorings.Single();
     var editHandler = workspace.ExportProvider.GetExportedValue<ICodeActionEditHandlerService>();
     previews = editHandler.GetPreviews(workspace, action.GetPreviewOperationsAsync(CancellationToken.None).Result, CancellationToken.None);
 }
Ejemplo n.º 4
0
        public SolutionPreviewResult GetPreviews(
            Workspace workspace, ImmutableArray <CodeActionOperation> operations, CancellationToken cancellationToken)
        {
            if (operations.IsDefaultOrEmpty)
            {
                return(null);
            }

            SolutionPreviewResult currentResult = null;

            foreach (var op in operations)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var applyChanges = op as ApplyChangesOperation;
                if (applyChanges != null)
                {
                    var oldSolution = workspace.CurrentSolution;
                    var newSolution = applyChanges.ChangedSolution.WithMergedLinkedFileChangesAsync(oldSolution, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
                    var preview     = _previewService.GetSolutionPreviews(
                        oldSolution, newSolution, cancellationToken);

                    if (preview != null && !preview.IsEmpty)
                    {
                        currentResult = SolutionPreviewResult.Merge(currentResult, preview);
                        continue;
                    }
                }

                var previewOp = op as PreviewOperation;
                if (previewOp != null)
                {
                    currentResult = SolutionPreviewResult.Merge(currentResult,
                                                                new SolutionPreviewResult(new SolutionPreviewItem(
                                                                                              projectId: null, documentId: null,
                                                                                              lazyPreview: c => previewOp.GetPreviewAsync(c))));
                    continue;
                }

                var title = op.Title;

                if (title != null)
                {
                    currentResult = SolutionPreviewResult.Merge(currentResult,
                                                                new SolutionPreviewResult(new SolutionPreviewItem(
                                                                                              projectId: null, documentId: null, text: title)));
                    continue;
                }
            }

            return(currentResult);
        }
Ejemplo n.º 5
0
        public async Task <SolutionPreviewResult?> GetPreviewsAsync(
            Workspace workspace, ImmutableArray <CodeActionOperation> operations, CancellationToken cancellationToken)
        {
            if (operations.IsDefaultOrEmpty)
            {
                return(null);
            }

            SolutionPreviewResult?currentResult = null;

            foreach (var op in operations)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (op is ApplyChangesOperation applyChanges)
                {
                    var oldSolution = workspace.CurrentSolution;
                    var newSolution = await applyChanges.ChangedSolution.WithMergedLinkedFileChangesAsync(
                        oldSolution, cancellationToken : cancellationToken).ConfigureAwait(false);

                    var preview = _previewService.GetSolutionPreviews(
                        oldSolution, newSolution, cancellationToken);

                    if (preview != null && !preview.IsEmpty)
                    {
                        currentResult = SolutionPreviewResult.Merge(currentResult, preview);
                        continue;
                    }
                }

                if (op is PreviewOperation previewOp)
                {
                    currentResult = SolutionPreviewResult.Merge(currentResult,
                                                                new SolutionPreviewResult(ThreadingContext, new SolutionPreviewItem(
                                                                                              projectId: null, documentId: null,
                                                                                              lazyPreview: c => previewOp.GetPreviewAsync(c))));
                    continue;
                }

                var title = op.Title;

                if (title != null)
                {
                    currentResult = SolutionPreviewResult.Merge(currentResult,
                                                                new SolutionPreviewResult(ThreadingContext, new SolutionPreviewItem(
                                                                                              projectId: null, documentId: null, text: title)));
                    continue;
                }
            }

            return(currentResult);
        }
Ejemplo n.º 6
0
        public void TestPickTheRightPreview_NoPreference()
        {
            using (var workspace = CreateWorkspaceFromFile("class D {}", null, null))
            {
                Document document = null;
                SolutionPreviewResult previews = null;
                GetMainDocumentAndPreviews(workspace, out document, out previews);

                // The changed document comes first.
                var preview = previews.TakeNextPreview();
                Assert.NotNull(preview);
                Assert.True(preview is IWpfDifferenceViewer);
                var diffView = preview as IWpfDifferenceViewer;
                var text     = diffView.RightView.TextBuffer.AsTextContainer().CurrentText.ToString();
                Assert.Equal(ChangedDocumentText, text);
                diffView.Close();

                // The added document comes next.
                preview = previews.TakeNextPreview();
                Assert.NotNull(preview);
                Assert.True(preview is IWpfDifferenceViewer);
                diffView = preview as IWpfDifferenceViewer;
                text     = diffView.RightView.TextBuffer.AsTextContainer().CurrentText.ToString();
                Assert.Contains(AddedDocumentName, text);
                Assert.Contains(AddedDocumentText, text);
                diffView.Close();

                // Then comes the removed metadata reference.
                preview = previews.TakeNextPreview();
                Assert.NotNull(preview);
                Assert.True(preview is string);
                text = preview as string;
                Assert.Contains(s_removedMetadataReferenceDisplayName, text);

                // And finally the added project.
                preview = previews.TakeNextPreview();
                Assert.NotNull(preview);
                Assert.True(preview is string);
                text = preview as string;
                Assert.Contains(AddedProjectName, text);

                // There are no more previews.
                preview = previews.TakeNextPreview();
                Assert.Null(preview);
                preview = previews.TakeNextPreview();
                Assert.Null(preview);
            }
        }
Ejemplo n.º 7
0
        private void GetMainDocumentAndPreviews(TestWorkspace workspace, out Document document, out SolutionPreviewResult previews)
        {
            document         = GetDocument(workspace);
            var provider     = CreateCodeRefactoringProvider(workspace) as CodeRefactoringProvider;
            var span         = document.GetSyntaxRootAsync().Result.Span;
            var refactorings = new List <CodeAction>();
            var context      = new CodeRefactoringContext(document, span, (a) => refactorings.Add(a), CancellationToken.None);

            provider.ComputeRefactoringsAsync(context).Wait();
            var action      = refactorings.Single();
            var editHandler = workspace.ExportProvider.GetExportedValue <ICodeActionEditHandlerService>();

            previews = editHandler.GetPreviews(workspace, action.GetPreviewOperationsAsync(CancellationToken.None).Result, CancellationToken.None);
        }