Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge release/dev17.12 to main #10951

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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