
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
language-formatters-pre-commit-hooks
Advanced tools
This package provides utilities for ensuring that your code is nicely formatted by using pre-commit hooks
pretty-format-golangpretty-format-inipretty-format-javapretty-format-kotlinpretty-format-rustpretty-format-toml ⚠️ removes comments under certain conditionspretty-format-yaml⚠: the list above could be out-of-sync respect the exposed pre-commit hooks.
Please refer to .pre-commit-hooks.yaml for a more updated list.
Add a similar snippet into your .pre-commit-config.yaml file
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ${LATEST_SHA_OR_VERSION}
hooks:
- id: pretty-format-java
args: [--autofix]
- id: pretty-format-kotlin
args: [--autofix]
- id: pretty-format-yaml
args: [--autofix, --indent, '2']
This tool uses tox as main tool to build virtual environments.
To get started will be enough to run make development.
If you have aactivator installed this step will happen automatically.
Contributions are always welcome.
git checkout -b my-new-feature)git push origin my-new-feature) - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-java
args: [--autofix, --aosp, --google-java-formatter-version=1.16.0]
This might be relevant for pretty-format-java and pretty-format-kotlin hooks.
The hooks depends on having java on version 11 or greater installed on your machine.
As you're working with compiled-to-JVM languages, we assume that you have java installed on your system. You might not have the minimum required version installed.
To work-around such scenario you have 2 approaches available:
Have multiple java versions installed on your system and ensure that while running the pre-commit hooks JRE 11+ is available on your PATH variable (ie. PATH=${JRE_11_PATH}:${PATH} pre-commit run).
Work around the issue by using docker.
⚠: This approach has been tested (at the time of writing) on Linux and MacOS.
The latter approach should be preferred if you cannot install an additional JRE version on your system or if doing is unfeasible (e.g. on a CI system). Please note you need to have docker installed on your system.
Add the following Dockerfile on your repository root (same directory where .pre-commit-config.yaml is stored)
FROM python:3.7-alpine
# Install JRE-11 as we will run pre-commit hooks that depends an Java 11+
RUN apk add --no-cache openjdk11-jre
ENV PRE_COMMIT_HOME /pre-commit-docker-cache
ENV PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION ${version of the library to install}
RUN set -x \
&& pip install --no-cache-dir language-formatters-pre-commit-hooks==${PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION} \
# Run pre-commit-hook to ensure that jars are downloaded and stored in the docker image
# Run the hooks that you're planning to run within docker.
# This reduces premission issues as well has makes all the run fast as the lazy-dependencies are pre-fetched
&& pretty-format-java \
# Update permissions as hooks will be run as your host-system user (your username) but the image is built as root
&& chmod a+r ${PRE_COMMIT_HOME}/*
and the following hook into your .pre-commit-config.yaml file
repos:
- repo: local
hooks:
- id: pretty-format-java-in-docker # Useful to eventually SKIP pre-commit hooks
name: pretty-format-java-in-docker # This is required, put something sensible
language: docker # Self explanatory
entry: pretty-format-java # Hook that you want to run in docker
args: [...] # Arguments that would would pass to the hook (as if it was local)
files: ^.*\.java$ # File filter has to be added ;)
By doing the following, the selected hook (pretty-format-java in the example) will be executed within the docker container.
Side note: We're not embedding the Dockerfile in the repository as this is more a workaround to support whom cannot of installing a more recent Java version on the library-user system and as such we are not planning to fully support this other than giving possible solutions (Java 11+ was released in September, 2018).
You can pass the jar file path to --google-java-formatter-jar argument:
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-java
args: [--google-java-formatter-jar=/usr/bin/google-java-format-1.17.0-all-deps.jar]
You can pass the jar file path to the --ktlint-jar argument:
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-kotlin
args: [--ktlint-jar=/usr/bin/ktlint.jar]
You can pass the jar file path to the --ktfmt-jar argument:
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-kotlin
args: [--ktfmt, --ktfmt-jar=/usr/bin/ktfmt-0.47.jar]
Only supported for the pretty-format-java and pretty-format-kotlin-hooks
Use the corresponding [...]-checksum argument
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-java
args: [
--google-java-formatter-version=1.17.0,
--formatter-jar-checksum=33068bbbdce1099982ec1171f5e202898eb35f2919cf486141e439fc6e3a4203,
]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-kotlin
args: [
--ktlint-version=1.2.1,
--formatter-jar-checksum=2e28cf46c27d38076bf63beeba0bdef6a845688d6c5dccd26505ce876094eb92,
]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-kotlin
args: [
--ktfmt,
--ktfmt-version=0.47,
--formatter-jar-checksum=af61161faacd74ac56374e0b43003dbe742ddc0d6a7e2c1fe43e15415e65ffbd,
]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: ...
hooks:
- id: pretty-format-kotlin
args: [--ktfmt, --ktfmt-style=google]
Supported styles are google (default), dropbox and kotlinlang
language-formatters-pre-commit-hooks is licensed with Apache License version 2.0.
pretty-format-rust to better manage workspaces PR #263 - @GUIpsppkg_resources--indent and --trailing-commas arguments for pretty-format-toml - PR #160 - @maresb thanks for your contributionpretty-format-kotlin interaction with ktlint to prevent attempts to format not kotlin files, (Issue #162) - PR #163 - @languitar thanks for your contributionpretty-format-kotlin to reduce log verbosity - PR #177 - @Velfi thanks for your contributionpretty-format-java and pretty-format-kotlin to leverage pre downloaded JAR instead of fetching it - PR #156 / PR #??? - @fabasoad thanks for your contributionpretty-format-yaml - PR #143pretty-format-toml to be compatible with latest toml-sort libraries - Thanks @liblaf and @stewartHutchins for the support on having toml prettification working again
The fix has been carried over multiple PRs (PR #134, PR #136 and PR #137).tox major release) - PR #141, inspired from PR #135 - Thanks @malmans2 for the supportconfigobj to config_formatter) to provide more deterministic output and proper comments handling - PR #113 - @Delgan thanks for your contributionpretty-format-yaml allows customization of max line length - PR #104ℹ: pretty-format-java now supports Java 16+
⚠: pretty-format-kotlin supports Java up to Java 15
ini files. PR #45 - @Skylion007 thanks for your contributiontoml files. PR #46 - @Skylion007 thanks for your contributionpretty-format-java serially to prevent multiple-downloads of the same Java artifact. PR #23 - @ineiti thanks for your contributionpre-commit supported version. PR #27pretty-format-java to modify the Google Java Formatter to use (--google-java-formatter-version CLI argument). PR #30pretty-format-kotlin to modify the KTLint to use (--ktlint-version CLI argument). PR #30--preserve-quotes argument into pretty-format-yaml. PR #16 - @vbisserie thanks for your contributionpretty-format-java does default to Google style. Add --aosp argument for Android Open Source Project style. PR #8 - @ChenAndrew thanks for your contribution.:warning: This version broke module retrieval (:disappointed:) while improving quality of PyPi uploaded information. You're recommended to use a more recent version of the library.
pretty-format-yaml to deal with YAML files containing primitive types only - PR #1 - @dan-cohn thanks for your contributioncargo fmt failsRUST_TOOLCHAIN environment variableFAQs
List of pre-commit hooks meant to format your source code.
We found that language-formatters-pre-commit-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.