diff --git a/doc/Coding-conventions.md b/doc/Coding-conventions.md index 5d534a1d70..7efabad8a4 100644 --- a/doc/Coding-conventions.md +++ b/doc/Coding-conventions.md @@ -2,6 +2,10 @@ We mainly follow the standard Java coding conventions and most of the conventions from the books _Effective Java_ [1] and _Clean Code_ [4]. Some exceptions and additional rules are listed below. Each rule starts with _DO_, _CONSIDER_, _AVOID_ or _DO NOT_, according to [2]. +## Automatic Formatting and Copyright Headers + +See [Build.md](Build.md). For the formatting rules, check [java-common.gradle.kts](https://github.com/ftsrg/theta/blob/master/buildSrc/src/main/kotlin/java-common.gradle.kts) + ## Source files * **DO** encode files in UTF-8. **DO NOT** use any other format. diff --git a/doc/Development.md b/doc/Development.md index 33b20f1891..5296332cfa 100644 --- a/doc/Development.md +++ b/doc/Development.md @@ -29,7 +29,30 @@ See [Build.md](Build.md). - Theta can be imported into [IntelliJ IDEA](https://www.jetbrains.com/idea/) as an existing Gradle project by selecting the _build.gradle.kts_ file in the root of the repository. - If you want to build the whole project (and not just run a single test for example), make sure to run the _build task of the whole project_. This can be done by opening the Gradle tab, and then selecting _theta / theta / Tasks / build / build_, right clicking and selecting _Run_. -- Code styling and copyright noticing should be automatically set up for the ones accepted by the Github CI. It is not recommended to change them. +- For code formatting and copyright header generation we use **spotless**. + - Locally, formatting can be done manually by `gradlew spotlessApply` + - Reformatting when saving a file can be set up by installing the `Spotless Applier` plugin (`Ctrl+Alt+s > Plugins`) and enabling it when saving (`Ctrl+Alt+s > Actions on Save`, enable `Run spotless`) + - On Linux, the following pre-commit hook can be used to disable commits without formatting: + ``` + #!/bin/bash + + # Run Spotless Check without interfering with uncommitted files + ./gradlew spotlessCheck 2>/dev/null 1>&2 + + # Capture the exit status of spotlessCheck + SPOTLESS_STATUS=$? + + # If spotlessCheck fails, prevent the commit + if [ $SPOTLESS_STATUS -ne 0 ]; then + echo "Code format check failed. Please run './gradlew spotlessApply' to fix formatting issues." + exit 1 + fi + + # If spotlessCheck passes, proceed with the commit + echo "Spotless check passed." + exit 0 + ``` + *(write the above into `.git/hooks/pre-commit` and enable execution rights on the file)* ## Coding conventions