Formatting Options
JuliaFormatter is intended to be configurable and comes with a number of options to control the output of the formatter. Suggestions for more options are welcome; please open an issue!
All of these formatting options can be specified as:
- keyword arguments to
JuliaFormatter.format,JuliaFormatter.format_textandJuliaFormatter.format_file - entries in a
.JuliaFormatter.tomlfile, or - command-line arguments to the
jlfmtcommand.
.JuliaFormatter.toml and jlfmt further accept additional options that control which files are to be formatted. These options are not passed to format and friends, and are thus categorised as file options instead: please see the File Options page for more deatils.
Below is a table of all the currently available formatting options, along with what each style sets each option to. Values that differ from DefaultStyle are shown in bold.
Note that, although styles each define a different set of options, they are not just collections of options; they also have unique formatting rules that are not captured by the options.
| Option | Default | YAS | Blue | SciML | Minimal |
|---|---|---|---|---|---|
align_assignment | false | false | false | false | false |
align_conditional | false | false | false | false | false |
align_matrix | false | false | false | false | false |
align_pair_arrow | false | false | false | false | false |
align_struct_field | false | false | false | false | false |
always_for_in | false | true | true | true | nothing |
always_use_return | false | true | false | false | false |
annotate_untyped_fields_with_any | true | true | false | true | false |
conditional_to_if | false | false | true | false | false |
disallow_single_arg_nesting | false | false | false | true | false |
for_in_replacement | "in" | "in" | "in" | "in" | "in" |
force_long_function_def | false | false | false | false | false |
format_docstrings | false | false | false | false | false |
import_to_using | false | true | true | false | false |
indent | 4 | 4 | 4 | 4 | 4 |
indent_submodule | false | false | true | false | false |
join_lines_based_on_source | false | true | false | true | true |
long_to_short_function_def | false | false | false | false | false |
margin | 92 | 92 | 92 | 92 | 10_000 |
normalize_line_endings | "auto" | "auto" | "auto" | "unix" | "auto" |
pipe_to_function_call | false | true | true | false | false |
remove_extra_newlines | false | true | true | true | false |
separate_kwargs_with_semicolon | false | true | true | false | false |
short_circuit_to_if | false | false | false | false | false |
short_to_long_function_def | false | true | true | true | false |
surround_whereop_typeparameters | true | true | true | true | false |
trailing_comma | true | true | true | false | nothing |
trailing_zero | true | true | true | true | false |
variable_call_indent | [] | [] | [] | [] | [] |
whitespace_in_kwargs | true | false | false | true | false |
whitespace_ops_in_indices | false | true | true | true | false |
whitespace_typedefs | false | false | false | true | false |
yas_style_nesting | false | false | false | false | false |
align_...
Default: false
The align_assignment, align_conditional, align_pair_arrow, and align_struct_field options allow you to vertically align operators on consecutive lines. See the Custom Alignment page for more details and examples.
align_matrix
Default: false
If enabled, this option preserves pre-existing whitespace surrounding matrix elements in the original source file. If you want to align matrix elements yourself, you should set this to true.
using JuliaFormatter: format_text
# Elements left-aligned in original source
s = """
a = [
100 300 400
1 eee 40000
2 α b
]"""
format_text(s, align_matrix=true) |> printa = [
100 300 400
1 eee 40000
2 α b
]# Elements right-aligned in original source
s = """
a = [
100 300 400
1 ee 40000
2 a b
]"""
format_text(s, align_matrix=true) |> printa = [
100 300 400
1 ee 40000
2 a b
]always_for_in
Default: false
If true, = is always replaced with in if part of a for loop condition. For example, for i = 1:10 will be transformed to for i in 1:10. Set this to nothing to leave the choice to the user.
always_use_return
Default: false
If true, return will be prepended to the last expression where applicable in function definitions, macro definitions, and do blocks.
Example:
function foo()
expr1
expr2
endto
function foo()
expr1
return expr2
endannotate_untyped_fields_with_any
Default: true
Annotates fields in a type definitions with ::Any if no type annotation is provided:
struct A
arg1
endto
struct A
arg1::Any
endconditional_to_if
Default: false
If the conditional E ? A : B exceeds the maximum margin converts it into the equivalent if block:
if E
A
else
B
enddisallow_single_arg_nesting
Prevents the nesting of a single argument arg in parenthesis, brackets, and curly braces.
# Without `disallow_single_arg_nesting`:
function_call(
"String argument"
)
[array_item(
10
)]
{key => value(
"String value"
)}
# With `disallow_single_arg_nesting` enabled:
function_call("String argument")
[array_item(10)]
{key => value("String value")}for_in_replacement
Can be used when always_for_in is true to replace the default in with ∈ (\\in), or = instead. The replacement options are ("in", "=", "∈").
for a = 1:10
end
# formatted with always_for_in = true, for_in_replacement = "∈"
for a ∈ 1:10
endforce_long_function_def
Default: false
If true tweaks the behavior of short_to_long_function_def to force the transformation no matter how short the function definition is.
format_docstrings
Default: false
Format code docstrings with the same options used for the code source.
Markdown is formatted with CommonMark alongside Julia code.
import_to_using
Default: false
If true, import expressions are rewritten to using expressions in the following cases:
import A
import A, B, Cis rewritten to:
using A: A
using A: A
using B: B
using C: CThere are some exceptions to this:
If
asis found in the import expression,usingcannot be used in this context. The following example will not be rewritten:import Base.Threads as thIf
importis used in the following context it is not rewritten. This may change in a future patch.@everywhere import A, B
indent
Default: 4
The number of spaces used for one level of indentation.
There is at present no option to use tabs for indentation: please open an issue if you want this feature.
indent_submodule
Default: false
When set to true, submodule(s) appearing in the same file will be indented.
module A
a = 1
module B
b = 2
module C
c = 3
end
end
d = 4
endwill be formatted to:
module A
a = 1
module B
b = 2
module C
c = 3
end
end
d = 4
endjoin_lines_based_on_source
Default: false
When true lines are joined as they appear in the original source file.
function foo(arg1,
arg2, arg3
)
body
endWhen false and the maximum margin is > than the length of "function foo(arg1, arg2, arg3)" this is formatted to
function foo(arg1, arg2, arg3)
body
endWhen true, arg1 and arg2, arg3 will remain on separate lines even if they can fit on the same line since it's within maximum margin. The indentation is dependent on the style.
function foo(arg1,
arg2, arg3,
)
endThere are exceptions to this:
if a body1 elseif b body2 else body3 endwill be formatted to the following, even if this option is set to true:
if a
body1
elseif b
body2
else
body3
endlong_to_short_function_def
Default: false
Transforms a long function definition
function f(arg2, arg2)
body
endto a short function definition if the short function definition does not exceed the maximum margin.
f(arg1, arg2) = bodySee also: short_to_long_function_def.
margin
Default: 92
The maximum length of a line. Code exceeding this margin will, in general, be formatted across multiple lines.
normalize_line_endings
Default: "auto"
One of "unix" (normalize all \r\n to \n), "windows" (normalize all \n to \r\n), "auto" (automatically choose based on which line ending is more common in the file).
pipe_to_function_call
Default: false
If true, x |> f is rewritten to f(x), and x .|> f to f.(x).
Note that this transformation may change the semantics of the code in some cases:
If
Base.:(|>)is overloaded to have a different meaning for a givenfandx.If the call to
x |> fis intercepted by a macro that transforms it to something other thanf(x). For example, Pipe.jl.
To avoid (2), JuliaFormatter refuses to apply this transformation within the body of a macro. However, there is no way to detect (1). As such, JuliaFormatter will emit a warning if this transformation is applied during formatting, to alert the user to the potential of unwanted changes.
It is recommended to set this option to true only if you are confident that there are no such cases. Note: pipe_to_function_call is set to true by default for Blue and YAS styles, so in such cases you have to opt out manually!
remove_extra_newlines
Default: false
If true, superfluous newlines will be removed. For example:
module M
a = 1
function foo()
return nothing
end
b = 2
endis rewritten as
module M
a = 1
function foo()
return nothing
end
b = 2
endModules are the only type of code block where it is permissible to keep a single newline prior to the initial or after the final piece of code.
separate_kwargs_with_semicolon
Default: false
When set to true, keyword arguments in a function call will be separated with a semicolon.
f(a, b=1)
->
f(a; b=1)short_circuit_to_if
If truer, converts shortcircuiting expressions to the equivalent if-expression.
function foo(a, b)
a || return "bar"
"hello"
b && return "ooo"
endbecomes
function foo(a, b)
if !(a)
return "bar"
end
"hello"
if b
return "ooo"
else
false
end
endshort_to_long_function_def
Default: false
Transforms a short function definition
f(arg1, arg2) = bodyto a long function definition if the short function definition exceeds the maximum margin, or if force_long_function_def is set to true.
function f(arg2, arg2)
body
endSee also: long_to_short_function_def.
surround_whereop_typeparameters
Default: true
If true, surrounds type parameters with curly braces if the braces are not already present.
function func(...) where TPARAM
endbecomes
function func(...) where {TPARAM}
endtrailing_comma
Default: true
One of true, false, or nothing.
Trailing commas are added after the final argument when nesting occurs and the closing punctuation appears on the next line.
For example when the following is nested (assuming DefaultStyle):
funccall(arg1, arg2, arg3)it turns into:
funccall(
arg1,
arg2,
arg3, # trailing comma added after `arg3` (final argument) !!!
)- When set to
true, the trailing comma is always added during nesting. - When set to
false, the trailing comma is always removed during nesting. - When set to
nothing, the trailing comma appears as it does in the original source.
trailing_zero
Default: true
Add a trailing zero, if needed.
variable_call_indent
Default: []
The SciMLStyle supports the additional option variable_call_indent. It permits continuation lines in calls to not align with the opening parenthesis:
# Allowed with and without `Dict in variable_call_indent`
Dict{Int, Int}(1 => 2,
3 => 4)
# Allowed when `Dict in variable_call_indent`, but
# will be changed to the first example when `Dict ∉ variable_call_indent`.
Dict{Int, Int}(
1 => 2,
3 => 4)whitespace_in_kwargs
Default: true
If true, = in keyword arguments will be surrounded by whitespace.
f(; a=4)to
f(; a = 4)Note that if this option is false, the arguments on either side of the equals may sometimes be parenthesised to avoid parsing ambiguities. For example, f(s! = x) will be transformed to f((s!)=x), and f(t = >=(1)) will be transformed to f(t=(>=(1))).
whitespace_ops_in_indices
Default: false
If true, whitespace is added for binary operations in indices. Make this true if you prefer arr[a + b] to arr[a+b]. Additionally, if there's a colon : involved, parenthesis will be added to the LHS and RHS.
Example: arr[(i1 + i2):(i3 + i4)] instead of arr[i1+i2:i3+i4].
whitespace_typedefs
Default: false
If true, whitespace is added for type definitions. Make this true if you prefer Union{A <: B, C} to Union{A<:B,C}.
yas_style_nesting
Default: false
The option yas_style_nesting is set to false by default. Setting it to true makes the SciMLStyle use the YASStyle nesting rules. For other styles, this option has no effect.
# With `yas_style_nesting = false`
function my_large_function(argument1, argument2,
argument3, argument4,
argument5, x, y, z)
foo(x) + goo(y)
end
# With `yas_style_nesting = true`
function my_large_function(argument1, argument2,
argument3, argument4,
argument5, x, y, z)
foo(x) + goo(y)
end