DFHack / dfhack

Memory hacking library for Dwarf Fortress and a set of tools that use it
Other
1.84k stars 463 forks source link

Code style automation #1535

Open ymber opened 4 years ago

ymber commented 4 years ago

DFHack has a code style specification and developing would be easier if things around that were automated. A config for astyle and CI checks to make sure C++ files are formatted would cover it.

# "One True Brace Style"
--style=1tbs

# 4 spaces per indentation level and per tab stop
--indent=spaces=4

# replaces tabs with spaces in non-indent sections, except in strings
--convert-tabs

# force LF line endings
--lineend=linux

# attach brackets to class and struct inline function definitions.
--attach-inlines

# 'char *foo' instead of 'char* foo'
--align-pointer=name

# maximum length for a single unbroken line
--max-code-length=100

# 'foo ||\nbar' instead of 'foo\n|| bar'
--break-after-logical

# indent 'public:' 'protected:' 'private:' deeper than 'class' or 'struct'
--indent-classes

# indent 'case' lines deeper than 'switch'
--indent-switches

# indent later lines of multi-line preprocessor directives
--indent-preproc-define

# indent comments on column 1 to match the code block they are in
--indent-col1-comments

# indent multi-line control block headers like 'if (foo\nbar)'
--min-conditional-indent=0

# add space around operators with two operands
--pad-oper

# add brackets to unbracketed one line conditional statements
--add-brackets

# remove extra space padding around parentheses
--unpad-paren

# insert space padding around parentheses on the inside only
--pad-paren-in

The first 4 options in there will make files meet the style guide in Contributing.rst except for sorting includes and file encoding. The rest aren't specified in the style guide but it's worth considering standardizing them.

CI checks for formatting could use astyle with --dry-run to check if any files would be changed.

myk002 commented 7 months ago

another option that integrates with our current use of pre-commit.ci: https://github.com/pocc/pre-commit-hooks

lethosor commented 7 months ago

My only worry with that one is that it appears to rely on system packages, and I've experienced issues e.g. with different major versions of clang-format producing slightly different output. (otherwise I have no complaints about clang-format.)

There are some other pre-commit hooks out there that attempt to vendor formatter binaries, with varying degrees of success. It's hard to make that reliable on all platforms, especially Linux.