[ANN] JuliaFormatter.jl

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

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 but there’s also YASStyle and BlueStyle which conform to the YAS and Blue style guides.

Besides styles there also several options 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, JuliaFormatter will automatically detect this file and override the default options.

Recently custom alignment detection 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 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! :wink: :heart: :clap: :tada:

47 Likes

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?

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.

Here’s an example

this workflow https://github.com/JuliaReinforcementLearning/ReinforcementLearningZoo.jl/blob/master/.github/workflows/format_pr.yml

creates this PR

2 Likes

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).

2 Likes

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.

Could this package integrate in LanguageServer.jl?

3 Likes

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

2 Likes

Are there any other packages like JuliaFormatter?

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

1 Like

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 ?

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.

2 Likes

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

1 Like

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 ) 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.

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

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

2 Likes

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.