Skip to content

Commit 66b00f8

Browse files
committed
use CST loc utilities again
1 parent 21017b1 commit 66b00f8

File tree

5 files changed

+18
-89
lines changed

5 files changed

+18
-89
lines changed

Manifest.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
55

66
[[CSTParser]]
77
deps = ["LibGit2", "Test", "Tokenize"]
8-
git-tree-sha1 = "437c93bc191cd55957b3f8dee7794b6131997c56"
8+
git-tree-sha1 = "82be55a328f39a11849569815a9f49911d6d7b58"
9+
repo-rev = "location"
10+
repo-url = "https://github.com/MikeInnes/CSTParser.jl"
911
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
10-
version = "0.5.2"
12+
version = "0.5.2+"
1113

1214
[[Compat]]
1315
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]

src/MacroTools.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ include("examples/threading.jl")
1717
include("examples/forward.jl")
1818

1919
include("patch/diff.jl")
20-
include("patch/location.jl")
2120
include("patch/cst.jl")
2221

2322
const animals = Symbol[]

src/patch/cst.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using CSTParser
2-
using CSTParser: EXPR, Call
2+
using CSTParser: EXPR, Call, Location, exprloc, charrange
33

44
expridx(x, ii) = (@assert isempty(ii); x)
55
expridx(x::Expr, ii) = isempty(ii) ? x : expridx(x.args[ii[1]], ii[2:end])
66

77
function precedence_level(cst::EXPR, loc::Location)
8-
parent = cst[MacroTools.parent(loc)]
8+
parent = cst[CSTParser.parent(loc)]
99
if parent isa Union{CSTParser.BinaryOpCall,CSTParser.UnaryOpCall,CSTParser.ChainOpCall}
1010
Base.operator_precedence(Expr(parent).args[1])
1111
elseif parent isa Union{CSTParser.BinarySyntaxOpCall,CSTParser.UnarySyntaxOpCall}
@@ -18,29 +18,29 @@ end
1818
# Get the range at loc, including trailing trivia from the previous node.
1919
function separator_range(cst, loc)
2020
i = loc.ii[end]
21-
loc = MacroTools.parent(loc)
22-
start = charrange(cst, child(loc, i-1))[1][1]+1
23-
stop = charrange(cst, child(loc, i))[1][end]
21+
loc = CSTParser.parent(loc)
22+
start = charrange(cst, loc[i-1])[1][1]+1
23+
stop = charrange(cst, loc[i])[1][end]
2424
return start:stop
2525
end
2626

2727
function separator(cst, loc, x::EXPR{Call}, i)
2828
length(x.args) == 3 && return ""
2929
length(x.args) == 4 && return ", "
30-
separator_range(cst, child(loc, max(i-1,4)))
30+
separator_range(cst, loc[max(i-1,4)])
3131
end
3232

3333
function separator(cst, loc, x::EXPR{CSTParser.Block}, i)
34-
out, in = charrange(cst, child(loc, max(i-1,1)))
34+
out, in = charrange(cst, loc[max(i-1,1)])
3535
in[end]+1:out[end]
3636
end
3737

3838
function separator(cst, loc, x::CSTParser.BinaryOpCall, i)
39-
separator_range(cst, child(loc, 2))
39+
separator_range(cst, loc[2])
4040
end
4141

4242
function separator(cst::EXPR, loc::Location)
43-
parent = MacroTools.parent(loc)
43+
parent = CSTParser.parent(loc)
4444
separator(cst, parent, cst[parent], loc.ii[end])
4545
end
4646

@@ -57,7 +57,7 @@ function SourceFile(path::String, text = String(read(path)))
5757
end
5858

5959
function replacement(src::SourceFile, p::Replace)
60-
loc = expr_location(src.cst, p.idx)
60+
loc = exprloc(src.cst, p.idx)
6161
prec = precedence_level(src.cst, loc)
6262
_, span = charrange(src.cst, loc)
6363
span => sprint(Base.show_unquoted, p.new, 0, prec)
@@ -66,9 +66,9 @@ end
6666
function replacement(src::SourceFile, p::Insert)
6767
append = p.idx[end] > length(expridx(src.ast, p.idx[1:end-1]).args)
6868
append && (p.idx[end] -= 1)
69-
loc = expr_location(src.cst, p.idx)
69+
loc = exprloc(src.cst, p.idx)
7070
# TODO handle cases like this more generally
71-
src.cst[MacroTools.parent(loc)] isa EXPR{Call} && (loc.ii[end] = max(loc.ii[end], 2))
71+
src.cst[CSTParser.parent(loc)] isa EXPR{Call} && (loc.ii[end] = max(loc.ii[end], 2))
7272
_, span = charrange(src.cst, loc)
7373
point = append ? span[end] : span[1]-1
7474
sep = separator(src.cst, loc)
@@ -81,7 +81,7 @@ function replacement(src::SourceFile, p::Insert)
8181
end
8282

8383
function replacement(src::SourceFile, p::Delete)
84-
loc = expr_location(src.cst, p.idx)
84+
loc = exprloc(src.cst, p.idx)
8585
span, _ = charrange(src.cst, loc)
8686
sep = separator(src.cst, loc)
8787
sep isa AbstractRange || (sep = span)

src/patch/location.jl

Lines changed: 0 additions & 72 deletions
This file was deleted.

test/patch.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ testrep(ex, text) = textmap(_ -> Expr(:file, ex), text)
1919
bar(uppercase("baz"))
2020
"""
2121

22-
@test_broken testrep(:(using Bar), "using Foo") == "using Bar"
22+
@test testrep(:(using Bar), "using Foo") == "using Bar"
2323

2424
# Insertion
2525

0 commit comments

Comments
 (0)