Skip to content

Commit 312b2aa

Browse files
committed
improve some edge cases
1 parent 4a473e8 commit 312b2aa

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/patch/cst.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ function precedence_level(cst::EXPR, loc::Location)
1515
end
1616
end
1717

18-
separator(x::EXPR{Call}, i::Integer) = max(i-1, 4)
18+
function separator(cst, loc, x::EXPR{Call}, i)
19+
length(x.args) == 3 && return ""
20+
length(x.args) == 4 && return ", "
21+
charrange(cst, CSTParser.child(loc, max(i-1, 4)))[1]
22+
end
23+
24+
function separator(cst, loc, x::CSTParser.BinaryOpCall, i)
25+
charrange(cst, CSTParser.child(loc, 2))[1]
26+
end
1927

2028
function separator(cst::EXPR, loc::Location)
2129
parent = CSTParser.parent(loc)
22-
i = separator(cst[parent], loc.ii[end])
23-
CSTParser.child(parent, i)
30+
separator(cst, parent, cst[parent], loc.ii[end])
2431
end
2532

2633
struct SourceFile
@@ -46,20 +53,24 @@ function replacement(src::SourceFile, p::Insert)
4653
append = p.idx[end] > length(expridx(src.ast, p.idx[1:end-1]).args)
4754
append && (p.idx[end] -= 1)
4855
loc = expr_location(src.cst, p.idx)
56+
# TODO handle cases like this more generally
57+
src.cst[CSTParser.parent(loc)] isa EXPR{Call} && (loc.ii[end] = max(loc.ii[end], 2))
4958
span, _ = charrange(src.cst, loc)
5059
point = append ? span[end] : span[1]-1
51-
sep = charrange(src.cst, separator(src.cst, loc))[1]
60+
sep = separator(src.cst, loc)
61+
sep isa AbstractRange && (sep = src.text[sep])
5262
(1:0).+point => sprint() do io
53-
append && write(io, src.text[sep])
63+
append && write(io, sep)
5464
Base.show_unquoted(io, p.tree, 0, 0)
55-
append || write(io, src.text[sep])
65+
append || write(io, sep)
5666
end
5767
end
5868

5969
function replacement(src::SourceFile, p::Delete)
6070
loc = expr_location(src.cst, p.idx)
6171
span, _ = charrange(src.cst, loc)
62-
sep = charrange(src.cst, separator(src.cst, loc))[1]
72+
sep = separator(src.cst, loc)
73+
sep isa AbstractRange || (sep = span)
6374
span = span[1] > sep[1] ? (sep[1]:span[end]) : (span[1]:sep[end])
6475
span => ""
6576
end

test/patch.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ using Test
1919

2020
# Insertion
2121

22+
@test textwalk(x -> isexpr(x, :call) ? :(f(a,b)) : x, "f(a)") == "f(a, b)"
23+
@test textwalk(x -> isexpr(x, :call) ? :(f(a)) : x, "f()") == "f(a)"
24+
2225
@test textwalk(x -> isexpr(x, :call) ? :(f(a, b, c)) : x, "f(a, b)") == "f(a, b, c)"
2326
@test textwalk(x -> isexpr(x, :call) ? :(f(a, c, b)) : x, "f(a, b)") == "f(a, c, b)"
2427
@test textwalk(x -> isexpr(x, :call) ? :(f(c, a, b)) : x, "f(a, b)") == "f(c, a, b)"
2528
@test textwalk(x -> isexpr(x, :call) ? :(c(f, a, b)) : x, "f(a, b)") == "c(f, a, b)"
2629

2730
# Deletion
2831

32+
@test textwalk(x -> isexpr(x, :call) ? :(f()) : x, "f(a)") == "f()"
33+
@test textwalk(x -> isexpr(x, :call) ? :(f()) : x, "f(a,)") == "f()"
34+
2935
@test textwalk(x -> isexpr(x, :call) ? :(f(a, b)) : x, "f(a, b, c)") == "f(a, b)"
3036
@test textwalk(x -> isexpr(x, :call) ? :(f(a, c)) : x, "f(a, b, c)") == "f(a, c)"
3137
@test textwalk(x -> isexpr(x, :call) ? :(f(b, c)) : x, "f(a, b, c)") == "f(b, c)"

0 commit comments

Comments
 (0)