(Auto)-formatting code?

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

7 Likes

I don’t think so. See this -users thread and https://github.com/JuliaLang/JuliaParser.jl/issues/22 for related discussion.

1 Like

For what is worth, my tokenization package https://github.com/KristofferC/Tokenize.jl (that is used for the syntax highlighting in 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.

1 Like

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 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. Would be great if someone wants to volunteer and help out with this!

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:

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

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

2 Likes

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

2 Likes

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

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

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

1 Like

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

Roslyn is another good thing to look at. It powers the full C# compiler and all IDE editing features in VS.

1 Like

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

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:

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

You could also try this one
SQL Complete is a pretty good solution for advanced code formatting