Sort "usings" directive by ReSharper Clean-up
Asked Answered
O

5

26

How to setup ReSharper to call "Sort usings" in class?

Outofdoor answered 9/11, 2011 at 9:59 Comment(0)
O
14

I will answer for my question.

Using shortcut from Resharper it's not best solution. Nice tool is CodeMaid it makes few nice cleanup's when saving file like with no shortcut key press:

  • sorting usings alphabetically and remove unused
  • deleting empty lines

This tool is complement for Resharper.

Outofdoor answered 11/9, 2012 at 6:16 Comment(0)
S
8

Try using the Stylecop Resharper Plugin

Stylecop Rule SA1210: Using directives must be sorted alphabetically by the namespaces will allow you to use a quick fix to sort them by pressing ALT+ENTER.

Using Stylecop is good at helping you write clean code, but you may want to turn off some of the rules.

Safeguard answered 10/11, 2011 at 19:57 Comment(1)
+1 It is easy to use StyleCop to waste your time if you have too many rules turned on.Mcclure
R
8

Resharper (9) will sort usings if you run Code Cleanup, via menus or keyboard shortcut CTRL+E, CTRL+C

Rosemarierosemary answered 17/2, 2016 at 7:46 Comment(0)
W
2

You don't need ReSharper to do this. By Default the shortcut CTRL+E, R will remove (unused) and sort the using statements.

If you only want sorting or the shortcut isn't set on your instance of Visual Studio, go to:

  • Tools-> Options.
  • Environment -> Keyboard
  • Search for "Editor.ContextMenus.CodeWindow.OrganizeUsings".

There are a few options there to play with.

Wasting answered 5/12, 2012 at 15:34 Comment(5)
This doesn't really work - resharper overrides the newly bound shortcut every time after I press "OK".Endurable
This is probably dependent on the version of VS and ReSharper. Couldn't get this keystroke to work. However, clicking on the USING statement with resharper pops up a lightbulb icon, which includes the option to remove and sort.Gleeful
In Visual Studio 2017 the name changed to: "EditorContextMenus.CodeWindow.RemoveAndSort"Extracanonical
As of 2019: "Edit.SortUsings"Ervinervine
@Ervinervine - The name is still "EditorContextMenus.CodeWindow.RemoveAndSort" even in Visual Studio 2022. The one you mentioned "Edit.SortUsings" only sorts, and is not removing unused ones.Extracanonical
S
-5

This question already covers this:

Remove unused Usings across entire assembly

The feature is in ReSharper 4.5. If you right click on the solution, there's a Cleanup Code... item, which allows you to apply a cleanup profile to the solution. You can create a new cleanup profile from the Code Cleanup node within ReSharper options, if you want a profile to just adjust the using directives.

Solvable answered 9/11, 2011 at 10:4 Comment(3)
on my VS this only removes not used directives, that's why I'am askingOutofdoor
Do you mean alphabetically "sort" then? Your question is very vague..! I thought you mean sort as in "sort out"! Sorry if I misinterpreted.Solvable
Yes, I want to sort alphabetically by resharper not VS.Outofdoor

© 2022 - 2024 — McMap. All rights reserved.