Skip to content

Commit

Permalink
dump
Browse files Browse the repository at this point in the history
  • Loading branch information
domluna committed Mar 17, 2019
1 parent 5607f57 commit 4f50eb8
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.jl
6 changes: 6 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ version = "0.5.2"
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[DocumentFormat]]
deps = ["CSTParser", "Test", "Tokenize"]
path = "/home/domluna/.julia/dev/DocumentFormat"
uuid = "ffa9a821-9c82-50df-894e-fbcef3ed31cd"
version = "0.3.0+"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ version = "0.1.0"

[deps]
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
DocumentFormat = "ffa9a821-9c82-50df-894e-fbcef3ed31cd"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624"
4 changes: 1 addition & 3 deletions src/JLFmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ function format(text::String; indent_width=4, max_width=80)
s = State(d, indent_width, 0, 1, 0, max_width)
x = CSTParser.parse(text, true)
tree = pretty(x, s)
#= @info "" tree =#

nest!(tree, s)
@info "" tree
#= @info "" tree =#

io = IOBuffer()
print_tree(io, tree, s)
Expand Down
190 changes: 117 additions & 73 deletions src/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,41 @@
#
# This is by replacing `Placeholder` nodes with `Newline` nodes
# and updated the PTree's indent.
#
#
# TODO: When nest! recurses into arg1
# it doesn't consider of length of " op"
#
# This would at most be 4, i.e. " ==="
# so not a dealbreaker.
#
# NOTE: this also applies to WhereOpCall nodes
#

#= nest!(::PLeaf, ::State) = nothing =#


rem_length(x::AbstractVector) = reverse(cumsum(reverse(length.(x))))

# nest is a versionf of nest! that doesn't mutate the PTree
# it just increments/resets the line_offset.
#
# This is useful for correctly resetting the line_offset after
# nesting a recursive node

nest(x::PLeaf, s::State) = (s.line_offset += length(x); nothing)

function nest(x::PTree, s::State)
@info "" typeof(x) s.line_offset
for (i, n) in enumerate(x.nodes)
walk(f, x::PLeaf, s::State) = nothing
function walk(f, x::PTree, s::State)
#= @info "" typeof(x) s.line_offset =#
for n in x.nodes
if n === Newline
s.line_offset = x.indent
else
nest(n, s)
f(n, s)
walk(f, n, s)
end
end
end

nest!(x::PLeaf, s::State) = (s.line_offset += length(x); nothing)
# Used to correctly reset the State's line_offset.
reset_line_offset(_, _) = nothing
reset_line_offset(x::PLeaf, s::State) = (s.line_offset += length(x); nothing)

nest!(x::PLeaf, s::State) = (s.line_offset += length(x); nothing)
function nest!(x::PTree, s::State)
@info "" typeof(x) s.line_offset
#= @info "" typeof(x) s.line_offset =#
for (i, n) in enumerate(x.nodes)
if n === Newline
s.line_offset = x.indent
Expand All @@ -41,17 +47,15 @@ function nest!(x::PTree, s::State)
end
end
end


#

function nest!(x::PTree{CSTParser.EXPR{T}}, s::State) where T <: Union{CSTParser.Curly,CSTParser.Call}
@info "" typeof(x) s.line_offset x length(x)
#= @info "" typeof(x) s.line_offset x length(x) =#
if s.line_offset + length(x) > s.max_width
x.indent = s.line_offset + length(x.nodes[1]) + length(x.nodes[2])
s.line_offset = x.indent

lens = rem_length(x.nodes[3:end])
@info "" lens s.line_offset
#= @info "" lens s.line_offset =#
for (i, n) in enumerate(x.nodes[3:end])
#= @info "" typeof(n) n s.line_offset =#
if n === Newline
Expand All @@ -78,7 +82,7 @@ function nest!(x::PTree{CSTParser.EXPR{T}}, s::State) where T <: Union{CSTParser
end

function nest!(x::PTree{CSTParser.EXPR{T}}, s::State) where T <: Union{CSTParser.TupleH,CSTParser.Vect,CSTParser.InvisBrackets,CSTParser.Braces}
@info "" typeof(x) s.line_offset length(x)
#= @info "" typeof(x) s.line_offset length(x) =#
if s.line_offset + length(x) > s.max_width
x.indent = s.line_offset
x.nodes[1] isa PLeaf{CSTParser.PUNCTUATION} && (x.indent += 1)
Expand Down Expand Up @@ -110,7 +114,7 @@ end
# nest B prior to nesting A
# only nest A if the line still exceeds the maximum width
function nest!(x::PTree{CSTParser.WhereOpCall}, s::State)
@info "" typeof(x) s.line_offset length(x)
#= @info "" typeof(x) s.line_offset length(x) =#
if s.line_offset + length(x) > s.max_width
line_offset = s.line_offset
# after "A where "
Expand All @@ -122,8 +126,8 @@ function nest!(x::PTree{CSTParser.WhereOpCall}, s::State)
x.indent = s.line_offset
is_lbrace(x.nodes[idx+1]) && (x.indent += 1)

@info "" typeof(x) s.line_offset x length(x.nodes)
@info "" idx Alens Blens
#= @info "" typeof(x) s.line_offset x length(x.nodes) =#
#= @info "" idx Alens Blens =#

# B
for (i, n) in enumerate(x.nodes[idx+1:end])
Expand All @@ -144,82 +148,122 @@ function nest!(x::PTree{CSTParser.WhereOpCall}, s::State)
end

# If A was nested we need to redo the indent for B
# TODO: need to propagate new indent
if line_offset + Alens[1] > s.max_width
x.indent = s.line_offset
if is_lbrace(x.nodes[idx+1])
x.indent += 1
elseif x.nodes[idx+1] isa PTree{CSTParser.EXPR{CSTParser.Curly}}
# reset the indent of the Curly node, +1 for {
x.nodes[idx+1].indent = x.indent + length(x.nodes[idx+1].nodes[1]) + 1
#= x.indent = x.nodes[idx+1].indent =#
elseif x.nodes[idx+1] isa PTree{<:Union{CSTParser.BinaryOpCall,CSTParser.BinarySyntaxOpCall}}
# NOTE: don't need this if a binary call of ISSUBTYPE is not nestable
# T <: S
x.nodes[idx+1].indent = x.indent
# The difference between the unnested pretty length
# of "A where" and the length of the last line, i.e.:
#
# f(a, b) where B
#
# vs.
#
# f(a,
# b) where B
#
# would be 3.
#
# All CSTParser.Curly and CSTParser.Braces nodes
# would have to be detented by 3.
diff = Alens[1] - s.line_offset
f = (x, s) -> begin
if x isa PTree{CSTParser.EXPR{CSTParser.Curly}}
x.indent -= diff
elseif x isa PTree{CSTParser.EXPR{CSTParser.Braces}}
x.indent -= diff
end
end
for (i, n) in enumerate(x.nodes[idx+1:end])
if is_lbrace(n)
x.indent += 1
else
f(n, s)
walk(f, n, s)
end
end
end

s.line_offset = line_offset
nest(x, s)
@info "EXIT" typeof(x) s.line_offset
walk(reset_line_offset, x, s)
else
for (i, n) in enumerate(x.nodes)
@info "WHEREOP" typeof(n) s.line_offset length(n)
#= @info "WHEREOP" typeof(n) s.line_offset length(n) =#
if n === Newline
s.line_offset = x.indent
else
nest!(n, s)
end
end
end
#= @info "EXIT" typeof(x) s.line_offset =#
end

# C ? E1 : E2
#
# fold in order of
#
# C ? E1 :
# E2
#
# C ?
# E1 :
# E2
function nest!(x::PTree{CSTParser.ConditionalOpCall}, s::State)
@info "" typeof(x) s.line_offset x
#= @info "ENTER" typeof(x) s.line_offset x =#
if s.line_offset + length(x) > s.max_width
#= idx1 = findfirst(n -> is_placeholder(n), x.nodes)[1] =#
#= idx2 = findlast(n -> is_placeholder(n), x.nodes)[1] =#
#= Clens = rem_length(x.nodes[1:idx1]) =#
#= E1lens = rem_length(x.nodes[idx1+1:idx2]) =#
#= E2lens = rem_length(x.nodes[idx2+1:end]) =#

idx1 = findfirst(n -> is_placeholder(n), x.nodes)[1]
idx2 = findlast(n -> is_placeholder(n), x.nodes)[1]
Clens = rem_length(x.nodes[1:idx1])
E1lens = rem_length(x.nodes[idx1+1:idx2])
E2lens = rem_length(x.nodes[idx2+1:end])

line_offset = s.line_offset
x.indent = s.line_offset
lens = length.(x.nodes)
alllens = rem_length(x.nodes)
idx = length(lens)
for (i, n) in enumerate(reverse(x.nodes))
s.line_offset = x.indent + alllens[i]
l = sum(lens[end-i+2:idx])
#= @info "" typeof(n) s.line_offset n l =#
if n == Newline
s.line_offset = x.indent
elseif is_placeholder(n) && s.line_offset + l > s.max_width
x.nodes[end-i+1] = Newline
idx = length(lens)-i
s.line_offset = x.indent
else
nest!(n, s)
end
s.line_offset = Clens[1] + E1lens[1]

# E2
if s.line_offset + E2lens[1] > s.max_width
x.nodes[idx2] = Newline
s.line_offset = x.indent
end
nest!(x.nodes[end], s)

# E1
s.line_offset = line_offset + Clens[1]
if s.line_offset + E1lens[1] > s.max_width
x.nodes[idx1] = Newline
s.line_offset = x.indent
end
# TODO: set line_offset
s.line_offset = x.indent + length(x)

for n in x.nodes[idx1+1:idx2-1]
nest!(n, s)
end

# C
s.line_offset = line_offset
for n in x.nodes[1:idx1-1]
nest!(n, s)
end

# reset line_offset
s.line_offset = line_offset
walk(reset_line_offset, x, s)
else
for (i, n) in enumerate(x.nodes)
@info "" typeof(n) s.line_offset length(n)
#= @info "" typeof(n) s.line_offset length(n) =#
if n === Newline
s.line_offset = x.indent
else
nest!(n, s)
end
end
end
#= @info "EXIT" typeof(x) s.line_offset =#
end

# arg1 op arg2
function nest!(x::PTree{T}, s::State; indent=-1) where T <: Union{CSTParser.BinaryOpCall,CSTParser.BinarySyntaxOpCall}
@info "ENTER" typeof(x) s.line_offset x length(x)
#
function nest!(x::PTree{T}, s::State) where T <: Union{CSTParser.BinaryOpCall,CSTParser.BinarySyntaxOpCall}
#= @info "ENTER" typeof(x) s.line_offset x length(x) =#
if s.line_offset + length(x) > s.max_width && findfirst(n -> is_placeholder(n), x.nodes) !== nothing
line_offset = s.line_offset
# idx op is 1 before the placeholder
Expand All @@ -231,35 +275,35 @@ function nest!(x::PTree{T}, s::State; indent=-1) where T <: Union{CSTParser.Bina
x.indent = s.line_offset
s.line_offset += arg1lens[1]

@info "" typeof(x) s.line_offset x arg2lens[1]
@info "" arg1lens arg2lens
#= @info "" typeof(x) s.line_offset x arg2lens[1] =#
#= @info "" arg1lens arg2lens =#

if s.line_offset + arg2lens[1] > s.max_width
x.nodes[idx+1] = Newline
s.line_offset = x.indent
end
# arg2
@info "nesting arg2"
#= @info "nesting arg2" =#
nest!(x.nodes[end], s)

# arg1 op
s.line_offset = line_offset
@info "nesting arg1"
#= @info "nesting arg1" =#
for (i, n) in enumerate(x.nodes[1:idx])
nest!(n, s)
end

s.line_offset = line_offset
nest(x, s)
@info "EXIT" typeof(x) s.line_offset
walk(reset_line_offset, x, s)
else
for (i, n) in enumerate(x.nodes)
@info "" typeof(n) s.line_offset length(n)
#= @info "" typeof(n) s.line_offset length(n) =#
if n === Newline
s.line_offset = x.indent
else
nest!(n, s)
end
end
end
#= @info "EXIT" typeof(x) s.line_offset =#
end
11 changes: 5 additions & 6 deletions src/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# const placeholder = Placeholder()
# const placeholderWS = PlaceholderWS()

struct PLeaf{T<:CSTParser.LeafNode}
struct PLeaf{T}
startline::Int
endline::Int
text::String
Expand All @@ -54,7 +54,7 @@ is_empty_space(x::PLeaf{CSTParser.LITERAL}) = x.text == "" && x !== Placeholder
is_placeholder(_) = false
is_placeholder(x::PLeaf{CSTParser.LITERAL}) = x === Placeholder || x === PlaceholderWS

mutable struct PTree{T<:CSTParser.AbstractEXPR}
mutable struct PTree{T}
startline::Int
endline::Int
indent::Int
Expand Down Expand Up @@ -88,11 +88,10 @@ function add_node!(x::PTree, node::Union{PTree,PLeaf}; join_lines=false)
return
end

function pretty(x::Union{Vector,CSTParser.AbstractEXPR}, s::State)
function pretty(x::Union{AbstractVector,CSTParser.AbstractEXPR}, s::State)
t = PTree(x, nspaces(s))
for a in x
n = pretty(a, s)
add_node!(t, n, join_lines=true)
add_node!(t, pretty(a, s), join_lines=true)
end
t
end
Expand Down Expand Up @@ -265,7 +264,7 @@ function pretty(x::CSTParser.EXPR{CSTParser.MacroCall}, s::State)
# same as CSTParser.EXPR{CSTParser.CALL} but whitespace sensitive
for (i, a) in enumerate(x)
n = pretty(a, s)
@info "MACROCALL" a
#= @info "MACROCALL" a =#

# i == 1 is probably redundant
if i == 1 && a isa CSTParser.EXPR{CSTParser.MacroName}
Expand Down
Loading

0 comments on commit 4f50eb8

Please sign in to comment.