Skip to content

Commit 904cf6a

Browse files
committed
better toplevelitems API
1 parent 99fed38 commit 904cf6a

7 files changed

Lines changed: 26 additions & 20 deletions

File tree

src/goto.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ function _collecttoplevelitems!(mod::Union{Nothing, String}, entrypath::String,
194194
_collecttoplevelitems!(mod, entrypath, text, pathitemsmaps)
195195
end
196196
function _collecttoplevelitems!(mod::Union{Nothing, String}, entrypath::String, text::String, pathitemsmaps::PathItemsMaps)
197-
parsed = CSTParser.parse(text, true)
198-
items = toplevelitems(parsed, text; mod = mod)
197+
items = toplevelitems(text; mod = mod)
199198
push!(pathitemsmaps, entrypath => items)
200199

201200
# looking for toplevel `include` calls

src/modules.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ function modulefiles(mod::String, entrypath::String, files = Vector{String}())
199199
push!(files, entrypath)
200200

201201
text = read(entrypath, String)
202-
parsed = CSTParser.parse(text, true)
203-
items = toplevelitems(parsed, text; mod = mod)
202+
items = toplevelitems(text; mod = mod)
204203

205204
for item in items
206205
if item isa ToplevelCall

src/outline.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ end
1515

1616
# NOTE: update outline and symbols cache all in one go
1717
function updateeditor(text, mod = "Main", path = nothing, updateSymbols = true)
18-
parsed = CSTParser.parse(text, true)
19-
items = toplevelitems(parsed, text)
18+
items = toplevelitems(text)
2019

2120
# update symbols cache
2221
# ref: https://github.com/JunoLab/Juno.jl/issues/407

src/static/toplevel.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,24 @@ struct ToplevelTupleH <: ToplevelItem
2424
lines::UnitRange{Int}
2525
end
2626

27-
function toplevelitems(
28-
expr, text, # necessary
27+
"""
28+
toplevelitems(text; kwargs...)
29+
30+
Returns [`ToplevelItem`](@ref)s in `text`.
31+
32+
keyword arguments:
33+
- `mod::Union{Nothing, String}`: if not `nothing` don't return items within modules
34+
other than `mod`, otherwise enter into every module.
35+
"""
36+
function toplevelitems(text; kwargs...)
37+
parsed = CSTParser.parse(text, true)
38+
_toplevelitems(text, parsed; kwargs...)
39+
end
40+
41+
function _toplevelitems(
42+
text, expr,
2943
items::Vector{ToplevelItem} = Vector{ToplevelItem}(), line = 1, pos = 1;
30-
mod::Union{Nothing, String} = nothing, # if given, don't enter into modules other than `mod`
44+
mod::Union{Nothing, String} = nothing,
3145
)
3246
# binding
3347
bind = CSTParser.bindingof(expr)
@@ -50,7 +64,7 @@ function toplevelitems(
5064
if shouldenter(expr, mod)
5165
if expr.args !== nothing
5266
for arg in expr.args
53-
toplevelitems(arg, text, items, line, pos; mod = mod)
67+
_toplevelitems(text, arg, items, line, pos; mod = mod)
5468
line += countlines(arg, text, pos)
5569
pos += arg.fullspan
5670
end

test/goto.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@
182182

183183
@testset "updating toplevel symbols" begin
184184
function updatesymbols(mod, path, text)
185-
parsed = CSTParser.parse(text, true)
186-
items = Atom.toplevelitems(parsed, text)
185+
items = Atom.toplevelitems(text)
187186
Atom.updatesymbols(items, mod, path, text)
188187
end
189188

test/outline.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
@testset "outline" begin
2-
function outline(str)
3-
parsed = CSTParser.parse(str, true)
4-
items = Atom.toplevelitems(parsed, str)
5-
Atom.outline(items)
6-
end
2+
outline(text) = Atom.outline(Atom.toplevelitems(text))
73

84
let str = """
95
module Foo

test/static/toplevel.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
parsed = CSTParser.parse(text, true)
77

88
# basic -- finds every toplevel items when `mod` options is `nothing` (default)
9-
@test filter(toplevelitems(parsed, text; mod = nothing)) do item
9+
@test filter(toplevelitems(text; mod = nothing)) do item
1010
item isa Atom.ToplevelBinding &&
1111
item.bind.name == "imwithdoc"
1212
end |> length === 3
1313

1414
# don't enter non-target modules, e.g.: submodules
15-
@test filter(toplevelitems(parsed, text; mod = "Junk")) do item
15+
@test filter(toplevelitems(text; mod = "Junk")) do item
1616
item isa Atom.ToplevelBinding &&
1717
item.bind.name == "imwithdoc"
1818
end |> length === 1 # should only find the `imwithdoc` in Junk module
1919

2020
# don't include items outside of a module
2121
# FIX: currently broken -- include `imwithdoc` in Junk module as well
22-
@test_broken filter(toplevelitems(parsed, text; mod = "SubJunk")) do item
22+
@test_broken filter(toplevelitems(text; mod = "SubJunk")) do item
2323
item isa Atom.ToplevelBinding &&
2424
item.bind.name == "imwithdoc"
2525
end |> length === 1 # should only find the `imwithdoc` in SubJunk module

0 commit comments

Comments
 (0)