Skip to content

Commit

Permalink
style update
Browse files Browse the repository at this point in the history
  • Loading branch information
domluna committed Sep 25, 2024
1 parent fbca9bd commit bdd712d
Show file tree
Hide file tree
Showing 20 changed files with 686 additions and 685 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
remove_extra_newlines = true
always_for_in = true
conditional_to_if = true
separate_kwargs_with_semicolon = true
6 changes: 3 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Documenter, JuliaFormatter

makedocs(
makedocs(;
sitename = "JuliaFormatter",
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
format = Documenter.HTML(; prettyurls = get(ENV, "CI", nothing) == "true"),
modules = [JuliaFormatter],
pages = [
"Introduction" => "index.md",
Expand All @@ -20,4 +20,4 @@ makedocs(
warnonly = true,
)

deploydocs(repo = "github.com/domluna/JuliaFormatter.jl.git", push_preview = true)
deploydocs(; repo = "github.com/domluna/JuliaFormatter.jl.git", push_preview = true)
2 changes: 1 addition & 1 deletion src/align.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ function align_conditional!(fst::FST)
# the placeholder would be i+1 if not for a possible inline comment
nind = findnext(n -> n.typ === PLACEHOLDER, nodes, ind + 1)::Int
if nodes[nind+1].startline != nodes[nind].startline
nodes[nind] = Newline(nest_behavior = AlwaysNest)
nodes[nind] = Newline(; nest_behavior = AlwaysNest)
end

if colon_len !== nothing
Expand Down
8 changes: 4 additions & 4 deletions src/fst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ function add_node!(
end

if nt !== PLACEHOLDER
add_node!(t, Newline(nest_behavior = AlwaysNest), s)
add_node!(t, Newline(; nest_behavior = AlwaysNest), s)
elseif hascomment(s.doc, current_line) && nt === PLACEHOLDER
# swap PLACEHOLDER (will be NEWLINE) with INLINECOMMENT node
idx = length(tnodes::Vector{FST})
Expand All @@ -1088,13 +1088,13 @@ function add_node!(

if nest
add_node!(t, Notcode(notcode_startline, notcode_endline), s)
add_node!(t, Newline(nest_behavior = AlwaysNest), s)
add_node!(t, Newline(; nest_behavior = AlwaysNest), s)
end
elseif !join_lines
if hascomment(s.doc, current_line) && current_line != n.startline
add_node!(t, InlineComment(current_line), s)
end
add_node!(t, Newline(nest_behavior = AlwaysNest), s)
add_node!(t, Newline(; nest_behavior = AlwaysNest), s)
elseif nt === PLACEHOLDER &&
current_line != n.startline &&
hascomment(s.doc, current_line)
Expand All @@ -1109,7 +1109,7 @@ function add_node!(
# rely on the whitespace tracked for the inline comment
tnodes[end] = Whitespace(0)
add_node!(t, InlineComment(current_line), s)
add_node!(t, Newline(nest_behavior = AlwaysNest), s)
add_node!(t, Newline(; nest_behavior = AlwaysNest), s)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/nest_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function nest_if_over_margin!(
if margin > s.opts.margin ||
(idx < length(fst.nodes::Vector) && is_comment(fst[idx+1])) ||
(idx > 1 && is_comment(fst[idx-1]))
fst[idx] = Newline(length = fst[idx].len)
fst[idx] = Newline(; length = fst[idx].len)
s.line_offset = fst.indent
return true
end
Expand Down Expand Up @@ -265,7 +265,7 @@ function find_optimal_nest_placeholders(
fst,
g,
start_line_offset,
max_margin,
max_margin;
last_group = i == length(placeholder_groups),
)
push!(optimal_placeholders, optinds...)
Expand Down
68 changes: 34 additions & 34 deletions src/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function flatten_binaryopcall(fst::FST; top = true)
end

if lhs_same_op
append!(nodes, flatten_binaryopcall(lhs, top = false))
append!(nodes, flatten_binaryopcall(lhs; top = false))
else
flatten_fst!(lhs)
push!(nodes, lhs)
Expand All @@ -31,7 +31,7 @@ function flatten_binaryopcall(fst::FST; top = true)
append!(nodes, fst[2:(idx-1)])

if rhs_same_op
append!(nodes, flatten_binaryopcall(rhs, top = false))
append!(nodes, flatten_binaryopcall(rhs; top = false))
else
flatten_fst!(rhs)
push!(nodes, rhs)
Expand Down Expand Up @@ -185,12 +185,12 @@ function import_to_usings(fst::FST, s::State)
add_node!(use, FST(KEYWORD, -1, sl, el, "using"), s)
add_node!(use, Whitespace(1), s)

add_node!(use, n, s, join_lines = true)
add_node!(use, n, s; join_lines = true)
colon = FST(OPERATOR, -1, sl, el, ":")
colon.metadata = Metadata(K"::", false)
add_node!(use, colon, s, join_lines = true)
add_node!(use, colon, s; join_lines = true)
add_node!(use, Whitespace(1), s)
add_node!(use, n[end], s, join_lines = true)
add_node!(use, n[end], s; join_lines = true)

push!(usings, use)
end
Expand All @@ -215,12 +215,12 @@ function annotate_typefields_with_any!(fst::FST, s::State)
line_offset = n.line_offset + length(n)
op = FST(OPERATOR, line_offset, n.startline, n.endline, "::")
op.metadata = Metadata(K"::", false)
add_node!(nn, op, s, join_lines = true)
add_node!(nn, op, s; join_lines = true)
line_offset += 2
add_node!(
nn,
FST(IDENTIFIER, line_offset, n.startline, n.endline, "Any"),
s,
s;
join_lines = true,
)
fst[i] = nn
Expand Down Expand Up @@ -286,13 +286,13 @@ function short_to_long_function_def!(
# func(a)
# OR
# func(a) where T
add_node!(funcdef, fst[1], s, join_lines = true)
add_node!(funcdef, fst[1], s; join_lines = true)

# body

# s.opts.always_use_return && prepend_return!(fst[end], s)
if fst[end].typ === Block
add_node!(funcdef, fst[end], s, max_padding = s.opts.indent)
add_node!(funcdef, fst[end], s; max_padding = s.opts.indent)
elseif fst[end].typ === Begin
# case where body is wrapped in a `begin` block
# which becomes superfluous when converted to a
Expand All @@ -308,7 +308,7 @@ function short_to_long_function_def!(
idx === nothing && return false
bnode = fst[end][idx]
add_indent!(bnode, s, -s.opts.indent)
add_node!(funcdef, bnode, s, max_padding = s.opts.indent)
add_node!(funcdef, bnode, s; max_padding = s.opts.indent)
else
# ```
# function
Expand All @@ -320,7 +320,7 @@ function short_to_long_function_def!(
# a `Block` node ensures the indent is correct.
bl = FST(Block, fst[end].indent)
add_node!(bl, fst[end], s)
add_node!(funcdef, bl, s, max_padding = s.opts.indent)
add_node!(funcdef, bl, s; max_padding = s.opts.indent)
end
add_indent!(funcdef[end], s, s.opts.indent)

Expand Down Expand Up @@ -461,17 +461,17 @@ function binaryop_to_whereop!(fst::FST, s::State)
if n.typ === Where
break
end
add_node!(binop, n, s, join_lines = true)
add_node!(binop, n, s; join_lines = true)
end
# # foo(a::A)::R gets the "R"
add_node!(binop, oldwhereop[1], s, join_lines = true)
add_node!(binop, oldwhereop[1], s; join_lines = true)

whereop = FST(Where, fst[1].indent)
add_node!(whereop, binop, s)

# "foo(a::A)::R where A"
for n in oldwhereop[2:end]
add_node!(whereop, n, s, join_lines = true)
add_node!(whereop, n, s; join_lines = true)
end

fst[1] = whereop
Expand Down Expand Up @@ -592,9 +592,9 @@ end
function conditional_to_if_block!(fst::FST, s::State, top::Bool)
t = FST(If, fst.indent)
kw = FST(KEYWORD, -1, fst.startline, fst.startline, top ? "if" : "elseif")
add_node!(t, kw, s, max_padding = 0)
add_node!(t, Whitespace(1), s, join_lines = true)
add_node!(t, fst[1], s, join_lines = true)
add_node!(t, kw, s; max_padding = 0)
add_node!(t, Whitespace(1), s; join_lines = true)
add_node!(t, fst[1], s; join_lines = true)

nodes = fst.nodes::Vector{FST}
idx1 = findfirst(n -> n.typ === OPERATOR && n.val == "?", nodes)::Int
Expand All @@ -610,7 +610,7 @@ function conditional_to_if_block!(fst::FST, s::State, top::Bool)
end
add_node!(block1, n, s)
end
add_node!(t, block1, s, max_padding = s.opts.indent)
add_node!(t, block1, s; max_padding = s.opts.indent)

block2 = FST(Block, fst.indent)
padding = 0
Expand All @@ -620,14 +620,14 @@ function conditional_to_if_block!(fst::FST, s::State, top::Bool)
block2.indent += s.opts.indent
padding = s.opts.indent
kw = FST(KEYWORD, 0, 0, -1, "else")
add_node!(t, kw, s, max_padding = 0)
add_node!(t, kw, s; max_padding = 0)
end
add_node!(block2, fst[end], s)
add_node!(t, block2, s, max_padding = 0)
add_node!(t, block2, s; max_padding = 0)

if top
kw = FST(KEYWORD, 0, 0, -1, "end")
add_node!(t, kw, s, max_padding = 0)
add_node!(t, kw, s; max_padding = 0)
end

fst.typ = t.typ
Expand Down Expand Up @@ -730,8 +730,8 @@ function _short_circuit_to_if!(fst::FST, s::State)
# change it into an if
t = FST(If, fst.indent)
kw = FST(KEYWORD, -1, fst.startline, fst.startline, "if")
add_node!(t, kw, s, max_padding = 0)
add_node!(t, Whitespace(1), s, join_lines = true)
add_node!(t, kw, s; max_padding = 0)
add_node!(t, Whitespace(1), s; join_lines = true)

nodes = fst.nodes::Vector{FST}
idx = findlast(n -> n.typ === OPERATOR && (n.val == "||" || n.val == "&&"), nodes)::Int
Expand All @@ -748,45 +748,45 @@ function _short_circuit_to_if!(fst::FST, s::State)
add_node!(
brackets,
FST(PUNCTUATION, -1, fst.startline, fst.startline, "("),
s,
s;
join_lines = true,
)
add_node!(brackets, Placeholder(0), s)
# inner
lhs = FST(Chain, fst.indent)
for n in nodes[1:(idx-1)]
add_node!(lhs, n, s, join_lines = true)
add_node!(lhs, n, s; join_lines = true)
end
# remove extra ws
if lhs[end].typ === WHITESPACE
lhs[end] = Whitespace(0)
end

add_node!(brackets, lhs, s, join_lines = true)
add_node!(brackets, lhs, s; join_lines = true)
add_node!(brackets, Placeholder(0), s)
add_node!(
brackets,
FST(PUNCTUATION, -1, nodes[idx-2].startline, nodes[idx-2].startline, ")"),
s,
s;
join_lines = true,
)
add_node!(call, brackets, s, join_lines = true)
add_node!(call, brackets, s; join_lines = true)
else
add_node!(call, fst[1], s, join_lines = true)
add_node!(call, fst[1], s; join_lines = true)
end

add_node!(t, call, s, join_lines = true)
add_node!(t, call, s; join_lines = true)
else
# from idx-1 go backwards until we find a node that's not a
lhs = FST(Chain, fst.indent)
for n in nodes[1:(idx-1)]
add_node!(lhs, n, s, join_lines = true)
add_node!(lhs, n, s; join_lines = true)
end
# remove extra ws
if lhs[end].typ === WHITESPACE
lhs[end] = Whitespace(0)
end
add_node!(t, lhs, s, join_lines = true)
add_node!(t, lhs, s; join_lines = true)
end

block1 = FST(Block, fst.indent + s.opts.indent)
Expand All @@ -799,10 +799,10 @@ function _short_circuit_to_if!(fst::FST, s::State)
end
add_node!(block1, n, s)
end
add_node!(t, block1, s, max_padding = s.opts.indent)
add_node!(t, block1, s; max_padding = s.opts.indent)

kw = FST(KEYWORD, 0, 0, -1, "end")
add_node!(t, kw, s, max_padding = 0)
add_node!(t, kw, s; max_padding = 0)

fst.typ = t.typ
fst.nodes = t.nodes
Expand Down
2 changes: 1 addition & 1 deletion src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function print_tree(io::IOBuffer, fst::FST, s::State)
if (fst.typ === Binary || fst.typ === Conditional || fst.typ === ModuleN)
notcode_indent = fst.indent
end
print_tree(io, fst.nodes::Vector{FST}, s, fst.indent, notcode_indent = notcode_indent)
print_tree(io, fst.nodes::Vector{FST}, s, fst.indent; notcode_indent = notcode_indent)
end

function print_tree(
Expand Down
4 changes: 2 additions & 2 deletions src/styles/blue/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function n_tuple!(
if n.typ === NEWLINE
s.line_offset = fst.indent
elseif (i == fidx || i == lidx) && !src_diff_line && nest_to_oneline
fst[i] = Newline(length = n.len)
fst[i] = Newline(; length = n.len)
s.line_offset = fst.indent
elseif n.typ === PLACEHOLDER
if src_diff_line
Expand All @@ -92,7 +92,7 @@ function n_tuple!(
end
end
elseif !nest_to_oneline
fst[i] = Newline(length = n.len)
fst[i] = Newline(; length = n.len)
s.line_offset = fst.indent
else
nest!(style, n, s, lineage)
Expand Down
2 changes: 1 addition & 1 deletion src/styles/blue/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ function p_return(
end
add_node!(t, Whitespace(1), s)
no = FST(KEYWORD, -1, t.endline, t.endline, "nothing")
add_node!(t, no, s, join_lines = true)
add_node!(t, no, s; join_lines = true)
t
end
Loading

0 comments on commit bdd712d

Please sign in to comment.