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] Namespace directives #10031

Merged
merged 6 commits into from
Mar 8, 2024

Conversation

chsienki
Copy link
Contributor

@chsienki chsienki commented Mar 5, 2024

Track the user portion of @namespace directives and emit #line pragmas for them

chsienki added 2 commits March 5, 2024 11:19
- Track the user written portion of the namespace
- When there is a user written portion, build a line pragma around it
@chsienki chsienki added this to the 17.10 P3 milestone Mar 5, 2024
@chsienki chsienki requested a review from a team as a code owner March 5, 2024 19:22
@chsienki
Copy link
Contributor Author

chsienki commented Mar 5, 2024

@dotnet/razor-compiler for review please.

//cc @ryzngard

LastNamespaceLocation = node.GetSourceSpan(_source);
var location = node.GetSourceSpan(_source);

var offset = 1 + NamespaceDirective.Directive.Directive.Length + 1; // @ + namespace + " "
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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>
Copy link
Member

@333fred 333fred left a 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.

@@ -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
Copy link
Contributor Author

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.

// 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, ..] }])
Copy link
Member

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, ..] }])
Copy link
Member

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?

Copy link
Contributor Author

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.

@chsienki chsienki merged commit 51289a3 into dotnet:features/fuse Mar 8, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants