Skip to content

Commit d0f055e

Browse files
committed
macros & tuples
1 parent 2b58896 commit d0f055e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/patch/cst.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ function separator(cst, loc, x::EXPR{Call}, i)
3131
separator_range(cst, loc[max(i-1,4)])
3232
end
3333

34+
function separator(cst, loc, x::EXPR{CSTParser.MacroCall}, i)
35+
if get(x.args, 2, nothing) isa CSTParser.PUNCTUATION # @foo(a, b)
36+
length(x.args) == 3 && return ""
37+
length(x.args) == 4 && return ", "
38+
separator_range(cst, loc[max(i-1,4)])
39+
else # @foo a b
40+
length(x.args) == 1 && return " "
41+
outer, inner = charrange(cst, loc[i-1])
42+
inner[end]+1:outer[end]
43+
end
44+
end
45+
46+
function separator(cst, loc, x::EXPR{CSTParser.TupleH}, i)
47+
brackets = x.args[1] isa CSTParser.PUNCTUATION
48+
length(x.args) == 2brackets && return ""
49+
length(x.args) == 1+2brackets && return ", "
50+
separator_range(cst, loc[max(i-1,2+brackets)])
51+
end
52+
53+
ex = CSTParser.parse("a, b")
54+
separator(ex, CSTParser.Location(), ex, 1)
55+
56+
ex = CSTParser.parse("(a,)")
57+
separator(ex, CSTParser.Location(), ex, 2)
58+
3459
function separator(cst, loc, x::EXPR{CSTParser.Block}, i)
3560
out, in = charrange(cst, loc[max(i-1,1)])
3661
in[end]+1:out[end]

test/patch.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ testrep(:(1; 2; 3), "(1; 2)") == "(1; 2; 3)"
7979
@test testrep(:(f(a, c)), "f(a, b, c)") == "f(a, c)"
8080
@test testrep(:(f(b, c)), "f(a, b, c)") == "f(b, c)"
8181
@test testrep(:(a(b, c)), "f(a, b, c)") == "a(b, c)"
82+
83+
# Others
84+
85+
@test testrep(:(@foo a), "@foo a b") == "@foo a"
86+
@test testrep(:(@foo a b c), "@foo a b") == "@foo a b c"
87+
@test testrep(:(@foo a b c), "@foo(a, b)") == "@foo(a, b, c)"
88+
89+
@test testrep(:(a,), "(a, b)") == "(a)"
90+
@test testrep(:(a,b,c), "a, b") == "a, b, c"

0 commit comments

Comments
 (0)