From d5b0db3c52cb2145b1f1d804455135f345a12906 Mon Sep 17 00:00:00 2001 From: Mauro Werder Date: Thu, 25 Feb 2021 16:52:00 +0100 Subject: [PATCH] Now getting the line-numbers right --- Project.toml | 2 ++ src/Parameters.jl | 51 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 610fb73..45d7d8b 100644 --- a/Project.toml +++ b/Project.toml @@ -4,10 +4,12 @@ author = ["Mauro Werder "] 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" diff --git a/src/Parameters.jl b/src/Parameters.jl index 12ebccc..243bb18 100644 --- a/src/Parameters.jl +++ b/src/Parameters.jl @@ -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 @@ -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." @@ -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 """ @@ -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 """