Skip to content

Commit

Permalink
Fix on type formatting (#10949)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Oct 2, 2024
2 parents 2e9deb5 + 4ae58de commit 044a9ee
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public ValueTask<Response> GetOnTypeFormattingTriggerKindAsync(
=> RunServiceAsync(
solutionInfo,
documentId,
context => IsValidOnTypeFormattingTriggerAsync(context, linePosition, triggerCharacter, cancellationToken),
context => GetOnTypeFormattingTriggerKindAsync(context, linePosition, triggerCharacter, cancellationToken),
cancellationToken);

private async ValueTask<Response> IsValidOnTypeFormattingTriggerAsync(RemoteDocumentContext context, LinePosition linePosition, string triggerCharacter, CancellationToken cancellationToken)
private async ValueTask<Response> GetOnTypeFormattingTriggerKindAsync(RemoteDocumentContext context, LinePosition linePosition, string triggerCharacter, CancellationToken cancellationToken)
{
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
var sourceText = codeDocument.Source.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public Task<ImmutableArray<TextChange>> GetDocumentFormattingChangesAsync(Docume

public Task<ImmutableArray<TextChange>> GetCSharpOnTypeFormattingChangesAsync(DocumentContext documentContext, RazorFormattingOptions options, int hostDocumentIndex, char triggerCharacter, CancellationToken cancellationToken)
{
throw new NotImplementedException();
Called = true;
return SpecializedTasks.EmptyImmutableArray<TextChange>();
}

public Task<TextChange?> TryGetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> edits, RazorFormattingOptions options, CancellationToken cancellationToken)
Expand Down

0 comments on commit 044a9ee

Please sign in to comment.