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

Add feature to convert to file-scoped namespace just by typing ; after a normal namespace. #58003

Merged
merged 13 commits into from
Dec 1, 2021

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Nov 29, 2021

Fixes: #57841

Feature is very simple. But if you have:

image

And you type ; after the namespace name, then it automatically translates you to:

image

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.

Copy link
Contributor

@davidwengier davidwengier left a 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 😬

_commandHandler = (ConvertNamespaceCommandHandler)GetExportedValues<ICommandHandler>().
Single(c => c is ConvertNamespaceCommandHandler);

//_commandHandler = new ConvertNamespaceCommandHandler(
Copy link
Member

@Cosifne Cosifne Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unused comments here

Copy link
Member Author

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

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

@CyrusNajmabadi
Copy link
Member Author

@davidwengier we now respect the option around completing code with a semicolon. :)

Copy link
Contributor

@davidwengier davidwengier left a 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

@CyrusNajmabadi CyrusNajmabadi merged commit a801e65 into dotnet:main Dec 1, 2021
@CyrusNajmabadi CyrusNajmabadi deleted the convertNamespaceSemicolon branch December 1, 2021 19:45
@ghost ghost added this to the Next milestone Dec 1, 2021
Comment on lines +167 to +170
var formattedRoot = (CompilationUnitSyntax)Formatter.Format(
convertedRoot, Formatter.Annotation,
document.Project.Solution.Workspace,
options: null, rules: null, cancellationToken);
Copy link
Member

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.

Copy link
Member

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

Copy link
Contributor

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)

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.

C# 10 File Namespace usage autoformat
7 participants