Skip to content

Commit 8b08c50

Browse files
committed
block insertion
1 parent a46b3c5 commit 8b08c50

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/patch/cst.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function separator(cst, loc, x::EXPR{Call}, i)
3030
separator_range(cst, CSTParser.child(loc, max(i-1,4)))
3131
end
3232

33+
function separator(cst, loc, x::EXPR{CSTParser.Block}, i)
34+
out, in = charrange(cst, CSTParser.child(loc, max(i-1,1)))
35+
in[end]+1:out[end]
36+
end
37+
3338
function separator(cst, loc, x::CSTParser.BinaryOpCall, i)
3439
separator_range(cst, CSTParser.child(loc, 2))
3540
end
@@ -64,7 +69,7 @@ function replacement(src::SourceFile, p::Insert)
6469
loc = expr_location(src.cst, p.idx)
6570
# TODO handle cases like this more generally
6671
src.cst[CSTParser.parent(loc)] isa EXPR{Call} && (loc.ii[end] = max(loc.ii[end], 2))
67-
span, _ = charrange(src.cst, loc)
72+
_, span = charrange(src.cst, loc)
6873
point = append ? span[end] : span[1]-1
6974
sep = separator(src.cst, loc)
7075
sep isa AbstractRange && (sep = src.text[sep])

test/patch.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ testrep(ex, text) = textmap(_ -> Expr(:file, ex), text)
3333
@test testrep(:(a+b+c), "a + b") == "a + b + c"
3434
@test testrep(:(a+b+c), "a+b") == "a+b+c"
3535

36+
testrep(:(1; 2; 3), """
37+
begin
38+
1
39+
2
40+
end
41+
""") == """
42+
begin
43+
1
44+
2
45+
3
46+
end
47+
"""
48+
49+
testrep(:(1; 2; 3), """
50+
begin
51+
1; 2
52+
end
53+
""") == """
54+
begin
55+
1; 2; 3
56+
end
57+
"""
58+
59+
testrep(:(1; 2; 3), """
60+
begin
61+
1; 2;
62+
end
63+
""") == """
64+
begin
65+
1; 2; 3;
66+
end
67+
"""
68+
3669
# Deletion
3770

3871
@test testrep(:(f()), "f(a)") == "f()"

0 commit comments

Comments
 (0)