Skip to content

Commit ac4492a

Browse files
authored
Merge pull request #215 from astrozot/master
New utility `rmdocs`
2 parents 937f97b + 35fb3a4 commit ac4492a

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

docs/src/utilities.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ MacroTools.@q
4646
MacroTools.@qq
4747
MacroTools.isexpr
4848
MacroTools.rmlines
49+
MacroTools.rmdocs
4950
MacroTools.unblock
5051
MacroTools.namify
5152
MacroTools.inexpr

src/utils.jl

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export @esc, isexpr, isline, iscall, rmlines, unblock, block, inexpr, namify, isdef,
1+
export @esc, isexpr, isline, iscall, rmlines, rmdocs, unblock, block, inexpr, namify, isdef,
22
longdef, shortdef, @expand, makeif, prettify, combinedef, splitdef, splitarg, combinearg
33

44
macro public(ex)
@@ -122,6 +122,43 @@ end
122122

123123
striplines(ex) = prewalk(rmlines, ex)
124124

125+
"""
126+
rmdocs(x)
127+
128+
Remove the documentation macros from the expression.
129+
130+
### Examples
131+
132+
To work with nested blocks:
133+
134+
```julia
135+
prewalk(rmdocs, ex)
136+
```
137+
138+
See also: [`rmlines`](@ref)
139+
"""
140+
rmdocs(x) = x
141+
function rmdocs(ex::Expr)
142+
if ex.head == :macrocall
143+
m = ex.args[1]
144+
if isa(m, GlobalRef) && m.mod == Core && m.name == Symbol("@doc")
145+
for i 2:length(ex.args)
146+
arg = ex.args[i]
147+
if !isline(arg) && !isnothing(arg)
148+
doc = arg
149+
if i < length(ex.args)
150+
return ex.args[i + 1]
151+
end
152+
end
153+
end
154+
return nothing
155+
end
156+
end
157+
return ex
158+
end
159+
160+
stripdocs(ex) = prewalk(rmdocs, ex)
161+
125162
"""
126163
unblock(expr)
127164
@@ -592,6 +629,12 @@ end
592629
prettify(ex)
593630
594631
Makes generated code generaly nicer to look at.
632+
633+
# Keywords
634+
- `lines::Bool=false`: whether to preserve line number nodes
635+
- `alias::Bool=true`: whether to replace gensyms with animal names
636+
- `docs::Bool=false`: whether to preserve docstrings
595637
"""
596-
prettify(ex; lines = false, alias = true) =
597-
ex |> (lines ? identity : striplines) |> flatten |> unresolve |> resyntax |> (alias ? alias_gensyms : identity)
638+
prettify(ex; lines=false, alias=true, docs=false) =
639+
ex |> (lines ? identity : striplines) |> (docs ? identity : stripdocs) |>flatten |>
640+
unresolve |> resyntax |> (alias ? alias_gensyms : identity)

test/utils.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ end
3838
@test isdef(:(function (y) y - 4 end))
3939
end
4040

41+
@testset "rmlines & rmdocs" begin
42+
ex1 = quote
43+
foo(x) = x + 1
44+
end
45+
ex2 = quote
46+
"My own documentation"
47+
foo(x) = x + 1
48+
49+
"This is a docstring"
50+
bar(x) = x + 2
51+
end
52+
ex3 = quote
53+
foo(x) = x + 1
54+
bar(x) = x + 2
55+
end
56+
@test !any(isline, MacroTools.striplines(ex1).args)
57+
@test !any(isline, MacroTools.striplines(ex2).args)
58+
@test MacroTools.striplines(MacroTools.stripdocs(ex2)) == MacroTools.striplines(ex3)
59+
end
60+
61+
4162
@testset "flatten" begin
4263
@test flatten(quote begin; begin; f(); g(); end; begin; h(); end; f(); end; end) |> striplines == quote f(); g(); h(); f() end |> striplines
4364
end

0 commit comments

Comments
 (0)