-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Sorting of using directives #661
Comments
I personally also like sorted imports, but I don't know if csharpier should be concerned with sorting them. So far csharpier has followed a lot of the rationale of prettier. Part of that is avoiding transforming any code and sticking to just printing code (adding/removing whitespace). This makes it fairly straightforward for csharpier to validate that changes it made do not change the behavior of the code. There are some other options for sorting usings. At work we use a combination of stylecop + csharpier. |
Related Prettier issue. I do understand not wanting to do it because of the added complexity, but having to use multiple tools to handle formatting is not exactly ideal. I hope you can consider it more. |
I think I am starting to feel less and less married to the idea that csharpier will never transform code. There are a lot of useful things it can't do if the only thing it worries about is whitespace. The challenge will be figuring out how to get the validation code to understand the transform. Although if validation is opt in, maybe that is less important. Another argument for sorting usings is that it can help avoid merge conflicts. I don't know that I've run into it often for c# (maybe because most IDEs auto add usings sorted) but I remember searching for an eslint plugin to do it specifically because of how often we ran into merge conflicts on imports in typescript files at work. |
Conditional compilation is going to cause some headaches on this. An example using System;
using System.Collections.Generic;
using Insite.Common.Dependencies;
using Insite.Common.Providers;
using Insite.WebFramework.Templating;
#if NET6_0_OR_GREATER
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Caching.Memory;
#else
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Optimization;
using System.Web.WebPages;
#endif |
Wouldn't it to be simply sorting these 3 blocks of usings separately? |
I think that's the proper way to handle ii. I don't think it will be too difficult to handle but wanted to make sure I didn't forget about it. There is the potential to introduce compilation errors if the usings are moved outside of the scope of an #if. |
While a solution is found for sorting import with CSharpier could an option to trigger the import formatter from OmniSharp be added to the VSCode extension?
The issue is that only 1 formatter can be set in VSCode. So I have to do CTRL + SHIFT + P then select "Format Document With..." then select "C#" and then save the file to trigger formatting with CSharpier. |
I think one should be careful with increasing the scope of CSharpier. I greatly appreciate that CSharpier only formats white spaces. One tool with one job is easy to understand; "Csharpier formats white space". I think this simplicity made it easier for me to convince my team to use CSharpier as they were initially not convinced to use it in favor of CSharpier brings something new that dotnet format doesn't have; wrapping long lines 😍. Sorting usings is something there exists tools for already so it will be yet another implementation. If this feature is introduced please make sure that it plays nice with the other two dotnet format commands; |
A colleague said yesterday that it would be nice if Csharpier removed unused usings. So if sorting is implemented, it makes sense to implement that as well. (Mind the feature creep) |
Prettier for Java sorts Docs: https://github.com/jhipster/prettier-java#organize-imports |
I'd love an automatic way to do that but don't believe CSharpier is the place for it. CSharpier only understands the syntax of the code and knows nothing of the semantics. Rider screws up removing unused usings in our multi-targeted project at work so it seems like a hard problem to solve and could be a bit too dangerous to do in an automatic way. |
Absolutely agree! I think CSharpier should format white space, and just that. Removing and sorting usings should be the job of another tool. It is already possible in all major IDEs, so it should also debated why we need yet another tool to do the same. |
By this point, CSharpier presumably has already moved past "format whitespace only" philosophy, as seen with the latest feature of reordering modifiers like |
This ended up being a bit tricky, but I have it working this way as of now. // comments or non #if directives that appear at the top of the file are assumed to be file headers and kept there.
// later comments stay with the using they are above
// blank lines around usings are not retained as of now
#nullable enable
global using System.Linq; // sort global first
using System; // sort System next
using NonSystem; // then sorted non-system
#if DEBUG // then any #if
using Z; // contents are not sorted as of now
using A;
#endif
using static Static; // then sorted static
using Alias = Z; // finally alias sorted by Alias, not Name
using SomeAlias = A; Retaining blank lines would be nice, I've seen some repos that use blank lines to organize into groups. Sometimes between system and non-system but also sometimes between groups within the non-system. using System;
using System.IO;
using Company.Feature;
using Company.MoreFeatures;
using Microsoft.Azure;
using Microsoft.Bing; Keeping the lines when sorting often ends up with them in places you wouldn't expect. It could turn into a "format, see where the blank lines end up then move them if needed" situation. Sorting usings within |
Scary! 👹 In general, formatting white space in a language that doesn't care about white space seems safe. In general, reordering lines doesn't seem safe because languages do care about line order. Not saying it is impossible, and of course it can be useful.
Absolutely agree on that a CLI tool or .NET plugin for code cleanup is better than using an IDE. One can use the already existing Resharper to sort usings pre-commit. I think it can be wise for a tool to have a clear philosophy of what features it should offer. I thought CSharpier was a white space formatter, which it seems to be growing out of. So what is CSharpier (becoming) @belav? :) Maybe Csharpier will become a formatter and move code around as well but never remove any? Or will it become a code cleanup tool and format and remove code based on some (linter) rules? And again I want to mention that CSharper is a nice replacement for |
It could be provided as an option, prettier defintely allows import sorting via plugins and it's very helpful. It makes the codebase more consistent and allows for manual code review to be more about functionality instead of styling, formatting or sorting. On the philosophy front, not really a fan of dogmatic approaches that can't be changed later on. Csharpier as an opinionated prettier like tool for c# with most of it's functionality would be awesome. Thanks for the great work @belav |
CSharpier has a "no option" philosophy, @gabynevada :) Plugins would be cool!
Isn't this exactly what CSharpier is now? |
It was originally @Sti2nd . I liked the firm stance that black had in that regard. It was an easy line to draw to say no to requests. But there are some non-whitespace changes that I think belong and I've struggled to put into words what I think belongs, which lead me to putting off responding to this for a bit too long. Some things I do think belong
Things I don't think belong
Things that I haven't thought about much, but could be debated
My attempt to put the new mostly whitespace only changes philosophy into words - When there are non-whitespace changes they should help avoid friction and keep code cleaner in diffs. Not change the meaning of the code. Be done to improve readability and avoid conflicts. I don't imagine that there will be that many non-whitespace changes implemented. They are somewhat of a pain do deal with in the
I have a branch with csproj formatting partially done. It moves the codebase in a direction where plugins for formatting new languages would be possible. I don't know that plugins to modify the existing formatting would be possible. From my knowledge of prettier plugins, they can support parsing and printing new languages, but aren't able to modify the existing formatters.
You're welcome! @gabynevada |
Thoughts on putting the #nullable enable
using System; vs using System;
#nullable enable As far as I can tell, Visual Studio doesn't have an opinion on the matter, but it seems that Rider prefers to put |
I usually prefer it before the using statements, makes it easier to see what files have the nullable config at first glance. |
I also prefer it before using statements at the beginning of the file. The microsoft examples seem to usually place it after the usings for some reason. |
* Initial work for sorting usings closes #661 * handling more cases * maybe fixing last edge case * Sort by alias * Fixing some edge cases. Cleaning up sorting. * Working on validation, notes for some failing to compile files * Making progress on edge cases for sorting usings * Adding even more edge cases * Some finishing touches * Self code review * minor change
It would be nice if it kept the space between different groups like the IDE (Visual Studio) adds. Alternatively, I'd be happy if this could be turned off and left to the IDE. |
I had a similar thought while looking at one of the test cases. I created #988 to look into it. |
I would like using directives to be sorted with these rules:
System
on top.using static
at the end which follows the same rules.Example:
The text was updated successfully, but these errors were encountered: