14

I've never really been a big fan of the way most editors handle namespaces. They always force you to add an extra pointless level of indentation.

For instance, I have a lot of code in a page that I would much rather prefer formatted as

namespace mycode{

class myclass{
  void function(){
    foo();
  }
  void foo(){
    bar();
  }
  void bar(){
    //code..
  }

}

}

and not something like

namespace mycode{

  class myclass{
    void function(){
      foo();
    }
    void foo(){
      bar();
    }
    void bar(){
      //code..
    }

  }

}

Honestly, I don't really even like the class thing being indented most of the time because I usually only have 1 class per file. And it doesn't look as bad here, but when you get a ton of code and lot of scopes, you can easily have indentation that forces you off the screen, and plus here I just used 2-space tabs and not 4-space as is used by us.

Anyway, is there some way to get Visual Studio to stop trying to indent namespaces for me like that?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Earlz
  • 62,085
  • 98
  • 303
  • 499
  • I think you can avoid this with ReSharper, a quite expensive solution to what should be a trivial problem though. :) – Skurmedel Feb 08 '10 at 23:00
  • yuck, Java-style formatting ;) – Thomas Levesque Feb 08 '10 at 23:04
  • I don't get it.. I don't know Java lol. if your talking about `void bar(){` all on one line, then that's just how I naturally write code since I learned C.. but VS kindly changes it to `void bar()\n{` for me.. – Earlz Feb 08 '10 at 23:07
  • Hey, I didn't see this question when I opened mine, but I noticed it now in the "Related" list. I put up a 500 bounty on it and received a great answer; since mine contains better answers I'll also vote to close this as a duplicate, even if it was asked earlier. My question is: http://stackoverflow.com/questions/3727862/is-there-any-way-to-make-visual-studio-stop-indenting-namespaces/3782433#3782433 – Andreas Bonini Sep 24 '10 at 02:46
  • @Skurmedel: I have ReSharper and I don't see this facility. If you can provide more specific information I'd be grateful. – RenniePet May 11 '16 at 21:10
  • @RenniePet: It was ages since I used ReSharper but either they removed the facility or I abused one of the settings. Sorry I can't recall better, but I remember you could control the indentations for pretty much everything. – Skurmedel May 12 '16 at 05:42
  • Anyone who's interested in this (and it has been viewed over 700 times) please vote here to try to get Microsoft to provide this option: https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/6367724-add-option-to-prevent-indentation-of-classes-insid – RenniePet May 14 '16 at 14:42

1 Answers1

5

It's a hack, but here goes:

namespace mycode{ 
#if 0
}
#endif

class myclass{
    ...
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 8
    I should've added "without really bad hacks" to my question :) – Earlz Feb 08 '10 at 22:57
  • This works with Visual Studio 2012, but not with Visual Studio 2015. With VS 2015 it considers "#if 0" to be a syntax error. Changing to "#if false" fixes that, but the editor no longer lets itself be fooled so you're back to the problem of the namespace contents being indented. Damn. – RenniePet May 14 '16 at 13:55