# \[ANN\] JuliaFormatter.jl

**URL:** <https://discourse.julialang.org/t/ann-juliaformatter-jl/48084>\
**Category:** Package Announcements\
**Tags:** package, announcement\
**Created:** [October 10, 2020, 1:29am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084 "2020-10-10T01:29:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [October 10, 2020, 1:29am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/1 "2020-10-10T01:29:07Z")

</div>

I’ve realized this hasn’t been posted on here, despite it being registered and used for several months.

[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) is a width-sensitive formatter for the Julia programming language.

It can format:

- Julia files
- Julia code in docstrings
- Julia code in markdown files

Code is formatted based on a style, the default style is appropriately named [`DefaultStyle`](https://domluna.github.io/JuliaFormatter.jl/dev/style/) but there’s also `YASStyle` and `BlueStyle` which conform to the [YAS](https://github.com/domluna/JuliaFormatter.jl/issues/198) and [Blue](https://github.com/domluna/JuliaFormatter.jl/issues/283) style guides.

Besides styles there also [several options](https://domluna.github.io/JuliaFormatter.jl/dev/api/#JuliaFormatter.format_text-Tuple%7BAbstractString%7D) which can be set including from the indentation and maximum margin. At the time of this writing there are 16 boolean options which means **65536** different formatting outputs for an input just by fiddling with these alone!

To keep of your options you can use a [configuration file](https://domluna.github.io/JuliaFormatter.jl/dev/config/), `JuliaFormatter` will automatically detect this file and override the default options.

Recently [custom alignment detection](https://domluna.github.io/JuliaFormatter.jl/dev/custom_alignment/) has been introduced for several types of code expressions. I think this feature is awesome.

Lastly I’d like to thank @ZacLN for [CSTParser.jl](https://github.com/julia-vscode/CSTParser.jl) and everyone who’s contributed, whether it’s been a PR, bug report, or presenting an idea for a cool feature. This project wouldn’t have gotten to this stage without it! 😉 ❤ 👏 🎉

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [October 10, 2020, 4:26am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/2 "2020-10-10T04:26:46Z")

</div>

I think that automatic formatting is a good fit for Julia.

The packages are a huge code base, and the user survey shows that a lot of us contribute bits of code here and there. We all have to stay across a dozen ways of doing the small stuff in the dozen packages we read or contribute to, so there is a lot of effort to save by standardizing it. Many of us are absent minded scientists, with a low tolerance for dotting Is and crossing Ts. It would be nice if you couldn’t tell that by looking at the code.

Is there a way to configure git so that any code I commit gets formatted the way the package maintainer wants?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 10, 2020, 4:38am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/3 "2020-10-10T04:38:17Z")

</div>

> [@thisrod](#):
>
> gets formatted the way the package maintainer wants

probably can be done via bot / github action

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [October 10, 2020, 4:40am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/4 "2020-10-10T04:40:54Z")

</div>

> [@jling](#):
>
> probably can be done via bot / github action

True, but it would be nice if I could check the auto-formatted code before it was published.

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [October 10, 2020, 4:46am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/5 "2020-10-10T04:46:22Z")

</div>

Here’s an example

this workflow [https://github.com/JuliaReinforcementLearning/ReinforcementLearningZoo.jl/blob/master/.github/workflows/format\_pr.yml](https://github.com/JuliaReinforcementLearning/ReinforcementLearningZoo.jl/blob/master/.github/workflows/format_pr.yml)

creates this PR

[https://github.com/JuliaReinforcementLearning/ReinforcementLearningZoo.jl/pull/98](https://github.com/JuliaReinforcementLearning/ReinforcementLearningZoo.jl/pull/98)

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [October 10, 2020, 7:26am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/6 "2020-10-10T07:26:05Z")

</div>

We for example run formatter on PRs to remind ourselves to run it before merging, that is not yet automatic in fixingg format – but then you locally run formatter and do a final commit at the ent of the PR (or learn to follow the rules you impose).

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [October 10, 2020, 7:27am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/7 "2020-10-10T07:27:29Z")

</div>

Thanks for this nice package, we already use it for quite a while to impose a unified code style to our packages in JuliaManifolds.

Thanks for also writing this announcement, because I learned about the config file, which is really handy and will shorten some of my commands to run formatter regularly.

---

<div class="post-metadata">

**Author:** ![d1cker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/d1cker/32/9816_2.png) [@d1cker](https://discourse.julialang.org/u/d1cker)\
**Post date:** [October 10, 2020, 8:19am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/8 "2020-10-10T08:19:22Z")

</div>

Could this package integrate in LanguageServer.jl?

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [October 10, 2020, 6:04pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/9 "2020-10-10T18:04:48Z")

</div>

[https://github.com/julia-vscode/LanguageServer.jl/pull/759](https://github.com/julia-vscode/LanguageServer.jl/pull/759)

---

<div class="post-metadata">

**Author:** ![niczky12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niczky12/32/16085_2.png) [@niczky12](https://discourse.julialang.org/u/niczky12)\
**Post date:** [October 17, 2020, 6:23am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/10 "2020-10-17T06:23:45Z")

</div>

If this had a pre-commit hook, I’d be sold. Julia really needs something like that.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [October 17, 2020, 1:00pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/11 "2020-10-17T13:00:55Z")

</div>

Are there any other packages like JuliaFormatter?

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [October 17, 2020, 4:51pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/12 "2020-10-17T16:51:57Z")

</div>

Would that be where it would format only code that was changed?

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [October 17, 2020, 4:53pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/13 "2020-10-17T16:53:16Z")

</div>

[https://github.com/julia-vscode/DocumentFormat.jl](https://github.com/julia-vscode/DocumentFormat.jl) is the closest thing

Is there something you would want available that’s not currently part of JuliaFormatter.jl ?

---

<div class="post-metadata">

**Author:** ![niczky12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niczky12/32/16085_2.png) [@niczky12](https://discourse.julialang.org/u/niczky12)\
**Post date:** [October 17, 2020, 4:59pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/14 "2020-10-17T16:59:36Z")

</div>

A pre-commit hookis something that runs every time before a commit is made to ensure code quality. So in this case, before a commit is run, juliaformatter would run, clean up the code and would ask you to commit the new changes. I found this workflow for python to be super valuable to maintain standards across the team.

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [October 17, 2020, 6:38pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/15 "2020-10-17T18:38:13Z")

</div>

just want to try all these similar packages and settle on one

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [October 24, 2020, 4:12am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/16 "2020-10-24T04:12:19Z")

</div>

How would you activate such a pre-commit hook?  
I would like to have this in VS Code for example on save. I just have not seen a pre-commit hook, how would it work?

The reason I am asking is, we are currently using a GitHub Action  
( [https://github.com/JuliaManifolds/Manifolds.jl/blob/master/.github/workflows/format.yml](https://github.com/JuliaManifolds/Manifolds.jl/blob/master/.github/workflows/format.yml) ) and a `.JuliaFormatter.toml` to check format on new PRs (and master commits for sure), but it happens regularly that I forget to format locally before I push. In the end this enforces a nice standard, but one sometimes pushes – ah format is missing - and pushes again.

---

<div class="post-metadata">

**Author:** ![niczky12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niczky12/32/16085_2.png) [@niczky12](https://discourse.julialang.org/u/niczky12)\
**Post date:** [October 24, 2020, 6:13am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/17 "2020-10-24T06:13:36Z")

</div>

That is exactly what pre-commit solves. Check this article out for an explanation:

> **[Automate Python workflow using pre-commits: black and flake8](https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)**
>
> Before I commit my staged Python files, black formats my code and flake8 checks my compliance to PEP8. If everything passes, the commit is made. If not, then...

Tbh I started using this about 6 months ago so I’m far from an expert.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [October 24, 2020, 6:46am UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/18 "2020-10-24T06:46:51Z")

</div>

Cool. Something like that with Julia would be great (but surely is off topic for this thread) – i.e. replace black with JuliaFormatter (and code blue for me) and a hook like flake8 in Julia.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [August 27, 2021, 12:21pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/19 "2021-08-27T12:21:27Z")

</div>

I like your package.  
Can you tell me how I can avoid that your package inserts semicolons in my function calls?  
I am not 100% sure, but I think this breaks something for one of my functions…

```julia
#don't want:
DataFrames.DataFrame(; a=rand(3))

#want:
DataFrames.DataFrame(a=rand(3))

```

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [August 27, 2021, 1:21pm UTC](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084/20 "2021-08-27T13:21:47Z")

</div>

one way is to remove the semicolon if there’s nothing before it. We don’t remove it in this case because the semantics would change. DataFrame(a=rand(3)) is slightly different from DataFrame(;a=rand(3)) afaik

[Next page](https://discourse.julialang.org/t/ann-juliaformatter-jl/48084.md?page=2)
