Skip to content

Commit b533363

Browse files
Make TryGetProvisionalCompletionInfoAsync synchronous
The only reason this method was async was to call the IDocumentMappingService extension method, GetPositionInfoAsync. However, the reason *that* extension method is async is to call GetCodeDocumentAsync(). Ultimately, this method can be made synchronous by passing RazorCodeDocument to it.
1 parent ef4a827 commit b533363

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Completion/Delegation/DelegatedCompletionListProvider.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ public DelegatedCompletionListProvider(
6161
return null;
6262
}
6363

64-
var provisionalCompletion = await DelegatedCompletionHelper.TryGetProvisionalCompletionInfoAsync(
65-
documentContext,
66-
completionContext,
67-
positionInfo,
68-
_documentMappingService,
69-
cancellationToken).ConfigureAwait(false);
64+
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
65+
7066
TextEdit? provisionalTextEdit = null;
71-
if (provisionalCompletion is { } provisionalCompletionValue)
67+
if (DelegatedCompletionHelper.TryGetProvisionalCompletionInfo(codeDocument, completionContext, positionInfo, _documentMappingService, out var provisionalCompletion))
7268
{
73-
provisionalTextEdit = provisionalCompletionValue.ProvisionalTextEdit;
74-
positionInfo = provisionalCompletionValue.DocumentPositionInfo;
69+
provisionalTextEdit = provisionalCompletion.ProvisionalTextEdit;
70+
positionInfo = provisionalCompletion.DocumentPositionInfo;
7571
}
7672

7773
if (DelegatedCompletionHelper.RewriteContext(completionContext, positionInfo.LanguageKind, _triggerAndCommitCharacters) is not { } rewrittenContext)
@@ -81,8 +77,6 @@ public DelegatedCompletionListProvider(
8177

8278
completionContext = rewrittenContext;
8379

84-
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
85-
8680
// It's a bit confusing, but we have two different "add snippets" options - one is a part of
8781
// RazorCompletionOptions and becomes a part of RazorCompletionContext and is used by
8882
// RazorCompletionFactsService, and the second one below that's used for delegated completion

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
using System.Collections.Immutable;
55
using System.Diagnostics;
6-
using System.Threading;
7-
using System.Threading.Tasks;
86
using Microsoft.AspNetCore.Razor.Language;
97
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
10-
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
118
using Microsoft.CodeAnalysis.Razor.Protocol;
129
using Microsoft.CodeAnalysis.Razor.Protocol.Completion;
1310
using Microsoft.VisualStudio.LanguageServer.Protocol;
@@ -159,34 +156,34 @@ public static VSInternalCompletionList RewriteHtmlResponse(
159156
/// to C# and will return a temporary edit that should be made to the generated document
160157
/// in order to add the '.' to the generated C# contents.
161158
/// </remarks>
162-
public static async Task<CompletionPositionInfo?> TryGetProvisionalCompletionInfoAsync(
163-
DocumentContext documentContext,
159+
public static bool TryGetProvisionalCompletionInfo(
160+
RazorCodeDocument codeDocument,
164161
VSInternalCompletionContext completionContext,
165162
DocumentPositionInfo originalPositionInfo,
166163
IDocumentMappingService documentMappingService,
167-
CancellationToken cancellationToken)
164+
out CompletionPositionInfo result)
168165
{
166+
result = default;
167+
169168
if (originalPositionInfo.LanguageKind != RazorLanguageKind.Html ||
170169
completionContext.TriggerKind != CompletionTriggerKind.TriggerCharacter ||
171170
completionContext.TriggerCharacter != ".")
172171
{
173172
// Invalid provisional completion context
174-
return null;
173+
return false;
175174
}
176175

177176
if (originalPositionInfo.Position.Character == 0)
178177
{
179178
// We're at the start of line. Can't have provisional completions here.
180-
return null;
179+
return false;
181180
}
182181

183-
var previousCharacterPositionInfo = await documentMappingService
184-
.GetPositionInfoAsync(documentContext, originalPositionInfo.HostDocumentIndex - 1, cancellationToken)
185-
.ConfigureAwait(false);
182+
var previousCharacterPositionInfo = documentMappingService.GetPositionInfo(codeDocument, originalPositionInfo.HostDocumentIndex - 1);
186183

187184
if (previousCharacterPositionInfo.LanguageKind != RazorLanguageKind.CSharp)
188185
{
189-
return null;
186+
return false;
190187
}
191188

192189
var previousPosition = previousCharacterPositionInfo.Position;
@@ -202,7 +199,8 @@ public static VSInternalCompletionList RewriteHtmlResponse(
202199
previousPosition.Character + 1),
203200
previousCharacterPositionInfo.HostDocumentIndex + 1);
204201

205-
return new CompletionPositionInfo(addProvisionalDot, provisionalPositionInfo, ShouldIncludeDelegationSnippets: false);
202+
result = new CompletionPositionInfo(addProvisionalDot, provisionalPositionInfo, ShouldIncludeDelegationSnippets: false);
203+
return true;
206204
}
207205

208206
public static bool ShouldIncludeSnippets(RazorCodeDocument razorCodeDocument, int absoluteIndex)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,10 @@ protected override IRemoteCompletionService CreateService(in ServiceArgs args)
6363
var positionInfo = GetPositionInfo(codeDocument, index);
6464

6565
if (positionInfo.LanguageKind != RazorLanguageKind.Razor
66-
&& await DelegatedCompletionHelper.TryGetProvisionalCompletionInfoAsync(
67-
remoteDocumentContext,
68-
completionContext,
69-
positionInfo,
70-
DocumentMappingService,
71-
cancellationToken)
72-
.ConfigureAwait(false) is { } provisionalCompletionInfo)
66+
&& DelegatedCompletionHelper.TryGetProvisionalCompletionInfo(
67+
codeDocument, completionContext, positionInfo, DocumentMappingService, out var provisionalCompletionInfo))
7368
{
74-
return new CompletionPositionInfo(
75-
provisionalCompletionInfo.ProvisionalTextEdit,
76-
provisionalCompletionInfo.DocumentPositionInfo,
77-
ShouldIncludeDelegationSnippets: false);
69+
return provisionalCompletionInfo with { ShouldIncludeDelegationSnippets = false };
7870
}
7971

8072
var shouldIncludeSnippets = positionInfo.LanguageKind == RazorLanguageKind.Html

0 commit comments

Comments
 (0)