-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add feature to convert to file-scoped namespace just by typing ;
after a normal namespace.
#58003
Add feature to convert to file-scoped namespace just by typing ;
after a normal namespace.
#58003
Conversation
src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs
Show resolved
Hide resolved
src/EditorFeatures/CSharpTest/ConvertNamespace/ConvertNamespaceCommandHandlerTests.cs
Show resolved
Hide resolved
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 wonder if this should be dependent on the "complete statement on semicolon" option? I could see accidental activation of this being annoying. eg I expect if someone did it in Roslyn and didn't notice, the PR would not be approved 😬
src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs
Show resolved
Hide resolved
src/EditorFeatures/CSharpTest/ConvertNamespace/ConvertNamespaceCommandHandlerTests.cs
Show resolved
Hide resolved
_commandHandler = (ConvertNamespaceCommandHandler)GetExportedValues<ICommandHandler>(). | ||
Single(c => c is ConvertNamespaceCommandHandler); | ||
|
||
//_commandHandler = new ConvertNamespaceCommandHandler( |
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.
Some unused comments here
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.
yup. will clean up
|
||
testState.SendTypeChar(';'); | ||
testState.AssertCodeIs( | ||
@"namespace N // Goo |
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.
The format here is a little bit strange to me.
I feel it's more natural to see namespace N; // Goo
src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs
Show resolved
Hide resolved
src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs
Show resolved
Hide resolved
@davidwengier we now respect the option around completing code with a semicolon. :) |
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.
Thank you for gating this
var formattedRoot = (CompilationUnitSyntax)Formatter.Format( | ||
convertedRoot, Formatter.Annotation, | ||
document.Project.Solution.Workspace, | ||
options: null, rules: null, cancellationToken); |
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.
❗ This assumes the file is formatted according to the formatter, which is not a safe assumption. This feature is only allowed to remove a uniform number of leading spaces from each line in the namespace block, and is not allowed to force potentially incorrect formatting throughout the file.
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 saw this tweet that indicated the codefix for converting to file scoped namespaces forced a format on users https://twitter.com/rickbrewPDN/status/1463361397677379590
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.
Yah, this has come up on Discord a couple of times too, and ASP.NET ran into it too: dotnet/aspnetcore#38681 (comment)
Fixes: #57841
Feature is very simple. But if you have:
And you type
;
after the namespace name, then it automatically translates you to:This is the same thing the code-action/refactoring does. However, i've found that my intuition is to just type
;
, and i think it would be nice to just have things update cleanly without having to manually delete the curlies and format the document.