-
Notifications
You must be signed in to change notification settings - Fork 199
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
Conversation
@dotnet/razor-compiler for review please. //cc @ryzngard |
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
30cffce
to
525f9ca
Compare
@@ -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; |
There was a problem hiding this comment.
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
@@ -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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
...ompiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs
Outdated
Show resolved
Hide resolved
@@ -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; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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.
@333fred for a second review, thanks! |
…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.
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.