Visual Studio : Automatically insert a space after typing if(
Asked Answered
W

6

16

We have a code style checker that's run before every check-in that requires that C# if statements be formatted like:

if (condition)

However, my muscle memory has already developed for typing:

if(condition)

Is there any way to get Visual Studio 2010 to automatically insert that space if I type the if without it? I know you can set it that when you paste code, it will automatically do this, but not while typing. Thanks!

Wilterdink answered 22/3, 2011 at 21:27 Comment(2)
You are asking the wrong question. The correct question is: "How do I convince my team to follow a sane coding style (that is a style, which requires the style checker to only allow 'if(condition)' and to explicitly disallow 'if (condition)')?"Starshaped
"if (" is the correct way to do it according to the accepted C# coding standards, and most other standards require a space prior to the condition, as it happens. "if(" is plain wrong. Deleting and reinserting the last bracket in your class will format your code correctly for you.Liar
I
28

Tools > Options > Text Editor > C# > Formatting > Spacing

Then check/tick Insert space after keywords in control flow statements, which is listed under the Set other spacing options sub group.

Win.

Idea answered 22/3, 2011 at 21:31 Comment(5)
Which spacing option controls this?Centaury
"Insert space after keywords in control flow statements" - it's like, a third of the way down maybe?Idea
In VS 2015 check "other spacing options -> insert spaces inside parentheses of control flow statements"Karalynn
Excellent. I can't stand when editors decide to do things like this on their own. Always gives issues with git commits.Blackmun
VS 2022 and for whatever reason this doesn't work for me. I had to turn off automatic brace completion (C# -> Automatic brace completion) because the only thing that would trigger the reformatting of the if statement was manually typing the closing brace of the if block.Sabir
C
5

Resharper will do this with its default formatting settings. Ctrl+K+D will format the entire document. Thats how I do it. It seems like VS's formatting options may be able to help you, but I have never played with them enough.

Resharper is totally worth it for this and so many other reasons.

Centaury answered 22/3, 2011 at 21:31 Comment(2)
Agreed, only downside is unless you're only working on open source, it's not free (although not overly expensive)Idea
Yes, I have ReSharper, but I think in this case it's causing the problem... it's automatically inserting the closing brace, thus preventing VS's space fix.... ah well, I can disable that part easily enough.Wilterdink
F
4

My VS2010 does it by default after the closing bracket. Here's the option:

Tools > Options > Text Editor > C# > Formatting > General > Automatically format completed block on }

and also:

Tools > Options > Text Editor > C# > Formatting > Spacing > Set other spacing options > Insert space after keywords in control flow statements

Fannyfanon answered 22/3, 2011 at 21:32 Comment(3)
Its annoying that it only applies after you type the closing bracket. I believe Resharper is automatically adding the closing bracket, and so the vs formatting is not running. Kinda lame.Centaury
@CMP -- Yes, this appears to be what's happening in my case.Wilterdink
That is quite lame, although it also seems like the sort of thing that should be configurable in Resharper... in fact, this question appears to have the setting: #4208731 (I don't have Resharper so I can't confirm!)Fannyfanon
C
1

Yeah, I think you are looking for Tools -> Options -> Text Editor -> C# -> Formatting -> Spacing -> Set other spacing options -> Insert space after keywords in control flow statements

Corrida answered 22/3, 2011 at 21:33 Comment(0)
S
1

I have Visual C#, but as I recall it is the same in Studio.

  1. Click Tools
  2. Click Options
  3. Click 'Text Editor' on left menu
  4. Click 'C#' on left menu
  5. Click 'Formatting' on left menu
  6. Make sure all 3 options are checked in right area.
  7. Click 'Spacing' on left menu
  8. Click 'Insert space after keywords in control flow statements' in right area under the heading of 'Set other spacing options'.
  9. Click OK.
Shu answered 22/3, 2011 at 21:35 Comment(0)
G
0

Tools > Options > Text Editor > C# > Formatting > Spacing Then select all-> Press CTRL+K+D

Grandsire answered 16/9, 2021 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.