From eb5f92ae89381bb9efb08a29bab49f69d80b3f5a Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 2 Oct 2024 12:16:09 +1000 Subject: [PATCH 1/4] Fix the bug --- .../Formatting/DocumentOnTypeFormattingEndpoint.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs index 7012b8173d7..b64fbc30c2f 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs @@ -89,7 +89,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormatting return null; } - if (_razorFormattingService.TryGetOnTypeFormattingTriggerKind(codeDocument, hostDocumentIndex, request.Character, out var triggerCharacterKind)) + if (!_razorFormattingService.TryGetOnTypeFormattingTriggerKind(codeDocument, hostDocumentIndex, request.Character, out var triggerCharacterKind)) { return null; } From bbc53f041984bbb99d7c9f75a1e3e1cd79f570d4 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 2 Oct 2024 12:16:20 +1000 Subject: [PATCH 2/4] Fix incorrect method name --- .../Formatting/RemoteFormattingService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/RemoteFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/RemoteFormattingService.cs index 29d47f91bc3..b9921462a6a 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/RemoteFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/RemoteFormattingService.cs @@ -97,10 +97,10 @@ public ValueTask GetOnTypeFormattingTriggerKindAsync( => RunServiceAsync( solutionInfo, documentId, - context => IsValidOnTypeFormattingTriggerAsync(context, linePosition, triggerCharacter, cancellationToken), + context => GetOnTypeFormattingTriggerKindAsync(context, linePosition, triggerCharacter, cancellationToken), cancellationToken); - private async ValueTask IsValidOnTypeFormattingTriggerAsync(RemoteDocumentContext context, LinePosition linePosition, string triggerCharacter, CancellationToken cancellationToken) + private async ValueTask GetOnTypeFormattingTriggerKindAsync(RemoteDocumentContext context, LinePosition linePosition, string triggerCharacter, CancellationToken cancellationToken) { var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false); var sourceText = codeDocument.Source.Text; From e4acdcb9df224918fc071ea190057017a213c480 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 2 Oct 2024 12:16:32 +1000 Subject: [PATCH 3/4] Add a test to prove it wasn't a formatting bug --- .../CSharpStatementBlockOnTypeFormattingTest.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CSharpStatementBlockOnTypeFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CSharpStatementBlockOnTypeFormattingTest.cs index 9354fecb02c..465befd46c2 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CSharpStatementBlockOnTypeFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CSharpStatementBlockOnTypeFormattingTest.cs @@ -123,6 +123,23 @@ await RunOnTypeFormattingTestAsync( triggerCharacter: ';'); } + [Fact] + public async Task Semicolon_PropertyGet() + { + await RunOnTypeFormattingTestAsync( + input: """ + @code { + private string Name {get;$$} + } + """, + expected: """ + @code { + private string Name { get; } + } + """, + triggerCharacter: ';'); + } + [Fact] public async Task Semicolon_AddsLineAtEndOfDocument() { From 4ae58dee540aabee706cd10690a2ac5b4a34efcb Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 2 Oct 2024 12:29:00 +1000 Subject: [PATCH 4/4] =?UTF-8?q?Add=20a=20test=20to=20validate=20the=20fix,?= =?UTF-8?q?=20and=20fix=20existing=20tests,=20and=20one=20more=20bug=20?= =?UTF-8?q?=F0=9F=A4=A6=E2=80=8D=E2=99=82=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DocumentOnTypeFormattingEndpoint.cs | 3 +- .../DocumentOnTypeFormattingEndpointTest.cs | 43 ++++++++++++++++++- .../FormattingLanguageServerTestBase.cs | 3 +- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs index b64fbc30c2f..04c740f1e6e 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs @@ -89,7 +89,8 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormatting return null; } - if (!_razorFormattingService.TryGetOnTypeFormattingTriggerKind(codeDocument, hostDocumentIndex, request.Character, out var triggerCharacterKind)) + if (!_razorFormattingService.TryGetOnTypeFormattingTriggerKind(codeDocument, hostDocumentIndex, request.Character, out var triggerCharacterKind) || + triggerCharacterKind == RazorLanguageKind.Razor) { return null; } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentOnTypeFormattingEndpointTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentOnTypeFormattingEndpointTest.cs index 7d313bfffa3..a91c2f4821c 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentOnTypeFormattingEndpointTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentOnTypeFormattingEndpointTest.cs @@ -4,8 +4,10 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; +using Microsoft.AspNetCore.Razor.Test.Common; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Protocol; +using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.LanguageServer.Protocol; using Xunit; using Xunit.Abstractions; @@ -81,7 +83,7 @@ public async Task Handle_OnTypeFormatting_RemapFailed_ReturnsNull() var uri = new Uri("file://path/test.razor"); var documentContext = CreateDocumentContext(uri, codeDocument); - var formattingService = new DummyRazorFormattingService(); + var formattingService = new DummyRazorFormattingService(RazorLanguageKind.CSharp); var optionsMonitor = GetOptionsMonitor(enableFormatting: true); var htmlFormatter = new TestHtmlFormatter(); @@ -183,7 +185,7 @@ public async Task Handle_OnTypeFormatting_UnexpectedTriggerCharacter_ReturnsNull var uri = new Uri("file://path/test.razor"); var documentContextFactory = CreateDocumentContextFactory(uri, codeDocument); - var formattingService = new DummyRazorFormattingService(); + var formattingService = new DummyRazorFormattingService(RazorLanguageKind.CSharp); var optionsMonitor = GetOptionsMonitor(enableFormatting: true); var htmlFormatter = new TestHtmlFormatter(); @@ -205,4 +207,41 @@ public async Task Handle_OnTypeFormatting_UnexpectedTriggerCharacter_ReturnsNull // Assert Assert.Null(result); } + + [Fact] + public async Task Handle_OnTypeFormatting_ExpectedTriggerCharacter_ReturnsNotNull() + { + // Arrange + TestCode content = """ + @code { + private string Goo {get;$$} + } + """; + var codeDocument = CreateCodeDocument(content.Text, [new SourceMapping(new SourceSpan(17, 0), new SourceSpan(17, 0))]); + var sourceText = SourceText.From(content.Text); + var uri = new Uri("file://path/test.razor"); + + var documentContextFactory = CreateDocumentContextFactory(uri, codeDocument); + var formattingService = new DummyRazorFormattingService(RazorLanguageKind.CSharp); + + var optionsMonitor = GetOptionsMonitor(enableFormatting: true); + var htmlFormatter = new TestHtmlFormatter(); + var endpoint = new DocumentOnTypeFormattingEndpoint( + formattingService, htmlFormatter, optionsMonitor, LoggerFactory); + var @params = new DocumentOnTypeFormattingParams() + { + TextDocument = new TextDocumentIdentifier { Uri = uri, }, + Character = ";", + Position = sourceText.GetPosition(content.Position), + Options = new FormattingOptions { InsertSpaces = true, TabSize = 4 } + }; + Assert.True(documentContextFactory.TryCreate(uri, out var documentContext)); + var requestContext = CreateRazorRequestContext(documentContext); + + // Act + await endpoint.HandleRequestAsync(@params, requestContext, DisposalToken); + + // Assert + Assert.True(formattingService.Called); + } } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs index 10e9266ccef..da04938029b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs @@ -48,7 +48,8 @@ public Task> GetDocumentFormattingChangesAsync(Docume public Task> GetCSharpOnTypeFormattingChangesAsync(DocumentContext documentContext, RazorFormattingOptions options, int hostDocumentIndex, char triggerCharacter, CancellationToken cancellationToken) { - throw new NotImplementedException(); + Called = true; + return SpecializedTasks.EmptyImmutableArray(); } public Task TryGetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray edits, RazorFormattingOptions options, CancellationToken cancellationToken)