# Julia code formatter / code beautifier

**URL:** <https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485>\
**Category:** General Usage\
**Created:** [June 6, 2018, 7:48pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485 "2018-06-06T19:48:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [June 6, 2018, 7:48pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/1 "2018-06-06T19:48:21Z")

</div>

Hello,

Code formatting is an important task to keep code more easily understandable.

[Go](https://golang.org/) have [gofmt](https://golang.org/cmd/gofmt/)  
[Rust](https://www.rust-lang.org/) have [rustfmt](https://github.com/rust-lang-nursery/rustfmt)  
[Python](https://www.python.org/) have [black](https://github.com/ambv/black)

I wonder if there’s an active project for such task in Julia.

I’ve found [https://github.com/invenia/JuliaFormat.jl](https://github.com/invenia/JuliaFormat.jl) from @invenia but it doesn’t seem to be maintained any more.

I wonder what are opinions of core (Base) Julia maintainers about such tools?

Is there a chance to have such tools be very well maintained and be used in Base code?

I also wonder why linter such as [https://github.com/tonyhffong/Lint.jl](https://github.com/tonyhffong/Lint.jl) is not currently used in Base (according [https://github.com/JuliaLang/julia/blob/master/.travis.yml](https://github.com/JuliaLang/julia/blob/master/.travis.yml) )

Kind regards

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [June 6, 2018, 9:06pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/2 "2018-06-06T21:06:51Z")

</div>

We have a code formatter in the julia [VS Code extension](https://github.com/JuliaEditorSupport/julia-vscode) that works.

That code is currently being factored out into [https://github.com/ZacLN/DocumentFormat.jl](https://github.com/ZacLN/DocumentFormat.jl). The linting stuff in the VS Code extension is moving into [https://github.com/ZacLN/StaticLint.jl](https://github.com/ZacLN/StaticLint.jl).

All of this work is based on [CSTParser.jl](https://github.com/ZacLN/CSTParser.jl), which is actively maintained and also used by stuff that Julia Computing is putting out (e.g. femtocleaner).

---

<div class="post-metadata">

**Author:** ![js135005](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/js135005/32/8219_2.png) [@js135005](https://discourse.julialang.org/u/js135005)\
**Post date:** [June 6, 2018, 9:19pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/3 "2018-06-06T21:19:08Z")

</div>

Is there a VS Code prerelease that works on Julia 0.7 without the highly annoying deprecation messages?

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [June 6, 2018, 10:34pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/4 "2018-06-06T22:34:10Z")

</div>

No, we don’t have a 0.7 compatible version at this point.

---

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [July 27, 2018, 8:14am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/5 "2018-07-27T08:14:02Z")

</div>

It will be great to have an online Julia code formatter (an Heroku app or [any web apps framework compatible with Julia](https://discourse.julialang.org/t/hosting-web-apps-in-julia/1063).

So code samples could be better formatted using such a service.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [July 27, 2018, 9:45am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/6 "2018-07-27T09:45:27Z")

</div>

VSCode is interesting. I am a big fan of Atom, and there is a really nice style checker for Python. Little warning trianges appear on the lines. This uses ‘pycodestyle’

Somethign similar for Julia woudl be most excellent. Happy to offer to test!

---

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [August 4, 2018, 9:29pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/7 "2018-08-04T21:29:43Z")

</div>

So what should I do in VS code to

- automatically indent my code (if, for and while loops…)
- automatically add space to expressions such as `a=b+2` to `a = b + 2`

?

I haven’t found a clear explanation, even in this thread [(Auto)-formatting code? - #14 by Tony\_Gonzalez](https://discourse.julialang.org/t/auto-formatting-code/1611/14)

A Github bot which could create PR to reformat code could be a very nice feature to have.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 5, 2018, 3:39am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/8 "2018-08-05T03:39:02Z")

</div>

Select the text you want to format, then execute the command “format selection” (or the precise name).

---

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [August 5, 2018, 6:13am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/9 "2018-08-05T06:13:36Z")

</div>

Thanks @davidanthoff

---

<div class="post-metadata">

**Author:** ![SZJX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/szjx/32/4036_2.png) [@SZJX](https://discourse.julialang.org/u/SZJX)\
**Post date:** [September 21, 2018, 1:45pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/10 "2018-09-21T13:45:49Z")

</div>

VSCode is mostly similar to Atom anyways, but with better performance and more active development etc. (also now that MS has bought Github). The VSCode extension is great and you can try it out to see if you can switch from Atom to VSCode.

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [January 2, 2019, 8:50pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/11 "2019-01-02T20:50:30Z")

</div>

@davidanthoff (and @ZacLN) thanks for those pointers! 🙂 Those are great packages! I love it!

* * *

Has anyone put together integration with `git`, so that we can run `format` on just the lines that have changed between some commits? Basically the equivalent of how this is set up:

> **[GitHub - audiohacked/git-clang-format: Clang-format configuration, script and...](https://github.com/audiohacked/git-clang-format)**
>
> Clang-format configuration, script and git hook used for our C++ code - GitHub - audiohacked/git-clang-format: Clang-format configuration, script and git hook used for our C++ code

I think I can put together the equivalent of the git integration file (based on this one: [https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format](https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format)), but i was just want to see if anyone’s already done it first! 🙂 Anyone know?

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [January 2, 2019, 11:49pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/12 "2019-01-02T23:49:41Z")

</div>

Haha okay, so… Ever impatient, i’ve gone ahead and hacked at it myself! \>.\< Oops!

> **[GitHub - NHDaly/GitJuliaFormat.jl](https://github.com/NHDaly/GitJuliaFormat.jl)**
>
> Contribute to NHDaly/GitJuliaFormat.jl development by creating an account on GitHub.

It’s definitely work-in-progress, but this was enough to get the `git`-side of the glue hooked up! 🙂 It’s currently just diffing `HEAD` against the previous commit [here](https://github.com/NHDaly/GitJuliaFormat.jl/blob/master/src/GitJuliaFormat.jl#L106), but it won’t be hard to change that to read the commit range from the command line arguments.

The next problem is that (as far as I can tell) `DocumentFormat.format()` expects valid expressions in the input string, and not the _partial-expressions_ that result from just reading whatever lines i happen to have modified.

So _either_ that git configuration repository needs to be able to intelligently read above and below the modified section to find the start and end of a valid Expression chunk, or perhaps we could consider adding some awareness to `DocumentFormat` of context for formatting chunks of files?

* * *

------ EDIT: -------

> [@NHDaly](#):
>
> `DocumentFormat.format()` expects valid expressions in the input string, and not the _partial-expressions_ that result from just reading whatever lines i happen to have modified.

Oh wait, that can’t be right! @davidanthoff was just saying above that you can format a “selection” in VSCode:

> [@davidanthoff](#):
>
> Select the text you want to format, then execute the command “format selection” (or the precise name).

I’ll have to look at how you do that, and then Maybe this little git-configuration project will be just about finished!! 😃 Can you send me a pointer? I can’t seem to find where that’s implemented. 😄 thanks!

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [January 3, 2019, 12:48am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/13 "2019-01-03T00:48:23Z")

</div>

@ZacLN would know best 🙂

---

<div class="post-metadata">

**Author:** ![ZacLN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacln/32/2748_2.png) [@ZacLN](https://discourse.julialang.org/u/ZacLN)\
**Post date:** [January 3, 2019, 8:57am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/14 "2019-01-03T08:57:55Z")

</div>

Hi, you’re correct we currently only format entire files, not snippets, as we need the full context to work out the correct indentation level.

We could apply a subset of the formatting edits produced by the current code only to the line marked as dirty by git (i.e. filter the edits by location) but this may put the indentation of said line out of whack with the rest of the file if it’s judged to be incorrectly indented by the formatter.

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [January 3, 2019, 9:28pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/15 "2019-01-03T21:28:59Z")

</div>

Thanks, yeah, that makes sense. I’m not even really sure if the git integration is an important feature or not.

I’m just wondering, though, how does the VSCode extension support formatting a selection then like @davidanthoff mentioned above? Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [January 3, 2019, 9:46pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/16 "2019-01-03T21:46:32Z")

</div>

I might well have been wrong, I have a long track record of a) not knowing about features that exist in the VS Code extension and b) think we have other features that don’t exist 🙂

---

<div class="post-metadata">

**Author:** ![PGS62](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgs62/32/207030_2.png) [@PGS62](https://discourse.julialang.org/u/PGS62)\
**Post date:** [January 28, 2019, 4:26pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/17 "2019-01-28T16:26:43Z")

</div>

I have Julia 1.1.0 and the Julia extension for VS code 0.11.4 (released 24 Jan 2019), on Windows 10. I’m pretty sure that code formatting a document (F1 \> Format Document) used to work (with an earlier version of the Julia extension that was compatible with Julia 0.6) but now I get a pop-up message “There is no document formatter for ‘julia’-files installed.” I tried the obvious - reinstalling the extension, rebooting the PC etc. but no dice.

It should work should it not? Can anyone suggest a solution? Thanks!

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [January 28, 2019, 6:49pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/18 "2019-01-28T18:49:19Z")

</div>

The extension right now has some issues on julia 1.1, we’re trying to fix those…

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 5, 2019, 7:20pm UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/19 "2019-02-05T19:20:22Z")

</div>

not exactly. I want a connected curve with parts which are dashed and other parts solid. Maybe an elaboration of your suggestion can provide this.

---

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [June 25, 2019, 10:58am UTC](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485/20 "2019-06-25T10:58:14Z")

</div>

I noticed in [JuliaCon 2019 :: pretalx](https://pretalx.com/juliacon2019/sneak/)

### 

> “Formatting Julia”
> 
> Dominique Luna; _Lightning Talk_
> 
> Ever wish your code automatically beautiful? Tired of spacing out commas, wrangling parenthesis and indenting? Julia’s formatter can do all this and more! Come find out how to use it your everyday workflow.

Maybe [GitHub - domluna/JuliaFormatter.jl: An opinionated code formatter for Julia. Plot twist - the opinion is your own.](https://github.com/domluna/JLFmt.jl) from @domluna could be worth trying.

A CLI tool (directly available from PATH) could be a great feature to have.

PS : see [https://youtu.be/12z5MzoFQOM](https://youtu.be/12z5MzoFQOM)

[Next page](https://discourse.julialang.org/t/julia-code-formatter-code-beautifier/11485.md?page=2)
