@@ -41,8 +41,9 @@ c = BB.c
41
41
```
42
42
"""
43
43
module Parameters
44
- import Base: @__doc__
45
- import OrderedCollections: OrderedDict
44
+ using Base: @__doc__
45
+ using OrderedCollections: OrderedDict
46
+ using MacroTools: rmlines, prewalk, flatten
46
47
using UnPack: @unpack , @pack!
47
48
48
49
export @with_kw , @with_kw_noshow , type2dict, reconstruct, @unpack , @pack! , @pack , @consts
@@ -309,6 +310,16 @@ function with_kw(typedef, mod::Module, withshow=true)
309
310
Also, make sure to use a trailing comma for single-field NamedTuples.
310
311
""" )
311
312
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
+
312
323
err1str = " Field \' "
313
324
err2str = " \' has no default, supply it with keyword."
314
325
@@ -579,19 +590,23 @@ function with_kw(typedef, mod::Module, withshow=true)
579
590
end
580
591
end
581
592
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)
590
604
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
+ ))
595
610
end
596
611
597
612
"""
@@ -620,11 +635,17 @@ function with_kw_nt(typedef, mod)
620
635
end
621
636
NT = gensym (:NamedTuple_kw )
622
637
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,
623
643
quote
624
644
$ NT (; $ (kwargs... )) = $ nt
625
645
$ NT ($ (args... )) = $ nt
626
646
$ NT
627
- end
647
+ end
648
+ ))
628
649
end
629
650
630
651
"""
0 commit comments