Skip to content

Commit d5b0db3

Browse files
committed
Now getting the line-numbers right
1 parent ecbf8df commit d5b0db3

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ author = ["Mauro Werder <[email protected]>"]
44
version = "0.12.2"
55

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

1011
[compat]
12+
MacroTools = "0.5"
1113
OrderedCollections = "1"
1214
UnPack = "0.1, 1.0"
1315
julia = "1"

src/Parameters.jl

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ c = BB.c
4141
```
4242
"""
4343
module Parameters
44-
import Base: @__doc__
45-
import OrderedCollections: OrderedDict
44+
using Base: @__doc__
45+
using OrderedCollections: OrderedDict
46+
using MacroTools: rmlines, prewalk, flatten
4647
using UnPack: @unpack, @pack!
4748

4849
export @with_kw, @with_kw_noshow, type2dict, reconstruct, @unpack, @pack!, @pack, @consts
@@ -309,6 +310,16 @@ function with_kw(typedef, mod::Module, withshow=true)
309310
Also, make sure to use a trailing comma for single-field NamedTuples.
310311
""")
311312
end
313+
314+
# record first line number
315+
firstline = nothing
316+
for arg in typedef.args[3].args
317+
if arg isa LineNumberNode
318+
firstline = arg
319+
break
320+
end
321+
end
322+
312323
err1str = "Field \'"
313324
err2str = "\' has no default, supply it with keyword."
314325

@@ -579,19 +590,23 @@ function with_kw(typedef, mod::Module, withshow=true)
579590
end
580591
end
581592

582-
# Finish up
583-
quote
584-
Base.@__doc__ $typ
585-
$outer_positional
586-
$outer_kw
587-
$outer_copy
588-
$showfn
589-
macro $unpack_name(ex)
593+
# Finish up:
594+
# - remove un-needed blocks
595+
# - replace all line numbers pointing (they are all pointing to this file) with firstline
596+
flatten(prewalk(x -> x isa LineNumberNode ? firstline : x,
597+
quote
598+
Base.@__doc__ $typ
599+
$outer_positional
600+
$outer_kw
601+
$outer_copy
602+
$showfn
603+
macro $unpack_name(ex)
590604
esc($Parameters._unpack(ex, $unpack_vars))
591-
end
592-
$pack_macros
593-
$tn
594-
end
605+
end
606+
$pack_macros
607+
$tn
608+
end
609+
))
595610
end
596611

597612
"""
@@ -620,11 +635,17 @@ function with_kw_nt(typedef, mod)
620635
end
621636
NT = gensym(:NamedTuple_kw)
622637
nt = Expr(:tuple, nt...)
638+
639+
# Finish up:
640+
# - remove un-needed blocks
641+
# - there are no line-numbers in the incoming expr, thus remove all
642+
flatten(prewalk(rmlines,
623643
quote
624644
$NT(; $(kwargs...)) =$nt
625645
$NT($(args...)) = $nt
626646
$NT
627-
end
647+
end
648+
))
628649
end
629650

630651
"""

0 commit comments

Comments
 (0)