Compare commits

...

3 Commits

Author SHA1 Message Date
Simon Christ
fe21c4de89 add non_underscore and plural aliases 2020-10-18 15:46:25 +02:00
Simon Christ
8dd2a7d099 add underscore 2020-10-15 22:20:06 +02:00
Simon Christ
14e78e7235 add add_attributes macro 2020-10-15 22:10:07 +02:00
3 changed files with 124 additions and 42 deletions

View File

@ -189,9 +189,9 @@ import RecipesPipeline: SliceIt,
include("types.jl") include("types.jl")
include("utils.jl") include("utils.jl")
include("components.jl")
include("axes.jl") include("axes.jl")
include("args.jl") include("args.jl")
include("components.jl")
include("themes.jl") include("themes.jl")
include("plot.jl") include("plot.jl")
include("pipeline.jl") include("pipeline.jl")

View File

@ -1,21 +1,34 @@
function makeplural(s::Symbol)
str = string(s)
if last(str) != 's'
return Symbol(string(s,"s"))
end
return s
end
function make_non_underscore(s::Symbol)
str = string(s)
str = replace(str, "_" => "")
return Symbol(str)
end
const _keyAliases = Dict{Symbol,Symbol}() const _keyAliases = Dict{Symbol,Symbol}()
function add_aliases(sym::Symbol, aliases::Symbol...) function add_aliases(sym::Symbol, aliases::Symbol...)
for alias in aliases for alias in aliases
if haskey(_keyAliases, alias) if haskey(_keyAliases, alias) || alias === sym
error("Already an alias $alias => $(_keyAliases[alias])... can't also alias $sym") return nothing
end end
_keyAliases[alias] = sym _keyAliases[alias] = sym
end end
return nothing
end end
function add_non_underscore_aliases!(aliases::Dict{Symbol,Symbol}) function add_non_underscore_aliases!(aliases::Dict{Symbol,Symbol})
for (k,v) in aliases for (k,v) in aliases
s = string(k) s = string(k)
if '_' in s if '_' in s
aliases[Symbol(replace(s, "_" => ""))] = v aliases[make_non_underscore(k)] = v
end end
end end
end end
@ -24,7 +37,7 @@ function add_non_underscore_aliases!(aliases::Dict{Symbol,Symbol}, args::Vector{
for arg in args for arg in args
s = string(arg) s = string(arg)
if '_' in s if '_' in s
aliases[Symbol(replace(s, "_" => ""))] = arg aliases[make_non_underscore(arg)] = arg
end end
end end
end end
@ -111,6 +124,31 @@ const _styleAliases = Dict{Symbol,Symbol}(
:ddd => :dashdotdot, :ddd => :dashdotdot,
) )
const _shape_keys = Symbol[
:circle,
:rect,
:star5,
:diamond,
:hexagon,
:cross,
:xcross,
:utriangle,
:dtriangle,
:rtriangle,
:ltriangle,
:pentagon,
:heptagon,
:octagon,
:star4,
:star6,
:star7,
:star8,
:vline,
:hline,
:+,
:x,
]
const _allMarkers = vcat(:none, :auto, _shape_keys) #sort(collect(keys(_shapes)))) const _allMarkers = vcat(:none, :auto, _shape_keys) #sort(collect(keys(_shapes))))
const _markerAliases = Dict{Symbol,Symbol}( const _markerAliases = Dict{Symbol,Symbol}(
:n => :none, :n => :none,
@ -491,7 +529,7 @@ const _all_series_args = sort(union([_series_args; _magic_series_args]))
const _all_plot_args = _plot_args const _all_plot_args = _plot_args
const _all_args = const _all_args =
sort([_all_axis_args; _all_subplot_args; _all_series_args; _all_plot_args]) sort(union([_all_axis_args; _all_subplot_args; _all_series_args; _all_plot_args]))
is_subplot_attr(k) = k in _all_subplot_args is_subplot_attr(k) = k in _all_subplot_args
is_series_attr(k) = k in _all_series_args is_series_attr(k) = k in _all_series_args
@ -502,9 +540,6 @@ RecipesBase.is_key_supported(k::Symbol) = is_attr_supported(k)
is_default_attribute(k) = k in _internal_args || k in _all_args || is_axis_attr_noletter(k) is_default_attribute(k) = k in _internal_args || k in _all_args || is_axis_attr_noletter(k)
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
makeplural(s::Symbol) = Symbol(string(s,"s"))
autopick_ignore_none_auto(arr::AVec, idx::Integer) = _cycle(setdiff(arr, [:none, :auto]), idx) autopick_ignore_none_auto(arr::AVec, idx::Integer) = _cycle(setdiff(arr, [:none, :auto]), idx)
autopick_ignore_none_auto(notarr, idx::Integer) = notarr autopick_ignore_none_auto(notarr, idx::Integer) = notarr
@ -646,10 +681,10 @@ add_aliases(:contour_labels, :contourlabels, :clabels, :clabs)
add_aliases(:warn_on_unsupported, :warn) add_aliases(:warn_on_unsupported, :warn)
# add all pluralized forms to the _keyAliases dict # add all pluralized forms to the _keyAliases dict
for arg in keys(_series_defaults) for arg in _all_args
_keyAliases[makeplural(arg)] = arg add_aliases(arg, makeplural(arg))
end end
# add all non_underscored forms to the _keyAliases
add_non_underscore_aliases!(_keyAliases) add_non_underscore_aliases!(_keyAliases)
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -1697,3 +1732,80 @@ function _series_index(plotattributes, sp)
end end
return idx return idx
end end
#--------------------------------------------------
## inspired by Base.@kwdef
macro add_attributes( level, expr )
expr = macroexpand(__module__, expr) # to expand @static
expr isa Expr && expr.head === :struct || error("Invalid usage of @add_attributes")
T = expr.args[2]
if T isa Expr && T.head === :<:
T = T.args[1]
end
key_args = Any[]
value_args = Any[]
_splitdef!(expr.args[3], value_args, key_args)
insert_block = Expr(:block)
for (key, value) in zip(key_args, value_args)
# e.g. _series_defualts[key] = value
exp_key = Symbol(lowercase(string(T)), "_", key)
pl_key = makeplural(exp_key)
push!(insert_block.args, Expr(
:(=), Expr(:ref, Symbol("_", level, "_defaults"), QuoteNode(exp_key)), value
))
push!(insert_block.args, :(
add_aliases($(QuoteNode(exp_key)), $(QuoteNode(pl_key)))
))
push!(insert_block.args, :(
add_aliases($(QuoteNode(exp_key)), $(QuoteNode(make_non_underscore(exp_key))))
))
push!(insert_block.args, :(
add_aliases($(QuoteNode(exp_key)), $(QuoteNode(make_non_underscore(pl_key))))
))
end
return quote
$expr
$insert_block
end |> esc
end
function _splitdef!(blk, value_args, key_args)
for i in eachindex(blk.args)
ei = blk.args[i]
if ei isa Symbol
# var
continue
elseif ei isa Expr
if ei.head === :(=)
lhs = ei.args[1]
if lhs isa Symbol
# var = defexpr
var = lhs
elseif lhs isa Expr && lhs.head === :(::) && lhs.args[1] isa Symbol
# var::T = defexpr
var = lhs.args[1]
else
# something else, e.g. inline inner constructor
# F(...) = ...
continue
end
defexpr = ei.args[2] # defexpr
push!(value_args, defexpr)
push!(key_args, var)
blk.args[i] = lhs
elseif ei.head === :(::) && ei.args[1] isa Symbol
# var::Typ
var = ei.args[1]
push!(value_args, var)
push!(key_args, var)
elseif ei.head === :block
# can arise with use of @static inside type decl
_kwdef!(ei, value_args, key_args)
end
end
end
blk
end

View File

@ -1,5 +1,3 @@
const P2 = GeometryTypes.Point2{Float64} const P2 = GeometryTypes.Point2{Float64}
const P3 = GeometryTypes.Point3{Float64} const P3 = GeometryTypes.Point3{Float64}
@ -77,7 +75,6 @@ function weave(x,y; ordering = Vector[x,y])
ret ret
end end
"create a star by weaving together points from an outer and inner circle. `n` is the number of arms" "create a star by weaving together points from an outer and inner circle. `n` is the number of arms"
function makestar(n; offset = -0.5, radius = 1.0) function makestar(n; offset = -0.5, radius = 1.0)
z1 = offset * π z1 = offset * π
@ -93,7 +90,6 @@ function makeshape(n; offset = -0.5, radius = 1.0)
Shape(partialcircle(z, z + 2π, n+1, radius)) Shape(partialcircle(z, z + 2π, n+1, radius))
end end
function makecross(; offset = -0.5, radius = 1.0) function makecross(; offset = -0.5, radius = 1.0)
z2 = offset * π z2 = offset * π
z1 = z2 - π/8 z1 = z2 - π/8
@ -103,7 +99,6 @@ function makecross(; offset = -0.5, radius = 1.0)
ordering=Vector[outercircle,innercircle,outercircle])) ordering=Vector[outercircle,innercircle,outercircle]))
end end
from_polar(angle, dist) = P2(dist*cos(angle), dist*sin(angle)) from_polar(angle, dist) = P2(dist*cos(angle), dist*sin(angle))
function makearrowhead(angle; h = 2.0, w = 0.4) function makearrowhead(angle; h = 2.0, w = 0.4)
@ -112,31 +107,6 @@ function makearrowhead(angle; h = 2.0, w = 0.4)
from_polar(angle + 0.5π, w) - tip, (0,0)]) from_polar(angle + 0.5π, w) - tip, (0,0)])
end end
const _shape_keys = Symbol[
:circle,
:rect,
:star5,
:diamond,
:hexagon,
:cross,
:xcross,
:utriangle,
:dtriangle,
:rtriangle,
:ltriangle,
:pentagon,
:heptagon,
:octagon,
:star4,
:star6,
:star7,
:star8,
:vline,
:hline,
:+,
:x,
]
const _shapes = KW( const _shapes = KW(
:circle => makeshape(20), :circle => makeshape(20),
:rect => makeshape(4, offset=-0.25), :rect => makeshape(4, offset=-0.25),