Add feature to convert to file-scoped namespace just by typing ; after a normal namespace.#58003
Conversation
davidwengier
left a comment
There was a problem hiding this comment.
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 😬
| _commandHandler = (ConvertNamespaceCommandHandler)GetExportedValues<ICommandHandler>(). | ||
| Single(c => c is ConvertNamespaceCommandHandler); | ||
|
|
||
| //_commandHandler = new ConvertNamespaceCommandHandler( |
There was a problem hiding this comment.
yup. will clean up
|
|
||
| testState.SendTypeChar(';'); | ||
| testState.AssertCodeIs( | ||
| @"namespace N // Goo |
There was a problem hiding this comment.
The format here is a little bit strange to me.
I feel it's more natural to see namespace N; // Goo
|
@davidwengier we now respect the option around completing code with a semicolon. :) |
davidwengier
left a comment
There was a problem hiding this comment.
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.
❗ 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.
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.
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.