When we last talked about formatting our C++ extension code, we were using CLion's formatter.
Long story short, we got frustrated with the limitations/misfeatures of that formatter, and decided to try using clang-format. So far we are pretty happy with it. We integrate it into CLion using the ClangFormatIJ plugin:
To use the plugin, just drop a .clang-format file in your project root, and then use a keybinding to run the formatter. We re-bound the plugin's hotkey to the default formatter keybinding (Cmd+Opt+L).
One thing that can be surprising about the plugin (but I think it's a good thing): It only reformats changed lines or selected lines, so sometimes it doesn't seem to do anything. Select the whole file if you want it to format the whole file.
Thanks,
d#
Appendix: Our .clang-format:
---
BasedOnStyle: Google
# How much whitespace?
UseTab: ForIndentation
TabWidth: 4
IndentWidth: 4
ContinuationIndentWidth: 8
SpacesBeforeTrailingComments: 1
# Line things up
AccessModifierOffset: -4 # outdent `public:`, etc
DerivePointerAlignment: false
PointerAlignment: Right # char *foo, char &bar
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
# Braces
AlwaysBreakAfterReturnType: TopLevelDefinitions
AllowShortFunctionsOnASingleLine: Inline
BreakBeforeBraces: Custom
BraceWrapping:
AfterStruct: true
AfterClass: true
AfterEnum: true
AfterFunction: true
AfterControlStatement: true
AfterNamespace: false
AfterExternBlock: false
BeforeCatch: true
SplitEmptyFunction: false
SplitEmptyRecord: false
# Put "postgres.h" and "postgres_undefs.h" first in a group of includes.
IncludeCategories:
- Regex: '"postgres(_undefs)?.h"'
Priority: 1