Skip to content

domluna/JuliaFormatter.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JLFmt

Build Status

WARNING. This an alpha release meant to find bugs. Overwrite files at your peril!

Width-sensitive formatter for Julia code. Inspired by gofmt and refmt.

]add https://github.com/domluna/JLFmt.jl

JLFmt exports a singular function:

format(text::String; indent_size=4, print_width=80)
  • indent_size - the number of spaces used for an indentation.
  • print_width - the maximum number of characters of code on a single line. Lines over the limit will be nested if possible.

How It Works

JLFmt parses the .jl source file into a Concrete Syntax Tree (CST) using CSTParser.

Pass 1: Prettify

The CST is prettified using pretty, creating a PTree. The printing output of a PTree is a canonical representation of the code removing unnecessary whitespace and joining or separating lines of code. The pretty testset displays these transformations.

Example 1:

function  foo
end

becomes

function foo end

Example 2:

for cond 1; 2; 3 end

becomes

for cond
    1
    2
    3
end

Pass 2: Nesting

PTree is nested using nest! to disjoin lines going over the print width. The PTree is modified in-place. All expressions are nested front to back with the exception of binary operations and conditionals.

Binary Ops

arg1 && arg2

->

arg1 && 
arg2
foo() = body

->

# The indentation of the body is based on `indent_size`
foo() =
    body

Conditionals

cond ? e1 : e2

->

cond ? e1 :
e2

->

cond ? 
e1 :
e2

Calls (also applies to {}, (), [], etc)

f(arg1, arg2, arg3)

->

f(
  arg1,
  arg2,
  arg3
)
function longfunctionname_that_is_long(lots, of, args, even, more, args)
    ..code..
end

->

# The arg is placed `indent_size` spaces after the start of the
# function name or one space after the opening parenthesis.
function longfunctionname_that_is_long(
             lots, 
             of, 
             args,
             even, 
             more, 
             args
         )
    ..code..
end
S <: Union{Type1,Type2,Type3}

->

S <: Union{
         Type1,
         Type2,
         Type3
     }

Part 3: Printing

Finally, the PTree to an IOBuffer with print_tree which is then consumed as a String.

Known Limitation(s)

If a comment is at the end of a line of code it will be removed.

var x = 10 # comment about x

Formatting will produce:

var x = 10

To get around with this write comments on separate lines.