dotnet / format

Home for the dotnet-format command
MIT License
1.94k stars 172 forks source link

formatter with library with same output. #866

Open worldleadertechSteven opened 4 years ago

worldleadertechSteven commented 4 years ago

using COM.KuMao.Admin.Business; using COM.KuMao.Admin.Entity.Activity; using App.Admin.Common; using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Web.Mvc; /n/ using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Web.Mvc; using COM.KuMao.Admin.Business; using COM.KuMao.Admin.Entity.Activity; using App.Admin.Common; /n/ In my project, there are two format with using library on different files, please provide the command line methods to help me to change it with the same format(either one format).

JoeRobich commented 4 years ago

Hi @worldleadertechSteven,

dotnet-format will help you standardize your using directives when you configure the "Organize using directive" settings (see https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#organize-using-directives) in your .editorconfig file.

If you do not have an .editorconfig file for you code, you may want to look at an example .editorconfig (see https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options#example-editorconfig-file).

Organizing imports is one of our whitespace formatters, which means you can run it with one of the command lines listed on https://github.com/dotnet/format/tree/master/docs#specify-a-workspace-required

worldleadertechSteven commented 4 years ago

I have tested it, it is okay for me. But it is too slowly and the control is too difficult. But I have found other solution, just right click with Remove and sorting using. is it have command line methods to help me make each file Remove and sorting using, I have many project to do it, if each file using the new solution, it is quit too waste time and the effectiveness is not quite good.

JoeRobich commented 4 years ago

If you are using Visual Studio, you can run Code Cleanup on your Solution or Project. Code Cleaup includes the ablity to organize your usings. See https://docs.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2019#apply-code-styles

worldleadertechSteven commented 4 years ago

Thank you for helping. This solution is quick good. But I have many project with more than 100. If I using this solution, it is quit too waste time and repeated the action with Code Cleanup on each project. Please provide the command line method to made it with automatic. if you can provide the solution how to Code Cleanup with the project in command line, I think it is the best methods to solve this issues.

jmarolf commented 4 years ago

Please provide the command line method to made it with automatic.

@JoeRobich has already given instructions for how to use dotnet-format to do this on the commandline. Can you explain this in more detail why this is not working?

worldleadertechSteven commented 3 years ago

This is not working because the using library is not sorting, and cannot make same format on each cs file space between on using library and namespace class.

JoeRobich commented 3 years ago

For formatting using directives with dotnet-format, you need an .editorconfig file to the root folder of your solution that contains the following:

.editorconfig (Taken from these docs https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#dotnet_sort_system_directives_first)

root = true

# .NET formatting rules
[*.{cs,vb}]
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

You could then run the following command from the root folder of your solution:

dotnet format -f -v diag

Some formatting options have defaults and will be applied to your code during whitespace formatting. To get full control over how your code is formatted we recommend using a more complete .editorconfig that contains your preferences (see example https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options#example-editorconfig-file).