Skip to content

Commit a198c29

Browse files
Make IDocumentContextFactory synchronous
Now that ISnapshotResolver is synchronous, there's no reason for IDocumentContextFactory to be synchronous.
1 parent e027f2e commit a198c29

File tree

38 files changed

+110
-107
lines changed

38 files changed

+110
-107
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CSharp/DefaultCSharpCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public async override Task<CodeAction> ResolveAsync(
7373
throw new ArgumentNullException(nameof(codeAction));
7474
}
7575

76-
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(csharpParams.RazorFileIdentifier, cancellationToken).ConfigureAwait(false);
76+
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier);
7777
if (documentContext is null)
7878
{
7979
return codeAction;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CSharp/UnformattedRemappingCSharpCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async override Task<CodeAction> ResolveAsync(
5454

5555
cancellationToken.ThrowIfCancellationRequested();
5656

57-
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(csharpParams.RazorFileIdentifier, cancellationToken).ConfigureAwait(false);
57+
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier);
5858
if (documentContext is null)
5959
{
6060
return codeAction;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Html/DefaultHtmlCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async override Task<CodeAction> ResolveAsync(
5555
throw new ArgumentNullException(nameof(codeAction));
5656
}
5757

58-
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(resolveParams.RazorFileIdentifier, cancellationToken).ConfigureAwait(false);
58+
var documentContext = _documentContextFactory.TryCreateForOpenDocument(resolveParams.RazorFileIdentifier);
5959
if (documentContext is null)
6060
{
6161
return codeAction;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/AddUsingsCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public AddUsingsCodeActionResolver(IDocumentContextFactory documentContextFactor
4545
return null;
4646
}
4747

48-
var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
48+
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
4949
if (documentContext is null)
5050
{
5151
return null;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CreateComponentCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public CreateComponentCodeActionResolver(IDocumentContextFactory documentContext
4343
return null;
4444
}
4545

46-
var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
46+
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
4747
if (documentContext is null)
4848
{
4949
return null;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public ExtractToCodeBehindCodeActionResolver(
6060

6161
var path = FilePathNormalizer.Normalize(actionParams.Uri.GetAbsoluteOrUNCPath());
6262

63-
var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
63+
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
6464
if (documentContext is null)
6565
{
6666
return null;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/GenerateMethodCodeActionResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public GenerateMethodCodeActionResolver(
7272
return null;
7373
}
7474

75-
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
75+
var documentContext = _documentContextFactory.TryCreateForOpenDocument(actionParams.Uri);
7676
if (documentContext is null)
7777
{
7878
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private async Task<VSInternalCompletionItem> PostProcessCompletionItemAsync(
100100
}
101101

102102
var identifier = context.OriginalRequestParams.Identifier.TextDocumentIdentifier;
103-
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(identifier, cancellationToken).ConfigureAwait(false);
103+
var documentContext = _documentContextFactory.TryCreateForOpenDocument(identifier);
104104
if (documentContext is null)
105105
{
106106
return resolvedCompletionItem;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ .. csharpDiagnostics ?? []
183183
delegatedResponse.Value.TryGetFirst(out var fullDiagnostics) &&
184184
fullDiagnostics.Items is not null)
185185
{
186-
var documentContext = await _documentContextFactory.Value
187-
.TryCreateAsync(delegatedParams.TextDocument.Uri, projectContext: null, token)
188-
.ConfigureAwait(false);
186+
var documentContext = _documentContextFactory.Value
187+
.TryCreate(delegatedParams.TextDocument.Uri, projectContext: null);
189188

190189
if (documentContext is not null)
191190
{

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContextFactory.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
@@ -27,12 +28,11 @@ internal sealed class DocumentContextFactory(
2728
private readonly IDocumentVersionCache _documentVersionCache = documentVersionCache;
2829
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<DocumentContextFactory>();
2930

30-
public async Task<DocumentContext?> TryCreateAsync(Uri documentUri, VSProjectContext? projectContext, bool versioned, CancellationToken cancellationToken)
31+
public DocumentContext? TryCreate(Uri documentUri, VSProjectContext? projectContext, bool versioned)
3132
{
3233
var filePath = documentUri.GetAbsoluteOrUNCPath();
33-
var documentAndVersion = await TryGetDocumentAndVersionAsync(filePath, projectContext, versioned, cancellationToken).ConfigureAwait(false);
3434

35-
if (documentAndVersion is null)
35+
if (!TryGetDocumentAndVersion(filePath, projectContext, versioned, out var documentAndVersion))
3636
{
3737
// Stale request or misbehaving client, see above comment.
3838
return null;
@@ -59,20 +59,24 @@ internal sealed class DocumentContextFactory(
5959
return new DocumentContext(documentUri, documentSnapshot, projectContext);
6060
}
6161

62-
private async Task<DocumentSnapshotAndVersion?> TryGetDocumentAndVersionAsync(string filePath, VSProjectContext? projectContext, bool versioned, CancellationToken cancellationToken)
62+
private bool TryGetDocumentAndVersion(
63+
string filePath,
64+
VSProjectContext? projectContext,
65+
bool versioned,
66+
[NotNullWhen(true)] out DocumentSnapshotAndVersion? documentAndVersion)
6367
{
64-
var documentSnapshot = await TryResolveDocumentAsync(filePath, projectContext, cancellationToken).ConfigureAwait(false);
65-
66-
if (documentSnapshot is not null)
68+
if (TryResolveDocument(filePath, projectContext, out var documentSnapshot))
6769
{
6870
if (!versioned)
6971
{
70-
return new DocumentSnapshotAndVersion(documentSnapshot, Version: null);
72+
documentAndVersion = new DocumentSnapshotAndVersion(documentSnapshot, Version: null);
73+
return true;
7174
}
7275

7376
if (_documentVersionCache.TryGetDocumentVersion(documentSnapshot, out var version))
7477
{
75-
return new DocumentSnapshotAndVersion(documentSnapshot, version.Value);
78+
documentAndVersion = new DocumentSnapshotAndVersion(documentSnapshot, version.Value);
79+
return true;
7680
}
7781

7882
_logger.LogWarning($"Tried to create context for document {filePath} and project {projectContext?.Id} and a document was found, but version didn't match.");
@@ -85,20 +89,26 @@ internal sealed class DocumentContextFactory(
8589
// version cache has evicted the entry
8690
// 2. Client is misbehaving and sending requests for a document that we've never seen before.
8791
_logger.LogWarning($"Tried to create context for document {filePath} and project {projectContext?.Id} which was not found.");
88-
return null;
92+
documentAndVersion = null;
93+
return false;
8994
}
9095

91-
private async Task<IDocumentSnapshot?> TryResolveDocumentAsync(string filePath, VSProjectContext? projectContext, CancellationToken cancellationToken)
96+
private bool TryResolveDocument(
97+
string filePath,
98+
VSProjectContext? projectContext,
99+
[NotNullWhen(true)] out IDocumentSnapshot? documentSnapshot)
92100
{
93101
if (projectContext is null)
94102
{
95-
return _snapshotResolver.ResolveDocumentInAnyProject(filePath);
103+
documentSnapshot = _snapshotResolver.ResolveDocumentInAnyProject(filePath);
104+
return documentSnapshot is not null;
96105
}
97106

98107
if (_projectManager.TryGetLoadedProject(projectContext.ToProjectKey(), out var project) &&
99108
project.GetDocument(filePath) is { } document)
100109
{
101-
return document;
110+
documentSnapshot = document;
111+
return true;
102112
}
103113

104114
// Couldn't find the document in a real project. Maybe the language server doesn't yet know about the project
@@ -109,10 +119,12 @@ internal sealed class DocumentContextFactory(
109119
if (miscellaneousProject.GetDocument(normalizedDocumentPath) is { } miscDocument)
110120
{
111121
_logger.LogDebug($"Found document {filePath} in the misc files project, but was asked for project context {projectContext.Id}");
112-
return miscDocument;
122+
documentSnapshot = miscDocument;
123+
return true;
113124
}
114125

115-
return null;
126+
documentSnapshot = null;
127+
return false;
116128
}
117129

118130
private record DocumentSnapshotAndVersion(IDocumentSnapshot Snapshot, int? Version);

0 commit comments

Comments
 (0)