Skip to content

Commit c9efa48

Browse files
Improve signatures of TryCreate methods
1 parent 313b8c6 commit c9efa48

File tree

38 files changed

+183
-127
lines changed

38 files changed

+183
-127
lines changed

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

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

76-
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier);
77-
if (documentContext is null)
76+
if (!_documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier, out var documentContext))
7877
{
7978
return codeAction;
8079
}

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

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

5555
cancellationToken.ThrowIfCancellationRequested();
5656

57-
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier);
58-
if (documentContext is null)
57+
if (!_documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileIdentifier, out var documentContext))
5958
{
6059
return codeAction;
6160
}

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

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

58-
var documentContext = _documentContextFactory.TryCreateForOpenDocument(resolveParams.RazorFileIdentifier);
59-
if (documentContext is null)
58+
if (!_documentContextFactory.TryCreateForOpenDocument(resolveParams.RazorFileIdentifier, out var documentContext))
6059
{
6160
return codeAction;
6261
}

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

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

48-
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
49-
if (documentContext is null)
48+
if (!_documentContextFactory.TryCreate(actionParams.Uri, out var documentContext))
5049
{
5150
return null;
5251
}

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

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

46-
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
47-
if (documentContext is null)
46+
if (!_documentContextFactory.TryCreate(actionParams.Uri, out var documentContext))
4847
{
4948
return null;
5049
}

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

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

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

63-
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
64-
if (documentContext is null)
63+
if (!_documentContextFactory.TryCreate(actionParams.Uri, out var documentContext))
6564
{
6665
return null;
6766
}

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

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

75-
var documentContext = _documentContextFactory.TryCreateForOpenDocument(actionParams.Uri);
76-
if (documentContext is null)
75+
if (!_documentContextFactory.TryCreateForOpenDocument(actionParams.Uri, out var documentContext))
7776
{
7877
return null;
7978
}

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

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

102102
var identifier = context.OriginalRequestParams.Identifier.TextDocumentIdentifier;
103-
var documentContext = _documentContextFactory.TryCreateForOpenDocument(identifier);
104-
if (documentContext is null)
103+
if (!_documentContextFactory.TryCreateForOpenDocument(identifier, out var documentContext))
105104
{
106105
return resolvedCompletionItem;
107106
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ .. csharpDiagnostics ?? []
183183
delegatedResponse.Value.TryGetFirst(out var fullDiagnostics) &&
184184
fullDiagnostics.Items is not null)
185185
{
186-
var documentContext = _documentContextFactory.Value
187-
.TryCreate(delegatedParams.TextDocument.Uri, projectContext: null);
188-
189-
if (documentContext is not null)
186+
if (_documentContextFactory.Value.TryCreate(delegatedParams.TextDocument.Uri, projectContext: null, out var documentContext))
190187
{
191188
return await _translateDiagnosticsService.Value
192189
.TranslateAsync(RazorLanguageKind.CSharp, fullDiagnostics.Items, documentContext, token)

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,44 @@ internal sealed class DocumentContextFactory(
2828
private readonly IDocumentVersionCache _documentVersionCache = documentVersionCache;
2929
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<DocumentContextFactory>();
3030

31-
public DocumentContext? TryCreate(Uri documentUri, VSProjectContext? projectContext, bool versioned)
31+
public bool TryCreate(
32+
Uri documentUri,
33+
VSProjectContext? projectContext,
34+
bool versioned,
35+
[NotNullWhen(true)] out DocumentContext? context)
3236
{
3337
var filePath = documentUri.GetAbsoluteOrUNCPath();
3438

3539
if (!TryGetDocumentAndVersion(filePath, projectContext, versioned, out var documentAndVersion))
3640
{
3741
// Stale request or misbehaving client, see above comment.
38-
return null;
42+
context = null;
43+
return false;
3944
}
4045

4146
var (documentSnapshot, version) = documentAndVersion;
4247
if (documentSnapshot is null)
4348
{
4449
Debug.Fail($"Document snapshot should never be null here for '{filePath}'. This indicates that our acquisition of documents / versions did not behave as expected.");
45-
return null;
50+
context = null;
51+
return false;
4652
}
4753

4854
if (versioned)
4955
{
5056
// If we were asked for a versioned document, but have no version info, then we didn't find the document
5157
if (version is null)
5258
{
53-
return null;
59+
context = null;
60+
return false;
5461
}
5562

56-
return new VersionedDocumentContext(documentUri, documentSnapshot, projectContext, version.Value);
63+
context = new VersionedDocumentContext(documentUri, documentSnapshot, projectContext, version.Value);
64+
return true;
5765
}
5866

59-
return new DocumentContext(documentUri, documentSnapshot, projectContext);
67+
context = new DocumentContext(documentUri, documentSnapshot, projectContext);
68+
return true;
6069
}
6170

6271
private bool TryGetDocumentAndVersion(

0 commit comments

Comments
 (0)