提交 80d1f6ec 编写于 作者: S Shyam N

Fix an issue that was causing the rename tracking fix to offer up empty preview.

In cases where primary document from which the rename tracking fix was triggered doesn't have any changes (aside from the rename which is already in the buffer), we would end up offering no preview. This change ensures that we will go on and return preview for some other document that did have changes in such cases.
上级 88447d0e
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Editor.Implementation.Suggestions;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text.Differencing;
using Roslyn.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings
......@@ -53,7 +50,7 @@ private void GetPreview(TestWorkspace workspace, CodeRefactoringProvider provide
VisualStudio.Text.ITextBuffer textBuffer;
RefactoringSetup(workspace, provider, refactorings, out editHandler, out extensionManager, out textBuffer);
var suggestedAction = new CodeRefactoringSuggestedAction(workspace, textBuffer, editHandler, refactorings.First(), provider);
suggestedAction.GetPreview(CancellationToken.None);
suggestedAction.GetPreviewAsync(CancellationToken.None).PumpingWaitResult();
Assert.True(extensionManager.IsDisabled(provider));
Assert.False(extensionManager.IsIgnored(provider));
}
......@@ -79,7 +76,7 @@ private void ActionSets(TestWorkspace workspace, CodeRefactoringProvider provide
VisualStudio.Text.ITextBuffer textBuffer;
RefactoringSetup(workspace, provider, refactorings, out editHandler, out extensionManager, out textBuffer);
var suggestedAction = new CodeRefactoringSuggestedAction(workspace, textBuffer, editHandler, refactorings.First(), provider);
var actionSets = suggestedAction.ActionSets;
var actionSets = suggestedAction.GetActionSetsAsync(CancellationToken.None).PumpingWaitResult();
Assert.True(extensionManager.IsDisabled(provider));
Assert.False(extensionManager.IsIgnored(provider));
}
......
......@@ -4,11 +4,11 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
namespace Microsoft.CodeAnalysis.Editor
{
internal class SolutionPreviewResult
internal class SolutionPreviewResult : ForegroundThreadAffinitizedObject
{
private IList<SolutionPreviewItem> _previews = null;
public readonly SolutionChangeSummary ChangeSummary;
......@@ -32,13 +32,15 @@ public bool IsEmpty
/// This function guarantees that it will not return the same preview object twice if called twice
/// (thereby reducing the possibility that a given preview object can end up with more than one owner).
/// </remarks>
public Task<object> TakeNextPreviewAsync(DocumentId preferredDocumentId = null, ProjectId preferredProjectId = null, CancellationToken cancellationToken = default(CancellationToken))
public async Task<object> TakeNextPreviewAsync(DocumentId preferredDocumentId = null, ProjectId preferredProjectId = null, CancellationToken cancellationToken = default(CancellationToken))
{
AssertIsForeground();
cancellationToken.ThrowIfCancellationRequested();
if (IsEmpty)
{
return SpecializedTasks.Default<object>();
return null;
}
SolutionPreviewItem previewItem = null;
......@@ -68,7 +70,23 @@ public Task<object> TakeNextPreviewAsync(DocumentId preferredDocumentId = null,
// object can end up with more than one owner - see <remarks> above).
_previews.Remove(previewItem);
return previewItem.LazyPreview(cancellationToken);
// We use ConfigureAwait(true) to stay on the UI thread.
var preview = await previewItem.LazyPreview(cancellationToken).ConfigureAwait(true);
if (preview == null)
{
// Keep going if preview is null. Null preview indicates that although the preferred document was marked as changed,
// there are no textual changes in the preferred document and so we can't create a diff preview for this docuement.
// This can happen in the case of the 'rename tracking' code fix - the document where the fix was triggered from (i.e.
// the preferred document) is always reported as changed (on account of difference in docuemnt version). However, if
// the renamed identifier is not referenced from any other location in this document, then there will be no text changes
// between the two versions. In such cases, We should keep going until we find a document with text changes that can be
// diffed and previewed.
// There is no danger of infinite recursion here since we remove null previews from the list each time.
preview = await TakeNextPreviewAsync(preferredDocumentId, preferredProjectId, cancellationToken).ConfigureAwait(true);
}
return preview;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册