-
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] Namespace directives #10031
[FUSE] Namespace directives #10031
Conversation
- Track the user written portion of the namespace - When there is a user written portion, build a line pragma around it
@dotnet/razor-compiler for review please. //cc @ryzngard |
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeDocumentExtensions.cs
Outdated
Show resolved
Hide resolved
LastNamespaceLocation = node.GetSourceSpan(_source); | ||
var location = node.GetSourceSpan(_source); | ||
|
||
var offset = 1 + NamespaceDirective.Directive.Directive.Length + 1; // @ + namespace + " " |
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.
What if there are multiple spaces (or other whitespae characters), like @namespace Test
- will that work?
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.
Yes, it will treat the 'user portion' as Test
. I'll update a test to include that.
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 test, and we now generate even more correctly due to using the tokens.
…DocumentExtensions.cs Co-authored-by: Jan Jones <jan.jones.cz@gmail.com>
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.
I'm not sure what I think of this. Just like the using
change, this information should just be put into the SyntaxTree
, rather than reconstructed from other information later. I don't want to see us keep going down this path rather than doing things the right way.
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs
Show resolved
Hide resolved
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeDocumentExtensions.cs
Outdated
Show resolved
Hide resolved
Add a test with whitespace
@@ -1,5 +1,5 @@ | |||
Document - | |||
NamespaceDeclaration - (11:0,11 [18] x:\dir\subdir\Test\TestComponent.cshtml) - MyApp.Components | |||
NamespaceDeclaration - (11:0,11 [16] x:\dir\subdir\Test\TestComponent.cshtml) - MyApp.Components |
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.
Note with the latest change this is actually a design time update too, but its so minor I'm not worried about it causing issues with design time docs and so doesn't seem worth trying to plumb stuff through to keep the original values.
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeDocumentExtensions.cs
Outdated
Show resolved
Hide resolved
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeDocumentExtensions.cs
Outdated
Show resolved
Hide resolved
// In practice, this should never be null and always start with 'namespace'. Just being defensive here. | ||
if (directiveContent != null && directiveContent.StartsWith(NamespaceDirective.Directive.Directive, StringComparison.Ordinal)) | ||
var children = node.Body?.ChildNodes().ToImmutableArray(); | ||
if (children is [_, CSharpCodeBlockSyntax { Children: [ _, CSharpSyntaxNode @namespace, ..] }]) |
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.
What nodes do the discards represent? Maybe add a comment. Or even asserts.
// In practice, this should never be null and always start with 'namespace'. Just being defensive here. | ||
if (directiveContent != null && directiveContent.StartsWith(NamespaceDirective.Directive.Directive, StringComparison.Ordinal)) | ||
var children = node.Body?.ChildNodes().ToImmutableArray(); | ||
if (children is [_, CSharpCodeBlockSyntax { Children: [ _, CSharpSyntaxNode @namespace, ..] }]) |
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.
How do we know this is a @namespace
directive? Shouldn't we check or assert that?
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.
We know its a namespace because of the DirectiveDescriptor
check above. I'm not sure it's worth asserting anything here; we're using the layout of what is parsed so it shouldn't be possible for it to be different. If we ever change the parsing then the namespace tests would start failing anyway, so I don't think it buys us anything.
Track the user portion of
@namespace
directives and emit#line
pragmas for them