Skip to content

Commit b31ace0

Browse files
Fix nullability of IDocumentSnapshot
For a long while IDocumentSnapshot's FileKind, FilePath, and TargetPath properties have all been annotated as nullable. It turns out that the only reason for this was ImportDocumentSnapshot, which has been removed. So, we can now allow these properties to be correctly annotated as non-nullable,
1 parent 8de0cf0 commit b31ace0

File tree

25 files changed

+48
-76
lines changed

25 files changed

+48
-76
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot
3030
// multiple parses/regenerations across LSP requests that are all for the same document version.
3131
lock (_codeDocumentCache)
3232
{
33-
var key = new DocumentKey(documentSnapshot.Project.Key, documentSnapshot.FilePath.AssumeNotNull());
33+
var key = new DocumentKey(documentSnapshot.Project.Key, documentSnapshot.FilePath);
3434
_codeDocumentCache[key] = codeDocument;
3535
}
3636
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ public bool Equals(IDocumentSnapshot? x, IDocumentSnapshot? y)
2626
}
2727

2828
public int GetHashCode(IDocumentSnapshot obj)
29-
{
30-
var filePath = obj.FilePath.AssumeNotNull();
31-
return FilePathComparer.Instance.GetHashCode(filePath);
32-
}
29+
=> FilePathComparer.Instance.GetHashCode(obj.FilePath);
3330
}
3431
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private async Task PublishDiagnosticsAsync(IDocumentSnapshot document, Cancellat
121121

122122
lock (_publishedDiagnostics)
123123
{
124-
var filePath = document.FilePath.AssumeNotNull();
124+
var filePath = document.FilePath;
125125

126126
// See if these are the same diagnostics as last time. If so, we don't need to publish.
127127
if (_publishedDiagnostics.TryGetValue(filePath, out var previousDiagnostics))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ internal class GeneratedDocumentSynchronizer(
1919
public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot document)
2020
{
2121
var hostDocumentVersion = document.Version;
22-
var filePath = document.FilePath.AssumeNotNull();
22+
var filePath = document.FilePath;
2323

2424
// If the document isn't open, and we're not updating buffers for closed documents, then we don't need to do anything.
25-
if (!_projectManager.IsDocumentOpen(document.FilePath) &&
25+
if (!_projectManager.IsDocumentOpen(filePath) &&
2626
!_languageServerFeatureOptions.UpdateBuffersForClosedDocuments)
2727
{
2828
return;
2929
}
3030

3131
// If the document has been removed from the project, then don't do anything, or version numbers will be thrown off
3232
if (!_projectManager.TryGetLoadedProject(document.Project.Key, out var project) ||
33-
!project.ContainsDocument(document.FilePath))
33+
!project.ContainsDocument(filePath))
3434
{
3535
return;
3636
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.Comparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public int GetHashCode(IDocumentSnapshot obj)
3737
{
3838
var hash = HashCodeCombiner.Start();
3939
hash.Add(obj.Project.Key.Id, FilePathComparer.Instance);
40-
hash.Add(obj.FileKind.AssumeNotNull(), FilePathComparer.Instance);
40+
hash.Add(obj.FileKind, FilePathComparer.Instance);
4141
return hash.CombinedHash;
4242
}
4343
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
148148
{
149149
foreach (var relatedDocument in oldProject.GetRelatedDocuments(document))
150150
{
151-
var relatedDocumentFilePath = relatedDocument.FilePath.AssumeNotNull();
151+
var relatedDocumentFilePath = relatedDocument.FilePath;
152152

153153
if (newProject.TryGetDocument(relatedDocumentFilePath, out var newRelatedDocument))
154154
{
@@ -169,7 +169,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
169169

170170
void EnqueueIfNecessary(IDocumentSnapshot document)
171171
{
172-
if (!_projectManager.IsDocumentOpen(document.FilePath.AssumeNotNull()) &&
172+
if (!_projectManager.IsDocumentOpen(document.FilePath) &&
173173
!_options.UpdateBuffersForClosedDocuments)
174174
{
175175
return;

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractRazorComponentDefinitionService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Threading;
55
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Razor;
76
using Microsoft.AspNetCore.Razor.Language;
87
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
98
using Microsoft.CodeAnalysis.Razor.Logging;
@@ -62,7 +61,7 @@ internal abstract class AbstractRazorComponentDefinitionService(
6261
return null;
6362
}
6463

65-
var componentFilePath = componentDocument.FilePath.AssumeNotNull();
64+
var componentFilePath = componentDocument.FilePath;
6665

6766
_logger.LogInformation($"Definition found at file path: {componentFilePath}");
6867

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ internal class DocumentContext(Uri uri, IDocumentSnapshot snapshot, VSProjectCon
2323

2424
public Uri Uri { get; } = uri;
2525
public IDocumentSnapshot Snapshot { get; } = snapshot;
26-
public string FilePath => Snapshot.FilePath.AssumeNotNull();
27-
public string FileKind => Snapshot.FileKind.AssumeNotNull();
26+
public string FilePath => Snapshot.FilePath;
27+
public string FileKind => Snapshot.FileKind;
2828
public IProjectSnapshot Project => Snapshot.Project;
2929

3030
public TextDocumentIdentifier GetTextDocumentIdentifier()

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,13 @@ private static async Task<RazorCodeDocument> GenerateCodeDocumentAsync(
217217
var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false);
218218

219219
return forceRuntimeCodeGeneration
220-
? projectEngine.Process(source, fileKind: document.FileKind, importSources, tagHelpers)
221-
: projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers);
220+
? projectEngine.Process(source, document.FileKind, importSources, tagHelpers)
221+
: projectEngine.ProcessDesignTime(source, document.FileKind, importSources, tagHelpers);
222222
}
223223

224224
private static async Task<ImmutableArray<ImportItem>> GetImportItemsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken)
225225
{
226-
var filePath = document.FilePath.AssumeNotNull();
227-
var fileKind = document.FileKind.AssumeNotNull();
228-
229-
var projectItem = projectEngine.FileSystem.GetItem(filePath, fileKind);
226+
var projectItem = projectEngine.FileSystem.GetItem(document.FilePath, document.FileKind);
230227

231228
using var importProjectItems = new PooledArrayBuilder<RazorProjectItem>();
232229

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#endif
77

88
using System.Diagnostics;
9-
using Microsoft.AspNetCore.Razor;
109
using Microsoft.AspNetCore.Razor.ProjectSystem;
1110
using Microsoft.AspNetCore.Razor.Serialization;
1211
using Microsoft.AspNetCore.Razor.Utilities;
@@ -16,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;
1615
internal static class Extensions
1716
{
1817
public static DocumentSnapshotHandle ToHandle(this IDocumentSnapshot snapshot)
19-
=> new(snapshot.FilePath.AssumeNotNull(), snapshot.TargetPath.AssumeNotNull(), snapshot.FileKind.AssumeNotNull());
18+
=> new(snapshot.FilePath, snapshot.TargetPath, snapshot.FileKind);
2019

2120
public static ProjectKey ToProjectKey(this Project project)
2221
{

0 commit comments

Comments
 (0)