Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove semantic model keep-alive code in completion (#73844)" #74302

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private AsyncCompletionData.CommitResult Commit(

// This might be an item promoted by us, make sure we restore it to the original state first.
roslynItem = Helpers.DemoteItem(roslynItem);
CompletionChange change = null!;
CompletionChange change;

// We met an issue when external code threw an OperationCanceledException and the cancellationToken is not canceled.
// Catching this scenario for further investigations.
Expand All @@ -229,10 +229,7 @@ private AsyncCompletionData.CommitResult Commit(
if (_textView is IDebuggerTextView)
roslynItem = ImportCompletionItem.MarkItemToAlwaysFullyQualify(roslynItem);

_threadingContext.JoinableTaskFactory.Run(async () =>
{
change = await completionService.GetChangeAsync(document, roslynItem, commitCharacter, cancellationToken).ConfigureAwait(true);
});
change = completionService.GetChangeAsync(document, roslynItem, commitCharacter, cancellationToken).WaitAndGetResult(cancellationToken);
}
catch (OperationCanceledException e) when (e.CancellationToken != cancellationToken && FatalError.ReportAndCatch(e))
{
Expand Down Expand Up @@ -322,11 +319,7 @@ private AsyncCompletionData.CommitResult Commit(
var spanToFormat = triggerSnapshotSpan.TranslateTo(subjectBuffer.CurrentSnapshot, SpanTrackingMode.EdgeInclusive);

// Note: C# always completes synchronously, TypeScript is async
var changes = ImmutableArray<TextChange>.Empty;
_threadingContext.JoinableTaskFactory.Run(async () =>
{
changes = await formattingService.GetFormattingChangesAsync(currentDocument, subjectBuffer, spanToFormat.Span.ToTextSpan(), cancellationToken).ConfigureAwait(true);
});
var changes = formattingService.GetFormattingChangesAsync(currentDocument, subjectBuffer, spanToFormat.Span.ToTextSpan(), cancellationToken).WaitAndGetResult(cancellationToken);
subjectBuffer.ApplyChanges(changes);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Features/Core/Portable/Completion/CompletionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ public virtual TextSpan GetDefaultCompletionListSpan(SourceText text, int caretP

var extensionManager = document.Project.Solution.Workspace.Services.GetRequiredService<IExtensionManager>();

document = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);

// We don't need SemanticModel here, just want to make sure it won't get GC'd before CompletionProviders are able to get it.
(document, var semanticModel) = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);
var description = await extensionManager.PerformFunctionAsync(
provider,
cancellationToken => provider.GetDescriptionAsync(document, item, options, displayOptions, cancellationToken),
defaultValue: null,
cancellationToken).ConfigureAwait(false);

GC.KeepAlive(semanticModel);
return description;
}

Expand All @@ -245,7 +245,8 @@ public virtual async Task<CompletionChange> GetChangeAsync(
{
var extensionManager = document.Project.Solution.Workspace.Services.GetRequiredService<IExtensionManager>();

document = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);
// We don't need SemanticModel here, just want to make sure it won't get GC'd before CompletionProviders are able to get it.
(document, var semanticModel) = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);

var change = await extensionManager.PerformFunctionAsync(
provider,
Expand All @@ -255,6 +256,7 @@ public virtual async Task<CompletionChange> GetChangeAsync(
if (change == null)
return CompletionChange.Create(new TextChange(new TextSpan(), ""));

GC.KeepAlive(semanticModel);
Debug.Assert(item.Span == change.TextChange.Span || item.IsComplexTextEdit);
return change;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ internal virtual async Task<CompletionList> GetCompletionsAsync(
ImmutableHashSet<string>? roles = null,
CancellationToken cancellationToken = default)
{
document = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);
// We don't need SemanticModel here, just want to make sure it won't get GC'd before CompletionProviders are able to get it.
(document, var semanticModel) = await GetDocumentWithFrozenPartialSemanticsAsync(document, cancellationToken).ConfigureAwait(false);

var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var completionListSpan = GetDefaultCompletionListSpan(text, caretPosition);
Expand Down Expand Up @@ -114,6 +115,8 @@ internal virtual async Task<CompletionList> GetCompletionsAsync(
var augmentingContexts = await ComputeNonEmptyCompletionContextsAsync(
document, caretPosition, trigger, options, completionListSpan, augmentingProviders, sharedContext, cancellationToken).ConfigureAwait(false);

GC.KeepAlive(semanticModel);

// Providers are ordered, but we processed them in our own order. Ensure that the
// groups are properly ordered based on the original providers.
var completionProviderToIndex = GetCompletionProviderToIndex(providers);
Expand Down Expand Up @@ -169,20 +172,19 @@ static async Task<ImmutableArray<CompletionProvider>> GetAugmentingProvidersAsyn
}

/// <summary>
/// Returns a document with frozen partial semantic model unless caller is test code require full semantics.
/// Returns a document with frozen partial semantic unless we already have a complete compilation available.
/// Getting full semantic could be costly in certain scenarios and would cause significant delay in completion.
/// In most cases we'd still end up with complete document, but we'd consider it an acceptable trade-off even when
/// we get into this transient state.
/// </summary>
private async Task<Document> GetDocumentWithFrozenPartialSemanticsAsync(Document document, CancellationToken cancellationToken)
private async Task<(Document document, SemanticModel? semanticModel)> GetDocumentWithFrozenPartialSemanticsAsync(Document document, CancellationToken cancellationToken)
{
if (_suppressPartialSemantics)
{
var _ = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return document;
return (document, await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false));
}

return document.WithFrozenPartialSemantics(cancellationToken);
return await document.GetFullOrPartialSemanticModelAsync(cancellationToken).ConfigureAwait(false);
}

private static bool ValidatePossibleTriggerCharacterSet(CompletionTriggerKind completionTriggerKind, IEnumerable<CompletionProvider> triggeredProviders,
Expand Down
Loading