Visual Studio or Resharper functionality for placement of using directives
Asked Answered
S

7

123

I like to put my using directives inside the current namespace, and not outside as VS and Resharper per default puts them.

Does anyone know of a macro/standard functionality that sorts/removes unused using directives and puts them inside the current namespace?

Schadenfreude answered 4/5, 2009 at 16:25 Comment(0)
S
225

UPDATE - ReSharper 2016.1: This option is now moved to Code Editing → C# → Code Style → Add 'using' directive to the deepest scope

Have you tried the ReSharper option:

Languages → C# → Formatting Style → Namespace Imports → Add using directive to the deepest scope

I'm not sure whether R#'s code cleanup will reorder the existing ones for you though.

Scarron answered 5/5, 2009 at 14:55 Comment(8)
In ReSharper 6, this options is now at Code Editiong -> C# -> Namespace Imports (not under Formatting Style).Charla
I'm not sure whether R#'s code cleanup will reorder the existing ones for you though - In R# 6 it does.Cerebritis
If you want to automatically fix not only this rule, but a bunch of the other ones also, it's better to update your StyleCop to the version 4.7 or higher, because it includes ReSharper plugin: https://mcmap.net/q/180312/-visual-studio-or-resharper-functionality-for-placement-of-using-directivesCerebritis
In Resharper 9 this option moved to Code Editing -> C# -> Code StylePomerleau
in resharper 10 this now doesn't work anymore (at least on my machine :-/)Monophony
In Resharper 10, it's in the same place that @JoaoMillasch mentioned. It's under the "Reference qualification" grouping.Tilburg
Does not work in R# 10 any more. There is no such an option.Layne
in R# 10 "Reference qualification" is under C# - Formatting Style - Code StyleScopp
N
43

In ReSharper 2020, go to Options and then:

Code EditingC#Syntax StyleAdd 'using' directive to deepest scope

enter image description here

Nd answered 29/10, 2016 at 6:4 Comment(0)
T
18

As of version 8.2, this has moved. It is now here:

Within Visual Studio
Resharper -> Options...

Then in the Options dialog
Code Editing -> C# -> Namespace Imports -> Insert using directives when necessary -> Add using directive to the deepest scope

enter image description here

Thrombophlebitis answered 22/4, 2014 at 17:37 Comment(0)
A
16

As of Resharper 9 this has moved into Code Editing => C# => Code Style => Reference Qualification => Add 'using' directive to deepest scope.

Or you can use new settings search facility and search for deepest scope. The exact setting will come up:

Add 'using' directive to deepest scope

Aintab answered 13/12, 2014 at 19:5 Comment(0)
C
2

StyleCop supports ReSharper integration starting from version 4.7.

From http://stylecop.codeplex.com/:

4.7 is compatible with JetBrains R#5.1( 5.1.3000.12), R#6.0 (6.0.2202.688), R#6.1 (6.1.37.86), R#6.1.1 (6.1.1000.82) and R#7.0 (7.0.54.77)

4.7 is compatible with Visual Studio 2008, Visual Studio 2010 and Visual Studio 11

This release includes the StyleCop ReSharper plugin (compatible with ReSharper 5.1, 6.0, 6.1, 6.1.1 and 7.0)

Cerebritis answered 4/6, 2012 at 16:11 Comment(0)
K
1

I've edited my default visual studio templates to put my using directives inside the namespace.

For sorting and removing unused directives, I use Power Commands for Visual Studio 2008.

Kronfeld answered 4/5, 2009 at 16:48 Comment(0)
S
1

Update for Resharper 2020.1: This option can now be found in Options → Code Editing → C# → Syntax Style → Reference Qualification and 'using' Directives

Image for reference

Sink answered 24/4, 2020 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.