Skip to content

Commit

Permalink
Now getting the line-numbers right
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Feb 25, 2021
1 parent ecbf8df commit d5b0db3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ author = ["Mauro Werder <[email protected]>"]
version = "0.12.2"

[deps]
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[compat]
MacroTools = "0.5"
OrderedCollections = "1"
UnPack = "0.1, 1.0"
julia = "1"
Expand Down
51 changes: 36 additions & 15 deletions src/Parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ c = BB.c
```
"""
module Parameters
import Base: @__doc__
import OrderedCollections: OrderedDict
using Base: @__doc__
using OrderedCollections: OrderedDict
using MacroTools: rmlines, prewalk, flatten
using UnPack: @unpack, @pack!

export @with_kw, @with_kw_noshow, type2dict, reconstruct, @unpack, @pack!, @pack, @consts
Expand Down Expand Up @@ -309,6 +310,16 @@ function with_kw(typedef, mod::Module, withshow=true)
Also, make sure to use a trailing comma for single-field NamedTuples.
""")
end

# record first line number
firstline = nothing
for arg in typedef.args[3].args
if arg isa LineNumberNode
firstline = arg
break
end
end

err1str = "Field \'"
err2str = "\' has no default, supply it with keyword."

Expand Down Expand Up @@ -579,19 +590,23 @@ function with_kw(typedef, mod::Module, withshow=true)
end
end

# Finish up
quote
Base.@__doc__ $typ
$outer_positional
$outer_kw
$outer_copy
$showfn
macro $unpack_name(ex)
# Finish up:
# - remove un-needed blocks
# - replace all line numbers pointing (they are all pointing to this file) with firstline
flatten(prewalk(x -> x isa LineNumberNode ? firstline : x,
quote
Base.@__doc__ $typ
$outer_positional
$outer_kw
$outer_copy
$showfn
macro $unpack_name(ex)
esc($Parameters._unpack(ex, $unpack_vars))
end
$pack_macros
$tn
end
end
$pack_macros
$tn
end
))
end

"""
Expand Down Expand Up @@ -620,11 +635,17 @@ function with_kw_nt(typedef, mod)
end
NT = gensym(:NamedTuple_kw)
nt = Expr(:tuple, nt...)

# Finish up:
# - remove un-needed blocks
# - there are no line-numbers in the incoming expr, thus remove all
flatten(prewalk(rmlines,
quote
$NT(; $(kwargs...)) =$nt
$NT($(args...)) = $nt
$NT
end
end
))
end

"""
Expand Down

0 comments on commit d5b0db3

Please sign in to comment.