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

Fixing HTML attribute commit #10897

Merged
merged 2 commits into from
Sep 18, 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 @@ -36,6 +36,11 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
{
ResolveProvider = true,
TriggerCharacters = _completionListProvider.AggregateTriggerCharacters.ToArray(),
// This is the intersection of C# and HTML commit characters.
// We need to specify it so that platform can correctly calculate ApplicableToSpan in
// https://devdiv.visualstudio.com/DevDiv/_git/VSLanguageServerClient?path=/src/product/RemoteLanguage/Impl/Features/Completion/AsyncCompletionSource.cs&version=GBdevelop&line=855&lineEnd=855&lineStartColumn=9&lineEndColumn=49&lineStyle=plain&_a=contents
// This is needed to fix https://github.com/dotnet/razor/issues/10787 in particular
AllCommitCharacters = [" ", ">", ";", "="]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,260 @@ public class CompletionIntegrationTests(ITestOutputHelper testOutputHelper) : Ab
[IdeFact]
public async Task SnippetCompletion_Html()
{
await TestServices.SolutionExplorer.AddFileAsync(
RazorProjectConstants.BlazorProjectName,
"Test.razor",
"""
@page "Test"
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

<h1>Test</h1>

<PageTitle>Test</PageTitle>
@code {
private int currentCount = 0;

<h1>Test</h1>
private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<h1>Test</h1>
<dl>
<dt></dt>
<dd></dd>
</dl>

@code {
private int currentCount = 0;
@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<h1>Test</h1>",
stringsToType: ["{ENTER}", "d", "d"]);
}

private void IncrementCount()
[IdeFact, WorkItem("https://github.com/dotnet/razor/issues/10787")]
public async Task CompletionCommit_HtmlAttributeWithoutValue()
{
currentCount++;
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

<button></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<button disabled></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<button",
stringsToType: [" ", "d", "i", "s"]);
}
}
""",
open: true,
ControlledHangMitigatingCancellationToken);

await TestServices.Editor.WaitForComponentClassificationAsync(ControlledHangMitigatingCancellationToken);
[IdeFact]
public async Task CompletionCommit_HtmlAttributeWithValue()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

await TestServices.Editor.PlaceCaretAsync("<h1>Test</h1>", charsOffset: 1, ControlledHangMitigatingCancellationToken);
TestServices.Input.Send("{ENTER}");
TestServices.Input.Send("d");
TestServices.Input.Send("d");
<PageTitle>Test</PageTitle>

await CommitCompletionAndVerifyAsync("""
@page "Test"
<button></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<button style=""></button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "<button",
stringsToType: [" ", "s", "t", "y"]);
}

[IdeFact]
public async Task CompletionCommit_HtmlTag()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<PageTitle>Test</PageTitle>
<span

<h1>Test</h1>
<dl>
<dt></dt>
<dd></dd>
</dl>
@code {
private int currentCount = 0;

@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
""",
search: "</PageTitle>",
stringsToType: ["{ENTER}", "{ENTER}", "<", "s", "p", "a"]);
}

private void IncrementCount()
[IdeFact]
public async Task CompletionCommit_WithAngleBracket_HtmlTag()
{
currentCount++;
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

<span></span>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
""",
search: "</PageTitle>",
stringsToType: ["{ENTER}", "{ENTER}", "<", "s", "p", "a"],
commitChar: '>');
}
}
""");

[IdeFact]
public async Task CompletionCommit_CSharp()
{
await VerifyTypeAndCommitCompletionAsync(
input: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int myCurrentCount = 0;

private void IncrementCount()
{
myCurrentCount++;
}
}
""",
output: """
@page "Test"

<PageTitle>Test</PageTitle>

@code {
private int myCurrentCount = 0;

private void IncrementCount()
{
myCurrentCount++;

myCurrentCount
}
}
""",
search: "myCurrentCount++;",
stringsToType: ["{ENTER}", "{ENTER}", "m", "y", "C", "u", "r"]);
}

private async Task VerifyTypeAndCommitCompletionAsync(string input, string output, string search, string[] stringsToType, char? commitChar = null)
{
await TestServices.SolutionExplorer.AddFileAsync(
RazorProjectConstants.BlazorProjectName,
"Test.razor",
input,
open: true,
ControlledHangMitigatingCancellationToken);

await TestServices.Editor.WaitForComponentClassificationAsync(ControlledHangMitigatingCancellationToken);

await TestServices.Editor.PlaceCaretAsync(search, charsOffset: 1, ControlledHangMitigatingCancellationToken);
foreach (var stringToType in stringsToType)
{
TestServices.Input.Send(stringToType);
}

await CommitCompletionAndVerifyAsync(output, commitChar);
}

[IdeFact]
Expand Down Expand Up @@ -225,12 +430,19 @@ public enum MyEnum
""");
}

private async Task CommitCompletionAndVerifyAsync(string expected)
private async Task CommitCompletionAndVerifyAsync(string expected, char? commitChar = null)
{
var session = await TestServices.Editor.WaitForCompletionSessionAsync(HangMitigatingCancellationToken);

Assert.NotNull(session);
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
if (commitChar.HasValue)
{
TestServices.Input.Send(commitChar.Value.ToString());
}
else
{
Assert.True(session.CommitIfUnique(HangMitigatingCancellationToken));
}

var textView = await TestServices.Editor.GetActiveTextViewAsync(HangMitigatingCancellationToken);
var text = textView.TextBuffer.CurrentSnapshot.GetText();
Expand Down