# (Auto)-formatting code?

**URL:** <https://discourse.julialang.org/t/auto-formatting-code/1611>\
**Category:** General Usage\
**Created:** [January 20, 2017, 12:48pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611 "2017-01-20T12:48:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 20, 2017, 12:48pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/1 "2017-01-20T12:48:49Z")

</div>

I’ve lost track of this. Is there a known configuration of an available editor or a tool, that just corrects formatting of julia code? (like gofmt or crtl-A, crtl-I in medit).

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 20, 2017, 3:18pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/2 "2017-01-20T15:18:55Z")

</div>

I don’t think so. See [this -users thread](https://groups.google.com/d/msg/julia-users/BjhSAAFiseU/EulhJQqSmvgJ) and [https://github.com/JuliaLang/JuliaParser.jl/issues/22](https://github.com/JuliaLang/JuliaParser.jl/issues/22) for related discussion.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 20, 2017, 3:41pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/3 "2017-01-20T15:41:26Z")

</div>

For what is worth, my tokenization package [https://github.com/KristofferC/Tokenize.jl](https://github.com/KristofferC/Tokenize.jl) (that is used for the syntax highlighting in [https://github.com/KristofferC/OhMyREPL.jl](https://github.com/KristofferC/OhMyREPL.jl)) is completely round trippable.

However, I am not sure it is sophisticated enough to write a full fledged auto formatter on top of it.

---

<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 20, 2017, 6:49pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/4 "2017-01-20T18:49:29Z")

</div>

ZacLN is doing a lot of work on JuliaParser that might eventually allow us to do code formatting.

In terms of UI on the VS Code side, all we would need is to extend our language server implementation (written all in julia) to also handle code formatting requests from VS Code, the complete UI is already provided by VS Code. Right now I see two paths towards that: 1) we wait until ZacLN’s JuliaParser rewrite enables code formatting or 2) someone tries to use [https://github.com/KristofferC/Tokenize.jl](https://github.com/KristofferC/Tokenize.jl) to add something in the meantime. I’ve opened a new issue for tracking this on the LanguageServer.jl side [https://github.com/JuliaEditorSupport/LanguageServer.jl/issues/34](https://github.com/JuliaEditorSupport/LanguageServer.jl/issues/34). Would be great if someone wants to volunteer and help out with this!

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 21, 2017, 10:10am UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/5 "2017-01-21T10:10:58Z")

</div>

I would also look forward to an auto-formatting tool. Of course, it’s not too hard to write something that handles the more basic structures of Julia programs—just keep track of indentation levels and a few special cases, and use it only when there’s no documentation about:

 ![](https://global.discourse-cdn.com/julialang/original/3X/8/2/822bfe725eec8836e863f99b73891e72e1af2104.gif)

(But I wouldn’t share this; I’m happy to ruin my code but would hate to ruin others’… 🙂

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 21, 2017, 9:23pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/6 "2017-01-21T21:23:00Z")

</div>

Vim with `julia-vim` plugin has auto-formatting ability, based on `=`, for example `ggVG=` format all code in buffer.

---

<div class="post-metadata">

**Author:** ![HarrisonGrodin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harrisongrodin/32/5214_2.png) [@HarrisonGrodin](https://discourse.julialang.org/u/HarrisonGrodin)\
**Post date:** [February 15, 2017, 5:31am UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/7 "2017-02-15T05:31:59Z")

</div>

Could we use Julia’s built-in displaying for this, at least partially? For example, `quote` blocks “fix” formatting when they display their code.

```julia
julia> quote
           for i in 1:10 println(i) end
           
           x = 3
           if x < 10 println("x is small.") else
               println("x is large.") end

           type Point
               x::Int
               y::Int
           end

           points = [Point(x,y) for x=1:3, y in 1:3]    
       end
quote # REPL[1], line 2:
    for i = 1:10 # REPL[1], line 2:
        println(i)
    end # REPL[1], line 4:
    x = 3 # REPL[1], line 5:
    if x < 10 # REPL[1], line 5:
        println("x is small.")
    else # REPL[1], line 6:
        println("x is large.")
    end # REPL[1], line 8:
    type Point # REPL[1], line 9:
        x::Int # REPL[1], line 10:
        y::Int
    end # REPL[1], line 13:
    points = [Point(x,y) for x = 1:3, y = 1:3]
end

```

Would it be possible to somehow implement this in such a way that spacing is kept, line number comments are not generated, and syntactic equivalents are not substituted (`in` vs. `=`, etc.)?

Using this general style for autoformatting would ensure that formatting always follows current Julian standards. (For example, the spacing after commas in type parameters was recently updated.)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [February 15, 2017, 10:54am UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/8 "2017-02-15T10:54:13Z")

</div>

I wouldn’t like a formatting tool that replaced my preferred `in`s with `=`s though…

---

<div class="post-metadata">

**Author:** ![HarrisonGrodin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harrisongrodin/32/5214_2.png) [@HarrisonGrodin](https://discourse.julialang.org/u/HarrisonGrodin)\
**Post date:** [February 15, 2017, 12:30pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/9 "2017-02-15T12:30:47Z")

</div>

Neither would I; that’s one thing that would have to be addressed, alongside newline spacing, line comments, and such. 🙂

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 15, 2017, 4:12pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/10 "2017-02-15T16:12:03Z")

</div>

If anyone’s interested in picking up a project on this, I would encourage trying to build something as powerful as Swift’s new tooling: [[swift-dev] Swift Syntax Structured Editing Library](https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20170206/004066.html)

---

<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:** [February 15, 2017, 6:10pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/11 "2017-02-15T18:10:32Z")

</div>

> If anyone’s interested in picking up a project on this, I would encourage trying to build something as powerful as Swift’s new tooling: [[swift-dev] Swift Syntax Structured Editing Library](https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20170206/004066.html)

[Roslyn](https://github.com/dotnet/roslyn) is another good thing to look at. It powers the full C# compiler and all IDE editing features in VS.

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [May 4, 2017, 8:03pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/12 "2017-05-04T20:03:30Z")

</div>

= in vim changes the indentation, but not to the correct way.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 5, 2017, 2:44am UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/13 "2017-11-05T02:44:39Z")

</div>

I use ctrl-a ctrl-shift-i in VS code to re-indent the entire file. It does not handle correctly continuation lines, but otherwise it is usable.

PS: You will need this:

```julia
{ "key": "ctrl+shift+i", "command": "editor.action.reindentlines",
        "when": "editorTextFocus" },

```

---

<div class="post-metadata">

**Author:** ![Tony\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@Tony\_Gonzalez](https://discourse.julialang.org/u/Tony_Gonzalez)\
**Post date:** [July 3, 2018, 2:26pm UTC](https://discourse.julialang.org/t/auto-formatting-code/1611/14 "2018-07-03T14:26:51Z")

</div>

You could also try this [one](https://www.devart.com/dbforge/sql/sqlcomplete/sql-code-formatter.html)  
SQL Complete is a pretty good solution for advanced code formatting
