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..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/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; 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() { 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)