From 5cb68cbe54a2b591d4812c8466ca9915cbfab7ce Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 19 Sep 2024 11:39:15 +1000 Subject: [PATCH 1/4] Allow TextChange.NewText to be null --- .../Extensions/RoslynLspExtensions_SourceText.cs | 2 +- .../Extensions/VsLspExtensions_SourceText.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs index 5c5d391316f..7b0bf01e5a7 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs @@ -30,6 +30,6 @@ public static TextChange GetTextChange(this SourceText text, TextEdit edit) => new(text.GetTextSpan(edit.Range), edit.NewText); public static TextEdit GetTextEdit(this SourceText text, TextChange change) - => RoslynLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText.AssumeNotNull()); + => RoslynLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText ?? ""); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs index 0df608360b8..d68d3f60bb8 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs @@ -46,5 +46,5 @@ public static TextChange GetTextChange(this SourceText text, TextEdit edit) => new(text.GetTextSpan(edit.Range), edit.NewText); public static TextEdit GetTextEdit(this SourceText text, TextChange change) - => VsLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText.AssumeNotNull()); + => VsLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText ?? ""); } From ca31b61e559a9a324b3e32f5c10174d5d215adce Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 19 Sep 2024 12:08:20 +1000 Subject: [PATCH 2/4] Don't throw if a document has been removed before we got to process them --- .../ProjectSystem/ProjectSnapshotManager.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs index 40f7d967401..3d7b555d6c7 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs @@ -518,7 +518,12 @@ private static Entry ComputeNewEntry(Entry originalEntry, IUpdateProjectAction a case CloseDocumentAction(var textLoader): { - documentState.AssumeNotNull(); + // If the document being closed has already been removed from the project then we no-op + if (documentState is null) + { + return originalEntry; + } + var state = originalEntry.State.WithChangedHostDocument( documentState.HostDocument, () => textLoader.LoadTextAndVersionAsync(s_loadTextOptions, cancellationToken: default)); @@ -562,7 +567,11 @@ private static Entry ComputeNewEntry(Entry originalEntry, IUpdateProjectAction a case DocumentTextChangedAction(var sourceText): { - documentState.AssumeNotNull(); + // If the document being changed has already been removed from the project then we no-op + if (documentState is null) + { + return originalEntry; + } if (documentState.TryGetText(out var olderText) && documentState.TryGetTextVersion(out var olderVersion)) From 3feef4dff732ef330a4d86c763411fdf1cf74581 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 19 Sep 2024 13:15:44 +1000 Subject: [PATCH 3/4] Fix build --- .../Extensions/RoslynLspExtensions_SourceText.cs | 2 -- .../Extensions/VsLspExtensions_SourceText.cs | 1 - 2 files changed, 3 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs index 7b0bf01e5a7..2a7b95f56e3 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RoslynLspExtensions_SourceText.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Razor; using Microsoft.CodeAnalysis.Text; namespace Roslyn.LanguageServer.Protocol; @@ -31,5 +30,4 @@ public static TextChange GetTextChange(this SourceText text, TextEdit edit) public static TextEdit GetTextEdit(this SourceText text, TextChange change) => RoslynLspFactory.CreateTextEdit(text.GetRange(change.Span), change.NewText ?? ""); - } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs index d68d3f60bb8..1eff67c59aa 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/VsLspExtensions_SourceText.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis.Text; From cfd20a8d98e81f6abafabc62cb9a613a058f3a06 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 19 Sep 2024 14:19:59 +1000 Subject: [PATCH 4/4] Delete failing test that validates a bad assumption --- .../RazorProjectServiceTest.cs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs index 5c10d288509..84a83312b8e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs @@ -1034,25 +1034,6 @@ public async Task UpdateDocument_DocumentVersionUpdated() Assert.Equal(2, latestVersion); } - [Fact] - public async Task UpdateDocument_ThrowsForUnknownDocument() - { - // Arrange - const string ProjectFilePath = "C:/path/to/project.csproj"; - const string IntermediateOutputPath = "C:/path/to/obj"; - const string RootNamespace = "TestRootNamespace"; - const string DocumentFilePath = "C:/path/to/document.cshtml"; - - await _projectService.AddProjectAsync( - ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken); - - // Act - await Assert.ThrowsAnyAsync(() => - { - return _projectService.UpdateDocumentAsync(DocumentFilePath, s_emptyText.Replace(0, 0, "Hello World"), DisposalToken); - }); - } - [Fact] public async Task AddProject_AddsProjectWithDefaultConfiguration() {