Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
[compat]
CommonMark = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10"
Glob = "1.3"
JuliaSyntax = "^0.4.10"
JuliaSyntax = "1"
PrecompileTools = "1"
TOML = "1"
Test = "1"
Expand Down
4 changes: 3 additions & 1 deletion src/JuliaFormatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

using PrecompileTools: @setup_workload, @compile_workload
using JuliaSyntax
using JuliaSyntax: haschildren, children, span, @K_str, kind, @KSet_str
using JuliaSyntax: children, span, @K_str, kind, @KSet_str
using TOML: parsefile
using Glob
import CommonMark: block_modifier
Expand All @@ -34,6 +34,8 @@ export format,
SciMLStyle,
MinimalStyle

haschildren(node::JuliaSyntax.GreenNode) = !JuliaSyntax.is_leaf(node)

struct Configuration
args::Dict{String,Any}
file::Dict{String,Any}
Expand Down
43 changes: 41 additions & 2 deletions src/fst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function is_macrostr(t::JuliaSyntax.GreenNode)::Bool
end

function contains_macrostr(t::JuliaSyntax.GreenNode)::Bool
if kind(t) in KSet"StringMacroName CmdMacroName core_@cmd"
if kind(t) in KSet"StringMacroName CmdMacroName"
return true
elseif kind(t) === "quote" && haschildren(t)
return contains_macrostr(t[1])
Expand Down Expand Up @@ -547,6 +547,21 @@ function _callinfo(x::JuliaSyntax.GreenNode)
return 0, 0
end
k = kind(x)
if k === K"call" && JuliaSyntax.is_infix_op_call(x)
args = count(n -> !JuliaSyntax.is_whitespace(n), children(x))
return div(args - 1, 2), div(args + 1, 2)
elseif k === K"dotcall" && JuliaSyntax.is_infix_op_call(x)
args = count(n -> !JuliaSyntax.is_whitespace(n), children(x))
nops = div(args - 1, 3)
return nops, nops + 1
elseif k === K"op="
return 1, 2
elseif JuliaSyntax.is_operator(x) && haschildren(x)
args = count(n -> !JuliaSyntax.is_whitespace(n), children(x))
if args >= 3
return div(args - 1, 2), div(args + 1, 2)
end
end
n_operators = 0
n_args = 0

Expand All @@ -565,7 +580,7 @@ function _callinfo(x::JuliaSyntax.GreenNode)
end

function is_unary(x::JuliaSyntax.GreenNode)
if JuliaSyntax.is_unary_op(x)
if JuliaSyntax.is_unary_op(x) && !haschildren(x)
return true
end
if kind(x) in KSet"call dotcall" || (JuliaSyntax.is_operator(x) && haschildren(x))
Expand Down Expand Up @@ -646,6 +661,13 @@ function is_function_or_macro_def(cst::JuliaSyntax.GreenNode)
return false
end

function is_short_function_def(cst::JuliaSyntax.GreenNode)
kind(cst) === K"function" && haschildren(cst) || return false
idx = findfirst(n -> !JuliaSyntax.is_whitespace(n), children(cst))
isnothing(idx) && return false
return kind(cst[idx]) !== K"function"
end
Comment on lines +664 to +669
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function is_short_function_def(cst::JuliaSyntax.GreenNode)
kind(cst) === K"function" && haschildren(cst) || return false
idx = findfirst(n -> !JuliaSyntax.is_whitespace(n), children(cst))
isnothing(idx) && return false
return kind(cst[idx]) !== K"function"
end
is_short_function_def(cst::JuliaSyntax.GreenNode) = JuliaSyntax.has_flags(cst, JuliaSyntax.SHORT_FORM_FUNCTION_FLAG)


function is_function_like_lhs(node::JuliaSyntax.GreenNode)
k = kind(node)
if k in KSet"call dotcall"
Expand Down Expand Up @@ -813,6 +835,23 @@ function eq_to_in_normalization!(fst::FST, always_for_in::Bool, for_in_replaceme
op.val = "="
op.len = length(op.val)
end
if !isnothing(fst.metadata)
metadata = fst.metadata::Metadata
opkind = try
JuliaSyntax.Kind(op.val)
catch
metadata.op_kind
end
fst.metadata = Metadata(
opkind,
metadata.op_dotted,
metadata.is_standalone_shortcircuit,
metadata.is_short_form_function,
op.val == "=",
metadata.is_long_form_function,
metadata.has_multiline_argument,
)
end
elseif fst.typ === Block || fst.typ === Brackets || fst.typ === Filter
past_if = false
for n in fst.nodes::Vector
Expand Down
3 changes: 3 additions & 0 deletions src/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ function pipe_to_function_call(fst::FST)
arg2[end][end].typ === IDENTIFIER
n = FST(PUNCTUATION, -1, arg2.endline, arg2.endline, ".")
push!(nodes, n)
elseif dot && arg2.typ === Accessor && arg2[end].typ === IDENTIFIER
n = FST(PUNCTUATION, -1, arg2.endline, arg2.endline, ".")
push!(nodes, n)
elseif dot && arg2.typ === Brackets
idx = findfirst(n -> n.typ === Binary && op_kind(n) === K"->", arg2.nodes)
if idx !== nothing
Expand Down
8 changes: 4 additions & 4 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function format_check(io::IOBuffer, fst::FST, s::State)
return
end

line_range = fst.startline:fst.endline
line_range = (fst.startline):(fst.endline)
skip = s.doc.format_skips[1]
nlines = numlines(s.doc)

Expand All @@ -19,7 +19,7 @@ function format_check(io::IOBuffer, fst::FST, s::State)
write(io, JuliaSyntax.sourcetext(s.doc.srcfile)[first(r1):last(r2)])
end

output = JuliaSyntax.sourcetext(s.doc.srcfile)[skip.startoffset:skip.endoffset]
output = JuliaSyntax.sourcetext(s.doc.srcfile)[(skip.startoffset):(skip.endoffset)]
l1 = skip.endline + 1
l2 = fst.endline

Expand Down Expand Up @@ -52,7 +52,7 @@ function format_check(io::IOBuffer, fst::FST, s::State)
end
s.on = false
elseif !s.on && skip.endline in line_range
output = JuliaSyntax.sourcetext(s.doc.srcfile)[skip.startoffset:skip.endoffset]
output = JuliaSyntax.sourcetext(s.doc.srcfile)[(skip.startoffset):(skip.endoffset)]
l1 = skip.endline + 1
l2 = fst.endline

Expand Down Expand Up @@ -180,7 +180,7 @@ function print_notcode(io::IOBuffer, fst::FST, s::State)
if !(s.on)
return
end
for l in fst.startline:fst.endline
for l in (fst.startline):(fst.endline)
_, v = get(s.doc.comments, l, (0, "\n"))
ws = fst.indent

Expand Down
Loading
Loading