Skip to content

Set up pre-commit hook for clang-format #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
Language: Cpp
BasedOnStyle: Microsoft

# Indentation
IndentWidth: 2 # 2 spaces per indent
AccessModifierOffset: -2
IndentAccessModifiers: false # Align access modifiers to braces
NamespaceIndentation: All # Indent namspace contents
ConstructorInitializerIndentWidth: 2

# Comments which match this regex will be unformatted (and therefore can be longer or have more whitespace than other comments)
CommentPragmas: '^\*\*'


# Alignment
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
# For future versions of clang-format
# AlignFunctionDeclarations: false
# AlignFunctionPointers: false
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true

# Newlines
ColumnLimit: 0
BreakBeforeBraces: Allman # Braces on their own lines
SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line
AlwaysBreakTemplateDeclarations: true # Put template on their own lines
AllowShortBlocksOnASingleLine: Never
# On a newer version of clang-format, replace with BinPackArguments: OnePerLine
BinPackArguments: false # Don't allow multiple function arguments on the same line unless they all fit
BinPackParameters: false # Same but for parameters
PackConstructorInitializers: NextLine
AllowShortFunctionsOnASingleLine: None
BreakBeforeBinaryOperators: NonAssignment # Put binary operators after a line break, rather than before
AllowShortIfStatementsOnASingleLine: Never

# Spaces
SpaceBeforeParens: ControlStatementsExceptControlMacros
SpaceAfterCStyleCast: true
PointerAlignment: Left

# Includes
IncludeBlocks: Regroup # Regroup includes based on config
IncludeCategories:
- Regex: '(^"|\.hpp)' # 'local' includes
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '\/' # Library includes
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*' # Everything else
Priority: 1
SortPriority: 0
CaseSensitive: false
22 changes: 22 additions & 0 deletions .github/workflows/pre_commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: gridkit-bot pre-commit

# Won't run on develop/main directly
on: [pull_request]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- uses: actions/[email protected]
- uses: pre-commit/[email protected]
- uses: EndBug/[email protected]
# Only need to try and commit if the action failed
if: failure()
with:
fetch: false
committer_name: GitHub Actions
committer_email: [email protected]
message: Apply pre-commmit fixes
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
.vscode/
*.DS_Store
build/
*.DS_Store
*.DS_Store
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.7
hooks:
- id: clang-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-toml
- id: forbid-new-submodules
- id: end-of-file-fixer
- id: check-yaml
30 changes: 24 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ Each class should be implemented in a `*.cpp` and a `*.hpp` files with
the same name. Files containing collection of standalone functions should
have a descriptive name starting with lowercase character.

### Using `clang-format`

Coding style rules are specified in `.clang-format` file in GridKit root
directory. Use
```shell
clang-format -i <filename>
```
to make sure your code follows the style guidelines.

### Line length

Keep line length below 80 characters unless a longer line is needed for better
clarity of the code. If your line is longer than 90 characters, you are
probably doing something wrong.

`clang-format` is configured so it does not enforce line length; that is
developer's responsibility. Break the long lines at places where it will
improve readability of the code. Running `clang-format` after that will help
align the code properly in accordance with GridKit coding style.

### Local variables naming

Expand Down Expand Up @@ -98,10 +117,10 @@ constants with long names, use underscores to separate words in the constant
name. Use all caps (screaming snake case).

```c++
constexpr double Pi = 3.1415; // No, the constant name should be all caps
constexpr double Pi = 3.1415; // No, use all caps for the constant name
constexpr double SQRT_TWO = 1.4142 // Yes
constexpr double SQRTTWO_ = 1.4142 // No, there is a trailing underscore
constexpr double EXP = 2.7183 // Yes
constexpr double SQRTTWO = 1.4142 // No, the two words not separated by "_"
constexpr double EXP = 2.7183 // Yes
```

### Pointers and references
Expand All @@ -115,7 +134,7 @@ int & n; // No, the reference symbol is a part of `int&` type
```

### Indentation
Use only spaces for indentation, not tabs. Indent size is 4 spaces.
Use only spaces for indentation, not tabs. Indent size is 2 spaces.

When defining a class, the code blocks after `private`, `public` and `protected`
should be aligned with opening/closing braces. There should be an empty line
Expand All @@ -138,7 +157,7 @@ protected:
### Braces
All braces should follow Allman style:
```c++
namespace someNamespace
namespace SomeNamespace
{
//some code
}
Expand Down Expand Up @@ -321,4 +340,3 @@ class Matrix // No, class is outside GridKit namespace
{
// matrix code
};

Loading