Skip to content

Commit 91b1f87

Browse files
Convert ResolveDocumentInAnyProject to Try* method
1 parent d05d81c commit 91b1f87

File tree

7 files changed

+34
-27
lines changed

7 files changed

+34
-27
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ private bool TryResolveDocument(
100100
{
101101
if (projectContext is null)
102102
{
103-
documentSnapshot = _snapshotResolver.ResolveDocumentInAnyProject(filePath);
104-
return documentSnapshot is not null;
103+
return _snapshotResolver.TryResolveDocumentInAnyProject(filePath, out documentSnapshot);
105104
}
106105

107106
if (_projectManager.TryGetLoadedProject(projectContext.ToProjectKey(), out var project) &&

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/ISnapshotResolver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Collections.Immutable;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -23,5 +24,5 @@ internal interface ISnapshotResolver
2324
/// Finds a <see cref="IDocumentSnapshot"/> for the given document path that is contained within any project, and returns the first
2425
/// one found if it does. This method should be avoided where possible, and the overload that takes a <see cref="ProjectKey"/> should be used instead
2526
/// </summary>
26-
IDocumentSnapshot? ResolveDocumentInAnyProject(string documentFilePath);
27+
bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot? document);
2728
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ public Task OpenDocumentAsync(string filePath, SourceText sourceText, int versio
111111
// We are okay to use the non-project-key overload of TryResolveDocument here because we really are just checking if the document
112112
// has been added to _any_ project. AddDocument will take care of adding to all of the necessary ones, and then below we ensure
113113
// we process them all too
114-
var document = _snapshotResolver.ResolveDocumentInAnyProject(textDocumentPath);
115-
116-
if (document is null)
114+
if (!_snapshotResolver.TryResolveDocumentInAnyProject(textDocumentPath, out var document))
117115
{
118116
// Document hasn't been added. This usually occurs when VSCode trumps all other initialization
119117
// processes and pre-initializes already open documents.

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/SnapshotResolver.cs

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

44
using System;
55
using System.Collections.Immutable;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.IO;
78
using System.Linq;
89
using System.Threading;
@@ -78,24 +79,20 @@ public IProjectSnapshot GetMiscellaneousProject()
7879
return _projectManager.GetLoadedProject(MiscellaneousHostProject.Key);
7980
}
8081

81-
public IDocumentSnapshot? ResolveDocumentInAnyProject(string documentFilePath)
82+
public bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot? document)
8283
{
8384
_logger.LogTrace($"Looking for {documentFilePath}.");
8485

85-
if (documentFilePath is null)
86-
{
87-
throw new ArgumentNullException(nameof(documentFilePath));
88-
}
89-
9086
var normalizedDocumentPath = FilePathNormalizer.Normalize(documentFilePath);
9187
var potentialProjects = FindPotentialProjects(documentFilePath);
9288

9389
foreach (var project in potentialProjects)
9490
{
95-
if (project.GetDocument(normalizedDocumentPath) is { } document)
91+
if (project.GetDocument(normalizedDocumentPath) is { } projectDocument)
9692
{
9793
_logger.LogTrace($"Found {documentFilePath} in {project.FilePath}");
98-
return document;
94+
document = projectDocument;
95+
return true;
9996
}
10097
}
10198

@@ -105,11 +102,13 @@ public IProjectSnapshot GetMiscellaneousProject()
105102
if (miscellaneousProject.GetDocument(normalizedDocumentPath) is { } miscDocument)
106103
{
107104
_logger.LogTrace($"Found {documentFilePath} in miscellaneous project.");
108-
return miscDocument;
105+
document = miscDocument;
106+
return true;
109107
}
110108

111109
_logger.LogTrace($"{documentFilePath} not found in {string.Join(", ", _projectManager.GetProjects().SelectMany(p => p.DocumentFilePaths))}");
112110

113-
return null;
111+
document = null;
112+
return false;
114113
}
115114
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentContextFactoryTest.cs

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

44
using System;
55
using System.Collections.Immutable;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.IO;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -217,9 +218,16 @@ public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFil
217218
public IProjectSnapshot GetMiscellaneousProject()
218219
=> throw new NotImplementedException();
219220

220-
public IDocumentSnapshot? ResolveDocumentInAnyProject(string documentFilePath)
221-
=> documentFilePath == _documentSnapshot?.FilePath
222-
? _documentSnapshot
223-
: null;
221+
public bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot? document)
222+
{
223+
if (documentFilePath == _documentSnapshot?.FilePath)
224+
{
225+
document = _documentSnapshot;
226+
return true;
227+
}
228+
229+
document = null;
230+
return false;
231+
}
224232
}
225233
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/SnapshotResolverTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ public async Task TryResolveDocumentInAnyProject_AsksPotentialParentProjectForDo
2424
var snapshotResolver = await CreateSnapshotResolverAsync(normalizedFilePath);
2525

2626
// Act
27-
var document = snapshotResolver.ResolveDocumentInAnyProject(documentFilePath);
27+
Assert.True(snapshotResolver.TryResolveDocumentInAnyProject(documentFilePath, out var document));
2828

2929
// Assert
30-
Assert.NotNull(document);
3130
Assert.Equal(normalizedFilePath, document.FilePath);
3231
}
3332

@@ -52,10 +51,9 @@ await projectManager.UpdateAsync(updater =>
5251
});
5352

5453
// Act
55-
var document = snapshotResolver.ResolveDocumentInAnyProject(documentFilePath);
54+
Assert.True(snapshotResolver.TryResolveDocumentInAnyProject(documentFilePath, out var document));
5655

5756
// Assert
58-
Assert.NotNull(document);
5957
Assert.Equal(normalizedFilePath, document.FilePath);
6058
}
6159

@@ -69,7 +67,7 @@ public async Task TryResolveDocumentInAnyProject_AsksPotentialParentProjectForDo
6967
await snapshotResolver.InitializeAsync(DisposalToken);
7068

7169
// Act
72-
var document = snapshotResolver.ResolveDocumentInAnyProject(documentFilePath);
70+
Assert.False(snapshotResolver.TryResolveDocumentInAnyProject(documentFilePath, out var document));
7371

7472
// Assert
7573
Assert.Null(document);

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/TestSnapshotResolver.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Collections.Immutable;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
@@ -38,6 +39,9 @@ public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFil
3839
public IProjectSnapshot GetMiscellaneousProject()
3940
=> _miscProject;
4041

41-
public IDocumentSnapshot? ResolveDocumentInAnyProject(string documentFilePath)
42-
=> null;
42+
public bool TryResolveDocumentInAnyProject(string documentFilePath, [NotNullWhen(true)] out IDocumentSnapshot? document)
43+
{
44+
document = null;
45+
return false;
46+
}
4347
}

0 commit comments

Comments
 (0)