@@ -41,8 +41,9 @@ c = BB.c
4141```
4242"""
4343module Parameters
44- import Base: @__doc__
45- import OrderedCollections: OrderedDict
44+ using Base: @__doc__
45+ using OrderedCollections: OrderedDict
46+ using MacroTools: rmlines, prewalk, flatten
4647using UnPack: @unpack , @pack!
4748
4849export @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+ ))
595610end
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+ ))
628649end
629650
630651"""
0 commit comments