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

Avoid expanding tabs to spaces in codegen #9247

Merged
merged 3 commits into from
Sep 8, 2023
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 @@ -68,12 +68,9 @@ int CalculatePadding()
{
break;
}
else if (@char == '\t')
{
spaceCount += writer.TabSize;
}
else
{
// Note that a tab is also replaced with a single space so character indices match.
spaceCount++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#nullable disable
#nullable restore
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml"
__o = a+b;
__o = a+b;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml"


#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Generated Location: (980:27,31 [22] )

Source Location: (69:2,29 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml)
|a+b|
Generated Location: (1211:35,38 [3] )
Generated Location: (1202:35,29 [3] )
|a+b|

Source Location: (80:2,40 [2] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml)
|
|
Generated Location: (1435:42,49 [2] )
Generated Location: (1417:42,40 [2] )
|
|

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#pragma warning restore 1998
#nullable restore
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal.cshtml"

string foo(string input) {
return input + "!";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ string foo(string input) {
return input + "!";
}
|
Generated Location: (817:21,15 [55] )
Generated Location: (814:21,12 [55] )
|
string foo(string input) {
return input + "!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#pragma warning restore 1998
#nullable restore
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal.cshtml"

string foo(string input) {
return input + "!";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#nullable disable
#nullable restore
#line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf.cshtml"

}

#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Generated Location: (744:19,1 [15] )
Source Location: (27:2,12 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf.cshtml)
|
}|
Generated Location: (937:28,15 [3] )
Generated Location: (934:28,12 [3] )
|
}|

Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,57 @@ private static int RaiseHere()
Assert.Equal(new TextSpan(originalIndex, snippet.Length), mappedSpan);
}
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9050")]
public async Task LineMapping_Tabs()
{
// Arrange
var tab = '\t';
var source = $$"""
<div>
{{tab}}@if (true)
{{tab}}{
{{tab}}{{tab}}@("code")
{{tab}}}
</div>
""";
var project = CreateTestProject(new()
{
["Shared/Component1.razor"] = source,
});
var compilation = await project.GetCompilationAsync();
var driver = await GetDriverAsync(project);

// Act
var result = RunGenerator(compilation!, ref driver);

// Assert
result.Diagnostics.Verify();

var original = project.AdditionalDocuments.Single();
var originalText = await original.GetTextAsync();
Assert.Equal(source, originalText.ToString());
var generated = result.GeneratedSources.Single();
var generatedText = generated.SourceText;
var generatedTextString = generatedText.ToString();

// Find snippets and verify their mapping.
var snippets = new[] { "true", "code" };
var expectedLines = new[] { 1, 3 };
var originalIndex = -1;
var generatedIndex = -1;
foreach (var (snippet, expectedLine) in snippets.Zip(expectedLines))
{
originalIndex = source.IndexOf(snippet, originalIndex + 1, StringComparison.Ordinal);
generatedIndex = generatedTextString.IndexOf(snippet, generatedIndex + 1, StringComparison.Ordinal);
var mapped = generated.SyntaxTree.GetMappedLineSpan(new TextSpan(generatedIndex, snippet.Length));
Assert.True(mapped.IsValid);
Assert.True(mapped.HasMappedPath);
Assert.Equal("Shared/Component1.razor", mapped.Path);
Assert.Equal(expectedLine, mapped.StartLinePosition.Line);
Assert.Equal(expectedLine, mapped.EndLinePosition.Line);
var mappedSpan = originalText.Lines.GetTextSpan(mapped.Span);
Assert.Equal(new TextSpan(originalIndex, snippet.Length), mappedSpan);
}
}
}