HydroCS Wiki

Pioneering Hydrologic Computational Science

User Tools

Site Tools


howtos:how_to_format_code

This is an old revision of the document!


How to format code

We use the following code formatters to maintain consistent coding styles across different programming languages. These tools automatically format source code without changing its behavior.

Python: Black

Black is an opinionated Python code formatter with minimal configuration.

Install:

pip install black

Format:

black script.py

Check without modifying:

black --check script.py

R: Air

Air is a fast R code formatter written in Rust and developed by Posit.

Install:

pip install air-formatter

Format:

air format script.R

Check without modifying:

air format --check script.R

Shell: shfmt

shfmt formats POSIX shell, Bash, and other supported shell scripts.

Install:

go install mvdan.cc/sh/v3/cmd/shfmt@latest

Format:

shfmt -w script.sh

Check without modifying (list files needing formatting):

shfmt -l script.sh

C/C++: clang-format

clang-format automatically formats C, C++, and related languages. Formatting rules can be configured using a .clang-format file.

Install:

pip install clang-format

Format:

clang-format -i source.c

Check without modifying:

clang-format --dry-run --Werror source.c

JavaScript/JSON/YAML: Prettier

Prettier is an opinionated formatter for JavaScript, TypeScript, JSON, HTML, CSS, and other web languages.

Install:

npm config set prefix ~/.local
npm install -g prettier

Format:

prettier --write script.js

Check without modifying:

prettier --check script.js

TOML: Tombi

Tombi formats and lints TOML files.

Install:

pip install tombi

Format:

tombi format config.toml

Check without modifying:

tombi format --check config.toml

CMake: cmake-format

cmake-format formats CMakeLists.txt and other CMake scripts.

Install:

pip install cmakelang

Format:

cmake-format -i CMakeLists.txt

Check without modifying:

cmake-format --check CMakeLists.txt

Print formatted output without modifying:

cmake-format CMakeLists.txt

Makefile: mbake

mbake formats and lints Makefiles.

Install:

pip install mbake

Format:

mbake format Makefile

Check without modifying:

mbake format --check Makefile

Show formatting differences without modifying:

mbake format --diff Makefile

Automated formatting: pre-commit

pre-commit automatically runs configured formatters before each Git commit. The .pre-commit-config.yaml file defines the formatters, their versions, and applicable file types and should be version-controlled in the repository root. Pre-commit automatically installs the configured formatters in isolated environments.

Install:

pip install pre-commit

Enable automatic formatting (once per clone):

pre-commit install

Format all tracked files:

pre-commit run --all-files

Update formatter versions:

pre-commit autoupdate

If any formatter modifies files during a commit, the commit is aborted. Review and stage the formatted files before committing again.

We use this config file .pre-commit-config.yaml:

repos:
  # Basic file hygiene
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
 
  # Python: Black
  - repo: https://github.com/psf/black
    rev: 26.10.1
    hooks:
      - id: black
 
  # R: Air
  - repo: https://github.com/posit-dev/air-pre-commit
    rev: "0.13.0"
    hooks:
      - id: air-format
 
  # Shell: shfmt
  - repo: https://github.com/scop/pre-commit-shfmt
    rev: v3.15.0-1
    hooks:
      - id: shfmt

  - repo: https://github.com/pre-commit/mirrors-clang-format
    rev: v23.1.5
 
  # JavaScript/JSON/YAML: Prettier
  - repo: https://github.com/rbubley/mirrors-prettier
    rev: v3.10.1
    hooks:
      - id: prettier
        types_or: [javascript, jsx, json, yaml]
 
 
  # TOML: Tombi
  - repo: https://github.com/tombi-toml/tombi-pre-commit
    rev: v1.11.3
    hooks:
      - id: tombi-format
 
 
  # CMake: cmake-format
  - repo: https://github.com/cheshirekow/cmake-format-precommit
    rev: v0.6.13
    hooks:
      - id: cmake-format
 
  # Makefile: mbake
  - repo: https://github.com/EbodShojaei/bake
    rev: v1.5.0
    hooks:
      - id: mbake-format

Notes

  • Formatters should be run before committing changes.
  • Use the default formatting rules unless project-specific requirements justify configuration.
  • Ensure that user-installed executable directories, such as ~/.local/bin, are included in PATH.
  • Formatting checks can also be integrated into CI workflows.
howtos/how_to_format_code.1796169680.txt.gz · Last modified: by hcho

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki