Julia code formatter / code beautifier

Hello,

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

Go have gofmt
Rust have rustfmt
Python have black

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

I’ve found 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 is not currently used in Base (according https://github.com/JuliaLang/julia/blob/master/.travis.yml )

Kind regards

7 Likes

We have a code formatter in the julia VS Code extension that works.

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

All of this work is based on CSTParser.jl, which is actively maintained and also used by stuff that Julia Computing is putting out (e.g. femtocleaner).

17 Likes

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

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

It will be great to have an online Julia code formatter (an Heroku app or any web apps framework compatible with Julia.

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

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!

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

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

1 Like

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

1 Like

Thanks @davidanthoff

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.

1 Like

@davidanthoff (and @ZacLN) thanks for those pointers! :slight_smile: 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:

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), but i was just want to see if anyone’s already done it first! :slight_smile: Anyone know?

2 Likes

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

It’s definitely work-in-progress, but this was enough to get the git-side of the glue hooked up! :slight_smile: It’s currently just diffing HEAD against the previous commit here, 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: -------

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

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

2 Likes

@ZacLN would know best :slight_smile:

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.

1 Like

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! :slight_smile:

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 :slight_smile:

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!

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

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.

I noticed in 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. 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

8 Likes