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

[FUSE] Add source and line mappings for usings #9949

Merged
merged 9 commits into from
Feb 28, 2024

Conversation

chsienki
Copy link
Member

Using statements can have an optional semicolon, which is added automatically if the user doesn't write one.
Track if the user explicitly wrote the semicolon so we know where the user written code starts and ends.

@chsienki chsienki added this to the 17.10 P2 milestone Feb 16, 2024
@chsienki chsienki requested a review from a team as a code owner February 16, 2024 20:02
@chsienki
Copy link
Member Author

@dotnet/razor-compiler for review please.

//cc @ryzngard

@chsienki chsienki added the area-compiler Umbrella for all compiler issues label Feb 16, 2024
Using statements can have an optional semicolon, which is added automatically if the user doesn't write one.
Track if the user explicitly wrote the semicolon so we know where the user written code starts and ends.
Write out the semicolon if the user didn't explicitly provide it
@chsienki chsienki force-pushed the fuse/using_mapping2 branch from 30cffce to 525f9ca Compare February 16, 2024 20:06
@@ -9,7 +9,7 @@ namespace Test
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
#line (1,2)-(2,1) 1 "x:\dir\subdir\Test\TestComponent.cshtml"
using Microsoft.AspNetCore.Components.RenderTree;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example with an explicit semicolon

@chsienki chsienki changed the title Add source and line mappings for usings [FUSE] Add source and line mappings for usings Feb 16, 2024
@@ -34,9 +33,14 @@ public override void WriteUsingDirective(CodeRenderingContext context, UsingDire
{
if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
using (context.CodeWriter.BuildEnhancedLinePragma(node.Source.Value, context, characterOffset: 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider creating a tracking issue for the characterOffset = 0 bug so we don't forget about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I realized while working on a different PR that this is also wrong. In an enhanced line directive, the offset is optional https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/enhanced-line-directives#detailed-design

So, to have an effective value of 0 you have to omit the value altogether, physically writing 0 is invalid, and unlike the other values it's not 1-based. Will fix.

@@ -989,7 +989,7 @@ namespace MyApp.Pages
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line 2 ""Pages/Index.razor""
#line (2,2)-(3,1) 1 ""Pages/Index.razor""
using SurveyPromptRootNamspace;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test where we have a space/newline before the explicit semicolon? And where we have multiple using statements on one line?

using System ;using System
; using System;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test to cover this. Interestingly the parsing treats @using namespace[ ]; as being a using statement, with a markup literal ; which is weird, but we at least map what was parsed correctly.

@chsienki chsienki requested a review from a team as a code owner February 22, 2024 19:41
@chsienki
Copy link
Member Author

@333fred for a second review, thanks!

@chsienki chsienki merged commit d4236bb into dotnet:features/fuse Feb 28, 2024
12 checks passed
davidwengier added a commit that referenced this pull request Mar 6, 2024
…ng directives (#9991)

Fixes #9946
Fixes #8671

Roslyn won't offer to generate using directives into hidden regions
unless an instance of an `ISpanMappingService` [is
provided](https://github.com/dotnet/roslyn/blob/39848683a068122de144949117a9e5111bfd42ba/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs#L65).
In VS Code, and in future in cohosting, this is not the case. The fix I
am proposing here is to simply allow the using directive block to not be
in a hidden region. Most using directives are already fully mapped
anyway (with more coming with #9949)
and even for those that aren't mapped, since there is no debuggable code
in the using directive block, there seems to me to be no downside to
having default mappings. The change ensures that whatever comes after
the using directives (attributes, class declaration, something else in
future) is correctly hidden.

The only tooling changes here are tests. We could change the property
that Roslyn checks because it is now redundant, but turns out that is
[hardcoded in
Roslyn](https://github.com/dotnet/roslyn/blob/main/src/Tools/ExternalAccess/Razor/RazorSpanMappingServiceWrapper.cs#L29)
anyway. The tests are a little funny, because they only validate user
scenarios that already work today, but interestingly wouldn't have
worked in our test code before, as our test code does not have a span
mapping service.

NOTE: This will conflict with the above mentioned PR, but not in any
interesting ways.

Reviewing commit-at-a-time might make sense, if only to make things
easier because of the giant commit to update all of the test baselines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues New Feature: Fuse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants