5

I just started using Visual Studio Code. In my company they use Whitesmiths indentation style, but I cannot find a way to set it on VSC. I'm programming in C, so I installed the C/C++ plugin. In this, the "Indent: Braces" option looks like the one for me, but it does not seem to work.

My json looks like this:

"C_Cpp.vcFormat.indent.braces": true,
"editor.detectIndentation": false,
"editor.tabSize": 3,

i set detect.Indentation to false, because i don't want it to interfere with indent.braces. This is the user json. I did not modify the workspace nor the folder settings.

However, my code still looks like this

void main()
{
   if(condition)
   {
      do something
   }
   else
   {
      whatever
   }
}

while it should look like this

void main()
   {
   if(condition)
      {
      do something
      }
   else
      {
      whatever
      }
   }

what am I missing? Is there a way to set this from the VSC settings without using the plugin?

4
  • 5
    I have seen a lot of styles but this is one of the ugliest I have seen, apart from the one where each function argument is on a separate line Commented Apr 14, 2021 at 14:27
  • you can setup your document formatter (prettier) to try to use this style and use Format Document on save Commented Apr 14, 2021 at 14:28
  • 1
    How does the company autoindent the style? If they're going to do style enforcement, they need [to provide] a program to do reindent. There's probably a VS hook that can invoke it. PS. I agree with rioV8. This is the ugliest style I've ever seen [in 40+ years]. It is so bad that, personally, I would refuse to use it. Because I know that it would impede my progress and my code would be more buggy when using it. Commented Apr 14, 2021 at 14:42
  • 4
    there's no accounting for taste. I personally this is the only logical style around for C++ code.. Commented Apr 30, 2021 at 22:47

4 Answers 4

6

PFE32 is an ancient editor that will indent in that style. The much newer Code::Blocks editor will handle it as well. Use Astyle to reformat to that style.

By the way, this is the style used with the original Whitesmith's tool chain. It was the first commercially available C compiler. This is the style that God uses when he writes in C. It is not ugly. It is correct, proper for the Backus-Naur definition of the language. It is pretty, just like me. ;-D

Sign up to request clarification or add additional context in comments.

3 Comments

This is the style that God uses when he writes in C I guess God never had complex if conditions that took more than one line? I find one advantage of the ANSI style is that by putting the opening brace on its own line, aligned vertically with the if, finding the break between a multi-line if condition and dependent compound statement is easy.
Not my downvote. I suspect someone didn't appreciate your tongue-in-cheek endorsement of the Whitesmith code style.
It is not ugly. It is correct -- "You're goddamn right." Also, there are now two people who have succumbed in public to HERESY. Y'all know who you are. There is still time to get right with the Lord. Embrace the original C coding style. Shun the heretic! Shun the unbeliever!
0

this post is for c# but it worked for me using the visual studio c++ formatter:

Visual Studio Code custom indentation style

my full .json file:

{
    "[c]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "editor.autoClosingQuotes": "never",
    "editor.autoClosingBrackets": "never",
    "workbench.list.automaticKeyboardNavigation": false,
    "workbench.settings.openDefaultKeybindings": true,
    "update.enableWindowsBackgroundUpdates": false,
    "update.mode": "manual",
    "C_Cpp.formatting": "vcFormat",
    "C_Cpp.vcFormat.indent.braces": true
}

Update:

This no longer seems to work as of version 1.82, in particular "C_Cpp.vcFormat.indent.braces": does not seem to have any impact anymore. And looking at the generic formatter spec (https://code.visualstudio.com/api/language-extensions/language-configuration-guide) there doesn't seem to be any way to have the indentation occur at the brace, only on the line after it.

Thus the best option now is

"editor.autoIndent": "keep"

which more or less leaves you responsible for doing your own indentation, but at least prevents the editor from adding unwanted indents.

3 Comments

Hi Al Ro, i already saw the therad you linked but i don't understand what it means with Omnisharp. Anyway, i copied your entire json but it still does not work...i copied it to AppData\Roaming\Code\User\settings.json, which in my understanding is the json that is applied to every project on my machine...should i copy it to a different one?
use the menu options inside vscode to open your settings json, that's the safest way.
"C_Cpp.vcFormat.indent.braces": true doesn't seem to do anything anymore, seems like maybe a code regression.
0

I user Whitesmith-style with IDEA, Intellij etc. I think all programs in IDEA lineup like CLion could handle it well. (Except GoLand since it requires GO-styling to compile). They And are really good editors worth paying for.

For VS Code I wish I had a solution since the world needs more good old Whitesmith formatting =)

You might have to customize the code style, but it is easy in a visual way. Some config details, Spaces and Wrapping and Braces tab does the trick

Comments

0

I have the same problem and have just figured out how to do it using vs code (to get the Whitesmith style). The way I did it was to use the vcFormat formatter rather than the default clang formatter. So go to settings in vscode (Ctrl ,) and edit the settings page (not the json page). I entered the search term 'format' and scrolled till I saw the setting

C_Cpp: Formatting

as seen in diagram below. screenshot of settings page showing where to change formatter type

I changed this from the default of clangFormat to vcFormat, then I changed a further two items. I actually changed them in the settings dialog, but for conciseness I am showing the changes in the following json version of the settings file:

"C_Cpp.vcFormat.indent.braces": true,
"C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "newLine",

Finally I had to add the following two items to get it to work for me. These changes are also shown in their json page.

"editor.tabSize": 2,
"editor.detectIndentation": false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.