dfhack icon indicating copy to clipboard operation
dfhack copied to clipboard

Code style automation

Open ymber opened this issue 5 years ago • 2 comments

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.

ymber avatar Apr 03 '20 21:04 ymber

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

myk002 avatar Nov 29 '23 08:11 myk002

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.

lethosor avatar Nov 29 '23 19:11 lethosor