7

We would like to Reorder using statements and keep them outside of the namespace. ReSharper puts them inside the namespace when reordering.

Visual Studio or Resharper functionality for placement of using directives asks how to put usings inside the namespace. That is not what we would like to do. Its answer suggest going to ReSharper > Options > Code Editing → C# → Code Style → Add 'using' directive to the deepest scope. Despite having un-selected that, on re-ording the usings, ReSharper places the usings inside the namespace.

enter image description here

How can we Reorder using statements and keep them outside the namespace?

Additional things we have tried:

Our StyleCop.Analyzers ruleset includes the following directive related rules:

SA1200 Using directives must be placed correctly
SA1208 System using directives must be placed before other using directives
SA1209 Using alias directives must be placed after other using directives
SA1210 Using directives must be ordered alphabetically by namespace

Given those rules along with the selection in Options not to "Add usings to the deepest scope", we receive the following warning on build:

SA1200 Using directive must appear within a namespace declaration.

How can we configure ReSharper to enforce that using directives must appear outside a namespace declaration?

Community
  • 1
  • 1
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467

1 Answers1

7

Add stylecop.json to the project with the following setting:

{  
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "orderingRules": {
      "usingDirectivesPlacement": "outsideNamespace"
    }
  }
}

Then, enable the use of stylecop.json by editing the ProjectName.csproj file, locating the following item in the project file...

<None Include="stylecop.json" />

... and changing the definition to the following.

<AdditionalFiles Include="stylecop.json" />
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 2
    Just to clarify, this is changing the settings for the StyleCopAnalyzers, which means that ReSharper isn't involved here. – citizenmatt Sep 27 '16 at 17:41
  • @citizenmatt Oddly enough, ReSharper honors the setting. That is, when we `Reorder using` with ReSharper, it puts them outside of the namespace if and only if we've added this. Is that your experience? Or, am I missing something? – Shaun Luttin Sep 27 '16 at 19:40
  • 2
    ReSharper has no knowledge of this file, so can't be honouring the setting. When you're invoking the "reorder usings", is this from the Alt+Enter menu, and if so, does the item have a little Visual Studio icon next to it? I think what's happening is that you're actually invoking Visual Studio's actions from ReSharper's menus. But it's not ReSharper doing the work here. – citizenmatt Sep 27 '16 at 22:08