Format If-else on Different Line in C++ VSCode - c++

I'm using the C++ Extension for VSCode (Visual Studio Code).
This format's my code when I save my C++ file. But the format results in if-statement and curly braces on same lines rather than on the new line.(I have already set format Curly Braces on Same Line)
C++ VSCode Formatted
if(...){
//...
}else if(...)
//...
}else{
//...
}
What I Want C++ VSCode Formatted Code to Look Like
if(...){
//...
}
else if(...){
//...
}
else{
//...
}
How can I make If-else in C++ format on the differernt line in Visual Studio Code?

You need to have C/C++ extension(just check if you have it). Original
1st idea
Go File-> Preferences -> Settings
Search for C_Cpp.clang_format_fallbackStyle
Click Edit, Copy to Settings
Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"
(Change from "Visual Studio" to "LLVM", "Google" or "WebKit")
Something like this:
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
Also check documentation: here and another one
2nd idea
Install C# FixFormat extension
View > Extension
Search "C# FixFormat"
Install
Shift + Alt + F
If it complains about multiple formatters, then press the Configure button and select C# FixFormat.
It is possible to go back to having open braces on a new line by going to File > Preferences > Settings. Then scroll down to Extensions, C# FixFormat configuration and uncheck Style > Braces: On Same Line

The VS Code extension uses a program called clang-format to format your code. You can change how clang-format behaves by placing a .clang-format file in your project root and double-checking that the default style is 'file' in VS Code's settings. The setting is C_Cpp: Clang_format_style.
Here is a minimal .clang-format that gets as close to your desired output as clang-format allows.
BreakBeforeBraces: Stroustrup
IndentWidth: 4
SpaceBeforeParens: Never
It cannot remove the space before the opening brace, but I'm personally on clang-format's side on that decision. You will naturally want to peruse the documentation, https://clang.llvm.org/docs/ClangFormatStyleOptions.html, to see all the options available to you.

Related

Is there a better way to set C++ format options in VS Code instead of packing them into one long string?

I am using Clang C++ on macOS in VS Code and I have overridden some C++ formatting options in 'settings.json' file. I am able to set some things from the Settings UI, but for more detailed settings I have added the following entry in 'settings.json':
"C_Cpp.clang_format_fallbackStyle": "{ EmptyLineBeforeAccessModifier: Never, AllowShortCaseLabelsOnASingleLine: true, ConstructorInitializerIndentWidth: 2, ColumnLimit: 120, PointerAlignment: Left, AllowShortIfStatementsOnASingleLine: true, SpaceAfterTemplateKeyword: false, AlignOperands: false, ContinuationIndentWidth: 2 }"
I found descriptions of these settings at Clang Format Style Options
It seems to work properly and my C++ files get nicely reformatted when I hit Alt-F, but I was wondering if there was a nicer way to specify the settings instead of using one long string in settings.json.
For example, it would be nice if I could specify each format setting on a different line or split up the string somehow into multiple lines. I don't think splitting a string into multiple lines can be done in regular JSON files, but perhaps VS code has a way?
I'm not sure if it's relevant, but I have set 'C_Cpp: Clang_format_fallback Style' to 'Google' in the Setting UI.
Any suggestions would be greatly appreciated.
Based on a comment from Frank I tried using .clang-format file and after some messing around I got it to work. I first tried putting .clang-format inside the .vscode directory in my project, but that didn't work. After reading move about clang format I moved the file to my home directory (note the file format for .clang-format is YAML instead of JSON) since it needs to be in the same directory or a parent directory of the file being formatted.
I don't think it was required, but I also ended up running brew install clang-format since I couldn't find it in my path. Once that was installed I ran clang-format -style google -dump-config>~/.clang-format and then made the overrides I wanted.
I was able to remove the long fallback string from settings.json and VS Code is still formatting nicely using my style overrides in my new .clang-format file.

Curly braces to new line - Prettier

So, I'm making a excercise to college in C
I like to use Prettier, and I never know how to
tell Prettier to don't push curly braces to new
line.
I want this
if (x > y) {
// Code
}
Instead
if (x > y)
{
// Code
}
Regarding Prettier:
Prettier doesn't allow you to change that setting. They are an "opinionated" formatting extension, and you subscribe to their opinions when you use it.
I unfortunately don't know C, so I can't tell you how to fix it in your specific case.
However,
For everyone that came to this post looking for the C# answer in VSCode:
Locate your .editorconfig files in your project
Locating .editorconfig files
Add the following line to the config files for both dotnet 5 and 6:
csharp_new_line_before_open_brace = all
File > Preferences > Settings
Enable Editor Config Support
Enabling editor config support
Enable Roslyn Analyzers
Enabling Roslyn Analyzers
Make sure your default formatter in VScode is "C#"
Setting the default formatter
Restart VS Code and the Omnisharp server
Restarting the Omnisharp server
Additionally, see all formatting options which can be added to the .editorconfig file below:
Read the documentation

VSCode: IDE does not highlight errors in C++ code anymore

Every now and then my VSCode IDE does not highlight errors in my C++ code anymore. Reloading the window, restarting VSCode or re-installing the extensions does not help.
Example:
void some_func() {
int some_num = 1;
some_nmn++; // should give error, since variablename contains typo. No error indicated.
}
In terms of C++, I am using the extensions only C/C++, however I also use a couple of other extensions that are helpful in the context, like CMake and Code Runner.
Apparently there were two things wrong with my setup: I had the wrong standard and I used the wrong Intellisense Engine.
Change the Intellisense Engine by doing Ctrl + Shift + P -> Preferences: Open Settings (UI) -> Enter C_Cpp: Intelli Sense Engine in the search bar. In the drop-down menu select Default.
After that I got a lot of unnecessary warnings regarding std, e.g. namespace std has no member optional (see post).
I fixed it by changing the standard in my workspace settings. Ctrl + Shift + P -> Open Workspace Settings (JSON). Add this line:
"C_Cpp.default.cppStandard": "c++17"
Be aware that the first setting is global and the second setting is only for your current workspace.
Something else must be wrong in your settings, or maybe some conflict.
Now as you uninstall VS Code it's possible that these settings are not erased, you can check the folders where the settings are kept and delete the them if necessary, the default locations for the settings are:
For Windows:
%APPDATA%\Code\User\settings.json
For macOS:
$HOME/Library/Application Support/Code/User/settings.json
For Linux:
$HOME/.config/Code/User/settings.json

Configuration of Visual Studio Code

So recently I started switching from sublime text 3 to Visual Studio Code, and there are a few things which bother me, so I was wondering if its possible to change/configure this:
When selecting "Open with Visual Studio Code" from the Windows Explorer on a file which is already opened, visual studio will still reopen the file in a new window.
For my C++ code, some inline method definitions are in files ending with .I (I am unable to change this). I already configured VS Code to detect those as C++ files, however Alt+O does not switch between the .I and .h / .cpp file. (It does fine between .h/.cpp)
Is there something like a hex view for binary files? Right now I am unable to edit them with VS Code.
I added some files to my exclude list in search.exclude and files.watcherExclude. Those are properly ignored in the quick-change bar, but they are still shown in the file-hierarchy on the left. Is it possible to hide them there as well? Found it: its files.exclude
When creating and saving a new file, the save dialog always starts at the project directory. Is it possible to change this, so that it defaults to the path of the last-opened file, just like in sublime text?
Is there an option/extension to have a code-minimap next to the scrollbar, like in sublime text?
Regarding 6.)
put something like this in your settings.json:
"editor.minimap.enabled": true,
"editor.minimap.maxColumn": 200,
"editor.minimap.renderCharacters": false

How to feed Visual Studio Clang-Format plugin with clang-format file?

So I downloaded, installed, and inserted into path the clang formatting plugin. I also tested it and it works for Google (Mozilla, etc.) formatting options out of the box, yet I cannot get it working with my .clang-format file. (I've put my file into the same folder as my source file, changed its encoding into UTF-8, also tried to put it into clang install folder, add file into project, write its contents inside '{key:value}' yet formatting does not happen). So how do you feed formatting file to chrome-format extension?
My file contents:
{ BasedOnStyle: "LLVM", IndentWidth: 4 }
My file name:nm.clang-format
Go to Tools->Options->LLVM/Clang->ClangFormat and put file in the Style option field.
Then place your style file named .clang-format (this is the full filename, not an extension) either in the source file's directory or one of its parent directories. Windows Explorer won't let you create filenames with leading . so you need to go to the console for this.
If like me you got confused later on where the .clang-format was living, use procmon to track the file reads of clang-format.exe
For the record, it seems that if both "Fallback Style" and "Style" are set to "file", no formatting will happen even if the style file is at its correct location. Setting "Fallback Style" to something different than "file" (e.g. "none") helps.
In VS2019 works if the clang-format file is named as .clang-format.
It must be .clang-format, not .clang-format.txt or clang-format.txt.