Package: language-formatters-pre-commit-hooks
Language Formatters Pre Commit HooksAboutThis package provides utilities for ensuring that your code is nicely formatted by usingpre-commithooksList of pretty-format hookspretty-format-golangpretty-format-inipretty-format-javapretty-format-kotlinpretty-format-rustpretty-format-tomlpretty-format-yaml⚠: the list above could be out-of-sync respect the exposed pre-commit hooks.Please refer to.pre-commit-hooks.yamlfor a more updated list.Example UsageAdd a similar snippet into your.pre-commit-config.yamlfile-repo:https://github.com/macisamuele/language-formatters-pre-commit-hooksrev:${LATEST_SHA_OR_VERSION}hooks:-id:pretty-format-javaargs:[--autofix]-id:pretty-format-kotlinargs:[--autofix]-id:pretty-format-yamlargs:[--autofix,--indent,'2']DevelopmentThis tool uses tox as main tool to build virtual environments.To get started will be enough to runmake development.If you haveaactivatorinstalled this step will happen automatically.ContributingContributions arealwayswelcome.Fork the projectCreate your feature branch (git checkout -b my-new-feature)Add your modificationsPush to the branch (git push origin my-new-feature)Create new Pull RequestFAQHow to deal with different Google Java Formatter versions?-repo:https://github.com/macisamuele/language-formatters-pre-commit-hooksrev:...hooks:-id:pretty-format-javaargs:[--autofix,--aosp,--google-java-formatter-version=1.16.0]How to deal with multiple Java versions?This might be relevant forpretty-format-javaandpretty-format-kotlinhooks.
The hooks depends on havingjavaon version11or greater installed on your machine.As you're working withcompiled-to-JVMlanguages, we assume that you havejavainstalled on your system. You might not have the minimum required version installed.To work-around such scenario you have 2 approaches available:Have multiplejavaversions installed on your system and ensure that while running the pre-commit hooks JRE 11+ is available on yourPATHvariable (ie.PATH=${JRE_11_PATH}:${PATH} pre-commit run).Work around the issue by usingdocker.
⚠: 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 havedockerinstalled on your system.Add the followingDockerfileon your repository root (same directory where.pre-commit-config.yamlis stored)FROMpython:3.7-alpine# Install JRE-11 as we will run pre-commit hooks that depends an Java 11+RUNapkadd--no-cacheopenjdk11-jreENVPRE_COMMIT_HOME/pre-commit-docker-cacheENVPRE_COMMIT_LANGUAGE_FORMATTERS_VERSION${versionof the library to install}RUNset-x\&&pipinstall--no-cache-dirlanguage-formatters-pre-commit-hooks==${PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION}\#Runpre-commit-hooktoensurethatjarsaredownloadedandstoredinthedockerimage#Runthehooksthatyou'replanningtorunwithindocker.#Thisreducespremissionissuesaswellhasmakesalltherunfastasthelazy-dependenciesarepre-fetched&&pretty-format-java\#Updatepermissionsashookswillberunasyourhost-systemuser(yourusername)buttheimageisbuiltasroot&&chmoda+r${PRE_COMMIT_HOME}/*and the following hook into your.pre-commit-config.yamlfilerepos:-repo:localhooks:-id:pretty-format-java-in-docker# Useful to eventually SKIP pre-commit hooksname:pretty-format-java-in-docker# This is required, put something sensiblelanguage:docker# Self explanatoryentry:pretty-format-java# Hook that you want to run in dockerargs:[...]# 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-javain 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).How to use pre-downloadedgoogle-java-format jarfile?You can pass the jar file path to--google-java-formatter-jarargument:-repo:https://github.com/macisamuele/language-formatters-pre-commit-hooksrev:...hooks:-id:pretty-format-javaargs:[--google-java-formatter-jar=/usr/bin/google-java-format-1.17.0-all-deps.jar]How to use ktfmt instead of ktlint-repo:https://github.com/macisamuele/language-formatters-pre-commit-hooksrev:...hooks:-id:pretty-format-kotlinargs:[--ktfmt,--ktfmt-style=google]Supported styles are google (default), dropbox and kotlinlangLicenselanguage-formatters-pre-commit-hooksis licensed withApache License version 2.0.Changelog2.12.0 (2023-12-19)Add support forktfmtKotlin code formatter -PR #196-@aandresthanks for your contributionUpdate KTLint to version 1.0.12.11.0 (2023-10-11)Update KTLint to version 1.0.0Update GoogleJavaFormatter to 1.18.1Make library compatible with Python 3.12 -PR #182-@Delganthanks for your contribution2.10.0 (2023-07-13)Add--indentand--trailing-commasarguments forpretty-format-toml-PR #160-@maresbthanks for your contributionImprovepretty-format-kotlininteraction withktlintto prevent attempts to format not kotlin files,(Issue #162)-PR #163-@languitarthanks for your contributionImprovepretty-format-kotlinto reduce log verbosity -PR #177-@Velfithanks for your contributionAllowpretty-format-javaandpretty-format-kotlinto leverage pre downloaded JAR instead of fetching it -PR #156/PR #???-@fabasoadthanks for your contributionUpdate KTLint to 0.50.02.9.0 (2023-05-13)Update GoogleJavaFormatter to 1.17.0Update KTLint to 0.49.1Bug fix pretty-format-yaml
Sequecence item indentation should condider offset as part of the indentation,#154 (comment)has more details.
Thanks@datalogics-kamand@fmigneaultfor reporting the issue and helping me identify the underlying root cause.2.8.0 (2023-03-17)Update GoogleJavaFormatter to 1.16.02.7.0 (2023-02-18)Add support for customisable offset inpretty-format-yaml-PR #143Update KTLint to 0.48.22.6.0 (2023-01-20)Fixpretty-format-tomlto be compatible with latesttoml-sortlibraries - Thanks@liblafand@stewartHutchinsfor the support on having toml prettification working again
The fix has been carried over multiple PRs (PR #134,PR #136andPR #137).Internal build fix (failures caused bytoxmajor release) -PR #141, inspired fromPR #135- Thanks@malmans2for the supportUpdate KTlint to 0.48.1 -PR #140- Thanks@detouchedfor the upgrade2.5.0 (2022-12-05)Lift JDK 16+ restriction -PR #123-@harti2006thanks for your contributionUpdate KTlint to 0.47.1 -PR #125pretty_format_rust does no longer use explicit rust versions -PR #1262.4.0 (2022-07-01)Update GoogleJavaFormatter to 1.15.0Update KTlint to 0.45.1Ensure Python 3.10 support and drop Python3.6 guaranteed support -PR #114/PR #115Updated prettifier library for INI files (fromconfigobjtoconfig_formatter) to provide more deterministic output and proper comments handling -PR #113-@Delganthanks for your contributionpretty-format-yamlallows customization of max line length -PR #104More explicit error messages in case of prettifier failires -PR #116Use explicit encoding within INI prettifier -PR #102-@hbrethanks for your contribution2.3.0 (2022-02-17)Update GoogleJavaFormatter to 1.14.0Update KTlint to 0.44.0Use explicit encoding within YAML prettifier -PR #92-@passionsfruchtthanks for your contribution2.2.0 (2021-08-08)Make download of external artifacts resilient to systems with temporary directory on different disk partitions -@psmitand@kbalstonthanks for your contributionMake usage of Google Java Formatter compatible with JDK16+ -@ostryathanks for your contributionUpdate GoogleJavaFormatter to 1.11.0Bump KTlint to 0.42.1Misc github workflow updates (testing on Python 3.9, better tracking of tool versions tested, etc.)Improved error message in case of Google Java Formatter and KTLint not supported Java Versionℹ:pretty-format-javanow supports Java 16+
⚠:pretty-format-kotlinsupports Java up to Java 152.1.0 (2021-05-28)Bump KTlint to 0.40.0Update GoogleJavaFormatter to 1.10.02.0.0 (2021-01-16)Preserve comments in while formattinginifiles.PR #45-@Skylion007thanks for your contributionPreserve comments in while formattingtomlfiles.PR #46-@Skylion007thanks for your contribution⚠ Drop Python2 support.PR #48Update KTLint to 0.40.01.6.1 (2020-10-31)Internal fix of downloaded files path.PR #431.6.0 (2020-10-24)Update KTLint to 0.39.0Update GoogleJavaFormatter to 1.9Runpretty-format-javaserially to prevent multiple-downloads of the same Java artifact.PR #23-@ineitithanks for your contributionInternal update of download logic to reduce race coditions while download big artifacts from network.PR #24Bump minpre-commitsupported version.PR #27Allowpretty-format-javato modify the Google Java Formatter to use (--google-java-formatter-versionCLI argument).PR #30Allowpretty-format-kotlinto modify the KTLint to use (--ktlint-versionCLI argument).PR #30Enhance security in commands execution (prevent shell-injection).PR #381.5.0 (2020-06-16)Add--preserve-quotesargument intopretty-format-yaml.PR #16-@vbisseriethanks for your contribution1.4.2 (2020-06-09)Update KTLint to 0.37.11.4.1 (2020-06-03)Update KTLint to 0.37.01.4.0 (2020-05-20)Improve handling of multi-document YAML files.PR #3-@dan-cohnthanks for your contributionpretty-format-javadoes default to Google style. Add--aospargument for Android Open Source Project style.PR #8-@ChenAndrewthanks for your contribution.Update GoogleJavaFormatter to 1.81.3.2 (2020-01-25)Definitive packaging fix1.3.1 (2020-01-24)Update Packaging informations:warning: This versionbroke module retrieval(:disappointed:) while improving quality of PyPi uploaded information. You're recommended to use a more recent version of the library.1.3.0 (2020-01-24)Update KTLint to 0.36.0Enhangepretty-format-yamlto deal with YAML files containing primitive types only -PR #1-@dan-cohnthanks for your contribution1.2.5 (2019-11-22)Update KTLint to 0.35.01.2.4 (2019-07-19)Update KTLint to 0.34.0 and fix KTLint GitHub link1.2.3 (2019-02-14)Update Google Java Formatter to 1.7 and KTlint to 0.30.01.2.2 (2018-11-20)pretty-format-rust fails ifcargo fmtfails1.2.1 (2018-11-20)no-diff release1.2.0 (2018-11-20)Bump KTlint to 0.29.0Remove duplicated filenames from command execution1.1.3 (2018-09-02)Last fix to cargo invocations to use the environmentally defined toolchain1.1.2 (2018-09-02)Bump KTlint to 0.27.01.1.1 (2018-09-02)Ensure that generated files end with a new lineAllow rust toolchain customization viaRUST_TOOLCHAINenvironment variable1.1.0 (2018-07-29)Add pretty formatters for INI, Rust and TOML files1.0.1 (2018-07-20)Improve detection of modified files from kotlin formatter1.0.0 (2018-07-20)Initial release: added pretty formatters for Golang, Java, Kotlin and YAML