Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now getting the line-numbers right #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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