-
Notifications
You must be signed in to change notification settings - Fork 229
Allow components in global namespace #10086
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
Conversation
4521fef to
114bac6
Compare
114bac6 to
9de3c17
Compare
| public (string Type, string Namespace) GetNames() | ||
| => _names ??= (_type.ToDisplayString(), _type.ContainingNamespace.ToDisplayString()); | ||
| => _names ??= (_type.ToDisplayString(), | ||
| _type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat)); |
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.
_type.ContainingNamespace.ToDisplayString() would be "<global namespace>"; with SymbolExtensions.FullNameTypeDisplayFormat we get an empty string instead (this format was already used in other places, just not everywhere)
| #nullable disable | ||
|
|
||
| public static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace) | ||
| => TryComputeNamespace(document, fallbackToRootNamespace: fallbackToRootNamespace, allowEmptyRootNamespace: false, out @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.
MVC scenarios still call this overload. They already kinda work - you can see that in the added test - they use a constant fallback namespace name, but do not append hash to class name, so users could be referencing them. Razor components in global namespace, on the other hand, used also fallback class name with appended hash so it would be nearly impossible for users to reference them.
|
Not sure how common this is, but I would expect that formatting of Razor components in the global namespace would not work. With on-type formatting specifically, editing these files could be a pretty annoying experience. |
Is there something I should change to make it work? |
|
I think expecting you to fix the formatting engine is unreasonable. Maybe if we're really lucky, then we'd just need to change the numbers in this line from I guess it would be nice to ensure there is some way we can find out if a particular Razor document is in the global namespace? Either a bool, or maybe a "base indent" we can use? That way at least we're set up for success. Personally I don't expect this will be too common that I'd block merging it on formatting being correct. Though having said that, I've no idea what the experience for users would actually be like :) |
|
Oh, so the concern is about indentation? Components in the global namespace are actually still indented as if they were emitted inside |
|
That's how they're indented before we ask Roslyn to format the generated code, and then interpret the results. |
| </boo> | ||
| """); | ||
| """, | ||
| inGlobalNamespace: inGlobalNamespace); |
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've tried testing formatting on component in global namespace (debugged through it to verify the generated C# code really doesn't contain the namespace block). It works. But I'm not sure if this is the correct place or correct way to test this.
I've also tried this in VS - and the design-time doc is using namespace __GeneratedComponent (my guess is RootNamespace is set to null) which is wrong but at least formatting should work. I will try to see if that wrong namespace can be fixed as well (without breaking formatting).
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.
If the test is generating the code you expect, then great. Perhaps my fears are unfounded, or its only certain edge cases that would be affected. Either way, adding the infrastructure for us to test it, like you have, will be very valuable so thank you very much for 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.
I ended up doing what you suggested to fix formatting. Thanks!
|
@dotnet/razor-compiler @dotnet/razor-tooling this is ready for review; thanks |
davidwengier
left a comment
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.
Tooling LGTM. Thanks for the extra test coverage.
|
@dotnet/razor-compiler for reviews, thanks |
333fred
left a comment
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.
Compiler changes LGTM. Didn't review the IDE layer changes.
| var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat); | ||
| var fullName = string.IsNullOrEmpty(containingNamespace) | ||
| ? type.Name | ||
| : $"{containingNamespace}.{type.Name}"; |
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.
Would prefer if we were more direct with the condition here:
| var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat); | |
| var fullName = string.IsNullOrEmpty(containingNamespace) | |
| ? type.Name | |
| : $"{containingNamespace}.{type.Name}"; | |
| var fullName = type.ContainingNamespace.IsGlobalNamespace | |
| ? type.Name | |
| : $"{type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat)}.{type.Name}"; |
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.
Also, is it possible to have a component in a nested type? If so, will this handle that correctly?
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.
Also, is it possible to have a component in a nested type? If so, will this handle that correctly?
I don't think it's possible. (You've already asked me this previously in #9689 (comment) :D)
In any case, the previous code did "{namespace}.{typeName}" and I've just changed it to omit the "{namespace}." prefix when needed, so nothing should change at this front, right?
edbeb7a to
1836b11
Compare
Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1954771.
Fixes #10356.