Skip to content

Commit a4324e6

Browse files
Remove sync over async blocking in workspace (#76491)
2 parents dd2ffb1 + 6b397a5 commit a4324e6

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

src/EditorFeatures/VisualBasic/EndConstructGeneration/EndConstructCommandHandler.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EndConstructGeneration
142142

143143
Dim options = buffer.GetCodeCleanupOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), document.Project.Services, explicitFormat:=False, allowImportsInHiddenRegions:=document.AllowImportsInHiddenRegions())
144144
Dim cleanDocument = CodeCleaner.CleanupAsync(document, GetSpanToCleanup(statement), Options, codeCleanups, cancellationToken:=cancellationToken).WaitAndGetResult(cancellationToken)
145-
Dim changes = cleanDocument.GetTextChangesAsync(document, cancellationToken).WaitAndGetResult(cancellationToken)
145+
Dim changes = cleanDocument.GetTextChangesSynchronously(document, cancellationToken)
146146

147147
Using transaction = New CaretPreservingEditTransaction(VBEditorResources.End_Construct, view, _undoHistoryRegistry, _editorOperationsFactoryService)
148148
transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance

src/EditorFeatures/VisualBasic/LineCommit/CommitFormatter.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.LineCommit
121121
End If
122122
End If
123123

124-
Dim changes = finalDocument.GetTextChangesAsync(document, cancellationToken).WaitAndGetResult(cancellationToken)
124+
Dim changes = finalDocument.GetTextChangesSynchronously(document, cancellationToken)
125125
buffer.ApplyChanges(changes)
126126
End Using
127127
End Sub

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,13 @@ public Document WithFilePath(string? filePath)
419419
/// Get the text changes between this document and a prior version of the same document.
420420
/// The changes, when applied to the text of the old document, will produce the text of the current document.
421421
/// </summary>
422-
public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocument, CancellationToken cancellationToken = default)
422+
public Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocument, CancellationToken cancellationToken = default)
423+
{
424+
return Task.FromResult<IEnumerable<TextChange>>(GetTextChangesSynchronously(oldDocument, cancellationToken));
425+
}
426+
427+
internal ImmutableArray<TextChange> GetTextChangesSynchronously(
428+
Document oldDocument, CancellationToken cancellationToken)
423429
{
424430
try
425431
{
@@ -443,10 +449,10 @@ public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocum
443449
var container = text.Container;
444450
if (container != null)
445451
{
446-
var textChanges = text.GetTextChanges(oldText).ToList();
452+
var textChanges = text.GetTextChanges(oldText).ToImmutableArray();
447453

448454
// if changes are significant (not the whole document being replaced) then use these changes
449-
if (textChanges.Count > 1 || (textChanges.Count == 1 && textChanges[0].Span != new TextSpan(0, oldText.Length)))
455+
if (textChanges.Length > 1 || (textChanges.Length == 1 && textChanges[0].Span != new TextSpan(0, oldText.Length)))
450456
{
451457
return textChanges;
452458
}
@@ -456,17 +462,18 @@ public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocum
456462
// get changes by diffing the trees
457463
if (this.SupportsSyntaxTree)
458464
{
459-
var tree = (await this.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false))!;
460-
var oldTree = await oldDocument.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
465+
var tree = this.GetSyntaxTreeSynchronously(cancellationToken);
466+
var oldTree = oldDocument.GetSyntaxTreeSynchronously(cancellationToken);
461467

468+
RoslynDebug.Assert(tree is object);
462469
RoslynDebug.Assert(oldTree is object);
463-
return tree.GetChanges(oldTree);
470+
return tree.GetChanges(oldTree).ToImmutableArray();
464471
}
465472

466-
text = await this.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
467-
oldText = await oldDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
473+
text = this.GetTextSynchronously(cancellationToken);
474+
oldText = oldDocument.GetTextSynchronously(cancellationToken);
468475

469-
return text.GetTextChanges(oldText).ToList();
476+
return text.GetTextChanges(oldText).ToImmutableArray();
470477
}
471478
}
472479
catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e, cancellationToken))

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ private void ApplyChangedDocument(
20132013
{
20142014
// We have the old text, but no new text is easily available. This typically happens when the content is modified via changes to the syntax tree.
20152015
// Ask document to compute equivalent text changes by comparing the syntax trees, and use them to
2016-
var textChanges = newDoc.GetTextChangesAsync(oldDoc, CancellationToken.None).WaitAndGetResult_CanCallOnBackground(CancellationToken.None); // needs wait
2016+
var textChanges = newDoc.GetTextChangesSynchronously(oldDoc, CancellationToken.None);
20172017
this.ApplyDocumentTextChanged(documentId, oldText.WithChanges(textChanges));
20182018
}
20192019
else
@@ -2025,15 +2025,6 @@ private void ApplyChangedDocument(
20252025
}
20262026
}
20272027

2028-
[Conditional("DEBUG")]
2029-
private static void CheckNoChanges(Solution oldSolution, Solution newSolution)
2030-
{
2031-
var changes = newSolution.GetChanges(oldSolution);
2032-
Contract.ThrowIfTrue(changes.GetAddedProjects().Any());
2033-
Contract.ThrowIfTrue(changes.GetRemovedProjects().Any());
2034-
Contract.ThrowIfTrue(changes.GetProjectChanges().Any());
2035-
}
2036-
20372028
private static ProjectInfo CreateProjectInfo(Project project)
20382029
{
20392030
return ProjectInfo.Create(

0 commit comments

Comments
 (0)