auto-indent research result and proposal

Danilo Cesar Lemes de Paula danilo.eu at gmail.com
Sat Jan 18 05:28:45 UTC 2014


So, I've been checking ways to auto indent code before commiting and I
used subsurface as a guinea pig for my tests with clang-format.

The approach I took is the same that gstreamer uses, using a git hook to
check all files that are being commited and run clang-format against them.
This means that all checked files will have to follow the same rules
(although we can tweak the hook to add exceptions). So, before a commit,
your whole file needs to pass the rules, otherwise you'll see a warning
(and this warning remains until the file is fixed by someone)

Basically, clang-format needs a .clang-format file in the root folder,
containing the rules. I started a rules file [1] that I think would make
sense (following Dirk's comments and a few more patters I saw people using).
I'm also sending a diff file [2] as a preview of what it can do.

I enjoyed the final result and I'm probably going to use in other projects.
But it's not perfect, I have a list of flaws:

1 - header files with classes: is common to use public:/private: at the
begging of the line. clang indents it with a tab.

2 - if(something) {} will be replaced by if (something) {
    } - it's cool. However, it can't recognize Qt controls statements
(Q_FOREACH) and assumes it's a function. so it will remove the space and
put a line break before the brace as in:
    q_foreach(something, something)
    {
      ....
    }
3 - it fixes all the arithmetics operators with a space and uses the
form QString* str instead of QString *str everywhere. However, inside
the non recognized control structures (q_foreach again) it thinks that
it's an arithmetic operator and expects the following usage:
    q_foreach(QString * str, list)... not cool as foreach(QString* str)

4 - it uses and expect tabstop=8 (kernel's guide expects the same), but
escaped new lines will look unindented if you use tabstop!=8.

5 - I couldn't find a way to put space after a cast, so casts will looks
like:
    (struct dive*)index.data(); instead of (struct dive*) index.data(),
which would be easier to see. IHMO.

6 - I never used git-hook before but I don't think it can be included in
the git repository. What gstreamer does, as example, is to install the
hook during the ./configure phase. I think we could do the same with qmake.


Even with those issues, I think it's a low price to be paid for
coding-style consistency in the whole code make the developer's life easier.

Again, it's only a proposal and it's understandably if it doesn't fit
subsurface's needs.

But if you guys are ok with it and want to give it a try, I can prepare
a proper commit.

that's all,

Danilo Cesar
-------------- next part --------------
AlignEscapedNewlinesLeft: false
AccessModifierOffset: 0 # so public: and private: stays at the left site
AllowAllParametersOfDeclarationOnNextLine: true
BinPackParameters: true
BreakBeforeBinaryOperators: true
BreakBeforeBraces: Stroustrup
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 0
IndentFunctionDeclarationAfterType: false #personal taste, good for long methods
IndentWidth: 8
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
UseTab: ForIndentation
PointerBindsToType: true
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.diff
Type: text/x-diff
Size: 368520 bytes
Desc: not available
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20140118/b5da6917/attachment-0001.diff>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20140118/b5da6917/attachment-0001.sig>


More information about the subsurface mailing list