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

Pass along cancellation token in snippets #73071

Merged
merged 1 commit into from Apr 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -27,18 +27,13 @@ public AbstractSnippetFunction(SnippetExpansionClient snippetExpansionClient, IT
_threadingContext = threadingContext;
}

protected bool TryGetDocument([NotNullWhen(true)] out Document? document)
{
document = _subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges()?.WithFrozenPartialSemantics(CancellationToken.None);
return document != null;
}
protected Document? GetDocument(CancellationToken cancellationToken)
=> _subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges()?.WithFrozenPartialSemantics(cancellationToken);

private int GetDefaultValue(CancellationToken cancellationToken, out string value, out int hasDefaultValue)
{
var (ExitCode, Value, HasDefaultValue) = _threadingContext.JoinableTaskFactory.Run(() => GetDefaultValueAsync(cancellationToken));
value = Value;
hasDefaultValue = HasDefaultValue;
return ExitCode;
(var exitCode, value, hasDefaultValue) = _threadingContext.JoinableTaskFactory.Run(() => GetDefaultValueAsync(cancellationToken));
return exitCode;
}

protected virtual Task<(int ExitCode, string Value, int HasDefaultValue)> GetDefaultValueAsync(CancellationToken cancellationToken)
Expand All @@ -48,10 +43,8 @@ protected virtual Task<(int ExitCode, string Value, int HasDefaultValue)> GetDef

private int GetCurrentValue(CancellationToken cancellationToken, out string value, out int hasCurrentValue)
{
var (ExitCode, Value, HasCurrentValue) = _threadingContext.JoinableTaskFactory.Run(() => GetCurrentValueAsync(cancellationToken));
value = Value;
hasCurrentValue = HasCurrentValue;
return ExitCode;
(var exitCode, value, hasCurrentValue) = _threadingContext.JoinableTaskFactory.Run(() => GetCurrentValueAsync(cancellationToken));
return exitCode;
}

protected virtual Task<(int ExitCode, string Value, int HasCurrentValue)> GetCurrentValueAsync(CancellationToken cancellationToken)
Expand Down
Expand Up @@ -28,29 +28,22 @@ protected override async Task<(int ExitCode, string Value, int HasDefaultValue)>
{
var hasDefaultValue = 0;
var value = string.Empty;
if (!TryGetDocument(out var document))
{
var document = GetDocument(cancellationToken);
if (document is null)
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

var surfaceBufferFieldSpan = new VsTextSpan[1];
if (snippetExpansionClient.ExpansionSession.GetFieldSpan(FieldName, surfaceBufferFieldSpan) != VSConstants.S_OK)
{
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

if (!snippetExpansionClient.TryGetSubjectBufferSpan(surfaceBufferFieldSpan[0], out var subjectBufferFieldSpan))
{
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

var snippetFunctionService = document.Project.GetRequiredLanguageService<SnippetFunctionService>();
value = await snippetFunctionService.GetContainingClassNameAsync(document, subjectBufferFieldSpan.Start.Position, cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(value))
{
hasDefaultValue = 1;
}

return (VSConstants.S_OK, value, hasDefaultValue);
}
Expand Down
Expand Up @@ -41,10 +41,9 @@ protected override int FieldChanged(string field, out int requeryFunction)

protected override async Task<(int ExitCode, string Value, int HasCurrentValue)> GetCurrentValueAsync(CancellationToken cancellationToken)
{
if (!TryGetDocument(out var document))
{
var document = GetDocument(cancellationToken);
if (document is null)
return (VSConstants.S_OK, string.Empty, HasCurrentValue: 0);
}

// If the switch expression is invalid, still show the default case
var hasCurrentValue = 1;
Expand All @@ -60,9 +59,7 @@ protected override async Task<(int ExitCode, string Value, int HasCurrentValue)>

var value = await snippetFunctionService.GetSwitchExpansionAsync(document, caseGenerationSpan.Value, switchExpressionSpan.Value, simplifierOptions, cancellationToken).ConfigureAwait(false);
if (value == null)
{
return (VSConstants.S_OK, snippetFunctionService.SwitchDefaultCaseForm, hasCurrentValue);
}

return (VSConstants.S_OK, value, hasCurrentValue);
}
Expand Down
Expand Up @@ -35,23 +35,18 @@ protected override async Task<(int ExitCode, string Value, int HasDefaultValue)>
{
var value = _fullyQualifiedName;
var hasDefaultValue = 1;
if (!TryGetDocument(out var document))
{
var document = GetDocument(cancellationToken);
if (document is null)
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

if (!TryGetFieldSpan(out var fieldSpan))
{
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

var simplifierOptions = await document.GetSimplifierOptionsAsync(snippetExpansionClient.EditorOptionsService.GlobalOptions, cancellationToken).ConfigureAwait(false);

var simplifiedTypeName = await SnippetFunctionService.GetSimplifiedTypeNameAsync(document, fieldSpan.Value, _fullyQualifiedName, simplifierOptions, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(simplifiedTypeName))
{
return (VSConstants.E_FAIL, value, hasDefaultValue);
}

return (VSConstants.S_OK, simplifiedTypeName!, hasDefaultValue);
}
Expand Down