Skip to content

Commit 3a0d65c

Browse files
Make C# completion list rewriting synchronous
C# completion list is currently split across three "rewriter" objects that are async. However, none of them need to be async if they're passed a RazorCodeDocument. So, this change acquires a RazorCodeDocument up front before calling the rewriters. With this in place, the rewriters can be synchronous.
1 parent 0bfbdab commit 3a0d65c

File tree

6 files changed

+34
-37
lines changed

6 files changed

+34
-37
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,18 @@ public static async ValueTask<VSInternalCompletionList> RewriteCSharpResponseAsy
113113
return new VSInternalCompletionList() { IsIncomplete = true, Items = [] };
114114
}
115115

116+
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
117+
116118
var rewrittenResponse = delegatedResponse;
117119

118120
foreach (var rewriter in s_delegatedCSharpCompletionResponseRewriters)
119121
{
120-
rewrittenResponse = await rewriter.RewriteAsync(
122+
rewrittenResponse = rewriter.Rewrite(
121123
rewrittenResponse,
124+
codeDocument,
122125
absoluteIndex,
123-
documentContext,
124126
projectedPosition,
125-
completionOptions,
126-
cancellationToken).ConfigureAwait(false);
127+
completionOptions);
127128
}
128129

129130
return rewrittenResponse;

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
using System.Collections.Frozen;
55
using System.Diagnostics;
6-
using System.Threading;
7-
using System.Threading.Tasks;
86
using Microsoft.AspNetCore.Razor;
7+
using Microsoft.AspNetCore.Razor.Language;
98
using Microsoft.AspNetCore.Razor.Language.Syntax;
109
using Microsoft.AspNetCore.Razor.PooledObjects;
11-
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1210
using Microsoft.CodeAnalysis.Text;
1311
using Microsoft.VisualStudio.LanguageServer.Protocol;
1412
using RazorSyntaxNode = Microsoft.AspNetCore.Razor.Language.Syntax.SyntaxNode;
@@ -32,23 +30,24 @@ internal class DesignTimeHelperResponseRewriter : IDelegatedCSharpCompletionResp
3230
"BuildRenderTree"
3331
}.ToFrozenSet();
3432

35-
public async Task<VSInternalCompletionList> RewriteAsync(
33+
public VSInternalCompletionList Rewrite(
3634
VSInternalCompletionList completionList,
35+
RazorCodeDocument codeDocument,
3736
int hostDocumentIndex,
38-
DocumentContext hostDocumentContext,
3937
Position projectedPosition,
40-
RazorCompletionOptions completionOptions,
41-
CancellationToken cancellationToken)
38+
RazorCompletionOptions completionOptions)
4239
{
43-
var syntaxTree = await hostDocumentContext.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
44-
var owner = syntaxTree.Root.FindInnermostNode(hostDocumentIndex);
40+
var owner = codeDocument
41+
.GetRequiredSyntaxRoot()
42+
.FindInnermostNode(hostDocumentIndex);
43+
4544
if (owner is null)
4645
{
4746
Debug.Fail("Owner should never be null.");
4847
return completionList;
4948
}
5049

51-
var sourceText = await hostDocumentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
50+
var sourceText = codeDocument.Source.Text;
5251

5352
// We should remove Razor design-time helpers from C#'s completion list. If the current identifier
5453
// being targeted does not start with a double underscore, we trim out all items starting with "__"
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
4+
using Microsoft.AspNetCore.Razor.Language;
75
using Microsoft.VisualStudio.LanguageServer.Protocol;
86

97
namespace Microsoft.CodeAnalysis.Razor.Completion.Delegation;
108

119
internal interface IDelegatedCSharpCompletionResponseRewriter
1210
{
13-
Task<VSInternalCompletionList> RewriteAsync(
11+
VSInternalCompletionList Rewrite(
1412
VSInternalCompletionList completionList,
13+
RazorCodeDocument codeDocument,
1514
int hostDocumentIndex,
16-
DocumentContext hostDocumentContext,
1715
Position projectedPosition,
18-
RazorCompletionOptions completionOptions,
19-
CancellationToken cancellationToken);
16+
RazorCompletionOptions completionOptions);
2017
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System.Threading;
5-
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Razor.Language;
65
using Microsoft.AspNetCore.Razor.PooledObjects;
7-
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
86
using Microsoft.VisualStudio.LanguageServer.Protocol;
97

108
namespace Microsoft.CodeAnalysis.Razor.Completion.Delegation;
@@ -17,13 +15,12 @@ namespace Microsoft.CodeAnalysis.Razor.Completion.Delegation;
1715
/// </remarks>
1816
internal class SnippetResponseRewriter : IDelegatedCSharpCompletionResponseRewriter
1917
{
20-
public Task<VSInternalCompletionList> RewriteAsync(
18+
public VSInternalCompletionList Rewrite(
2119
VSInternalCompletionList completionList,
20+
RazorCodeDocument codeDocument,
2221
int hostDocumentIndex,
23-
DocumentContext hostDocumentContext,
2422
Position projectedPosition,
25-
RazorCompletionOptions completionOptions,
26-
CancellationToken cancellationToken)
23+
RazorCompletionOptions completionOptionsn)
2724
{
2825
using var items = new PooledArrayBuilder<CompletionItem>(completionList.Items.Length);
2926

@@ -43,6 +40,6 @@ public Task<VSInternalCompletionList> RewriteAsync(
4340
completionList.Items = items.ToArray();
4441
}
4542

46-
return Task.FromResult(completionList);
43+
return completionList;
4744
}
4845
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
4+
using Microsoft.AspNetCore.Razor.Language;
75
using Microsoft.VisualStudio.LanguageServer.Protocol;
86

97
namespace Microsoft.CodeAnalysis.Razor.Completion.Delegation;
108

119
internal class TextEditResponseRewriter : IDelegatedCSharpCompletionResponseRewriter
1210
{
13-
public async Task<VSInternalCompletionList> RewriteAsync(
11+
public VSInternalCompletionList Rewrite(
1412
VSInternalCompletionList completionList,
13+
RazorCodeDocument codeDocument,
1514
int hostDocumentIndex,
16-
DocumentContext hostDocumentContext,
1715
Position projectedPosition,
18-
RazorCompletionOptions completionOptions,
19-
CancellationToken cancellationToken)
16+
RazorCompletionOptions completionOptions)
2017
{
21-
var sourceText = await hostDocumentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
18+
var sourceText = codeDocument.Source.Text;
2219

2320
var hostDocumentPosition = sourceText.GetPosition(hostDocumentIndex);
2421
completionList = TranslateTextEdits(hostDocumentPosition, projectedPosition, completionList);

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RazorCodeDocumentExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ internal static class RazorCodeDocumentExtensions
2222
{
2323
private static readonly object s_csharpSyntaxTreeKey = new();
2424

25+
public static RazorSyntaxTree GetRequiredSyntaxTree(this RazorCodeDocument codeDocument)
26+
=> codeDocument.GetSyntaxTree().AssumeNotNull();
27+
28+
public static Syntax.SyntaxNode GetRequiredSyntaxRoot(this RazorCodeDocument codeDocument)
29+
=> codeDocument.GetRequiredSyntaxTree().Root;
30+
2531
public static SourceText GetCSharpSourceText(this RazorCodeDocument document)
2632
=> document.GetCSharpDocument().Text;
2733

0 commit comments

Comments
 (0)