Skip to content

Commit 5b8b628

Browse files
Provide true-async and true-sync versions of GetTextChanges
1 parent d35882b commit 5b8b628

File tree

1 file changed

+20
-8
lines changed
  • src/Workspaces/Core/Portable/Workspace/Solution

1 file changed

+20
-8
lines changed

src/Workspaces/Core/Portable/Workspace/Solution/Document.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,28 @@ public Document WithFilePath(string? filePath)
416416
=> this.Project.Solution.WithDocumentFilePath(this.Id, filePath).GetRequiredDocument(Id);
417417

418418
/// <summary>
419-
/// Get the text changes between this document and a prior version of the same document.
420-
/// The changes, when applied to the text of the old document, will produce the text of the current document.
419+
/// Get the text changes between this document and a prior version of the same document. The changes, when applied
420+
/// to the text of the old document, will produce the text of the current document.
421421
/// </summary>
422-
public Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocument, CancellationToken cancellationToken = default)
422+
public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocument, CancellationToken cancellationToken = default)
423423
{
424-
return Task.FromResult<IEnumerable<TextChange>>(GetTextChangesSynchronously(oldDocument, cancellationToken));
424+
return await GetTextChangesAsync(useAsync: true, oldDocument, cancellationToken).ConfigureAwait(false);
425425
}
426426

427+
/// <summary>
428+
/// Similar to <see cref="GetTextChangesAsync(Document, CancellationToken)"/>, but should be used when in a forced
429+
/// synchronous context.
430+
/// </summary>
427431
internal ImmutableArray<TextChange> GetTextChangesSynchronously(
428432
Document oldDocument, CancellationToken cancellationToken)
433+
{
434+
// Should always complete synchronously since we passed in 'useAsync: false'
435+
var result = GetTextChangesAsync(useAsync: false, oldDocument, cancellationToken);
436+
return result.VerifyCompleted();
437+
}
438+
439+
private async Task<ImmutableArray<TextChange>> GetTextChangesAsync(
440+
bool useAsync, Document oldDocument, CancellationToken cancellationToken)
429441
{
430442
try
431443
{
@@ -462,16 +474,16 @@ internal ImmutableArray<TextChange> GetTextChangesSynchronously(
462474
// get changes by diffing the trees
463475
if (this.SupportsSyntaxTree)
464476
{
465-
var tree = this.GetSyntaxTreeSynchronously(cancellationToken);
466-
var oldTree = oldDocument.GetSyntaxTreeSynchronously(cancellationToken);
477+
var tree = useAsync ? await GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false) : this.GetSyntaxTreeSynchronously(cancellationToken);
478+
var oldTree = useAsync ? await oldDocument.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false) : oldDocument.GetSyntaxTreeSynchronously(cancellationToken);
467479

468480
RoslynDebug.Assert(tree is object);
469481
RoslynDebug.Assert(oldTree is object);
470482
return tree.GetChanges(oldTree).ToImmutableArray();
471483
}
472484

473-
text = this.GetTextSynchronously(cancellationToken);
474-
oldText = oldDocument.GetTextSynchronously(cancellationToken);
485+
text = useAsync ? await this.GetTextAsync(cancellationToken).ConfigureAwait(false) : this.GetTextSynchronously(cancellationToken);
486+
oldText = useAsync ? await oldDocument.GetTextAsync(cancellationToken).ConfigureAwait(false) : oldDocument.GetTextSynchronously(cancellationToken);
475487

476488
return text.GetTextChanges(oldText).ToImmutableArray();
477489
}

0 commit comments

Comments
 (0)