From 0308b477305c1040d76722a879204351c0e4a3ba Mon Sep 17 00:00:00 2001 From: davidanthoff Date: Thu, 18 May 2023 20:31:04 +0000 Subject: [PATCH] Format files using DocumentFormat --- docs/make.jl | 46 +++--- src/Vega.jl | 2 +- .../dsl_vlplot_function.jl | 6 +- src/dsl_vlplot_macro/dsl_vlplot_macro.jl | 10 +- src/rendering/io.jl | 4 +- src/rendering/render.jl | 153 +++++++++--------- src/rendering/show.jl | 4 +- src/rendering/to_julia.jl | 48 +++--- src/spec_utils.jl | 6 +- test/test_io.jl | 4 +- test/test_macro.jl | 60 +++---- test/test_properties.jl | 2 +- test/test_vg.jl | 4 +- 13 files changed, 178 insertions(+), 171 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index 510496b..7fde57c 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -9,30 +9,28 @@ function Base.show(io::IO, m::MIME"text/html", v::Vega.VGSpec) end makedocs( - modules=[Vega], - sitename="Vega.jl", - pages=[ - "Home" => "index.md", - "Getting Started" => Any[ - "Installation" => "gettingstarted/installation.md" - ], - "User Guide" => Any[ - "Using Vega" => "userguide/vega.md" - ], - "Examples" => Any[ - - "Line & Area Charts" => "examples/examples_vega_line_area_charts.md", - "Circular Charts" => "examples/examples_vega_circular_charts.md", - "Scatter Plots" => "examples/examples_vega_scatter_plots.md", - "Tree Diagrams" => "examples/examples_vega_tree_diagrams.md", - "Network Diagrams" => "examples/examples_vega_network_diagrams.md", - "Single Data Source" => "examples/examples_vega_single_data_source.md", - "Multiple Data Source" => "examples/examples_vega_multi_data_source.md" - ], - "Reference Manual" => [ - "Global settings" => "referencemanual/global.md", - "Outputs" => "referencemanual/output.md"] - ] + modules=[Vega], + sitename="Vega.jl", + pages=[ + "Home" => "index.md", + "Getting Started" => Any[ + "Installation"=>"gettingstarted/installation.md" + ], + "User Guide" => Any[ + "Using Vega"=>"userguide/vega.md" + ], + "Examples" => Any["Line & Area Charts"=>"examples/examples_vega_line_area_charts.md", + "Circular Charts"=>"examples/examples_vega_circular_charts.md", + "Scatter Plots"=>"examples/examples_vega_scatter_plots.md", + "Tree Diagrams"=>"examples/examples_vega_tree_diagrams.md", + "Network Diagrams"=>"examples/examples_vega_network_diagrams.md", + "Single Data Source"=>"examples/examples_vega_single_data_source.md", + "Multiple Data Source"=>"examples/examples_vega_multi_data_source.md" + ], + "Reference Manual" => [ + "Global settings" => "referencemanual/global.md", + "Outputs" => "referencemanual/output.md"] + ] ) deploydocs( diff --git a/src/Vega.jl b/src/Vega.jl index 4e99f77..2cba87f 100644 --- a/src/Vega.jl +++ b/src/Vega.jl @@ -68,7 +68,7 @@ show if plots will have (true) or not (false) the action links displayed indicate if actions links should be dislpayed under the plot """ actionlinks() = ACTIONSLINKS -actionlinks(b::Bool) = (global ACTIONSLINKS ; ACTIONSLINKS = b) +actionlinks(b::Bool) = (global ACTIONSLINKS; ACTIONSLINKS = b) ######################## includes ##################################### diff --git a/src/dsl_vlplot_function/dsl_vlplot_function.jl b/src/dsl_vlplot_function/dsl_vlplot_function.jl index 7dc45b3..36b7603 100644 --- a/src/dsl_vlplot_function/dsl_vlplot_function.jl +++ b/src/dsl_vlplot_function/dsl_vlplot_function.jl @@ -19,7 +19,7 @@ function convert_nt_to_dict(item::AbstractVegaFragment, fragtype) return fragtype(item.positional, OrderedDict{String,Any}(string(k) => convert_nt_to_dict(v, fragtype) for (k, v) in pairs(item.named))) end -function walk_dict(f, d::T, parent) where {T <: AbstractDict} +function walk_dict(f, d::T, parent) where {T<:AbstractDict} res = T() for p in d if p[2] isa Dict @@ -108,6 +108,6 @@ function convert_frag_tree_to_dict(spec::VGFrag) return OrderedDict{String,Any}(k => replace_remaining_frag(v) for (k, v) in spec.named) end -function vgplot(args...;kwargs...) - return VGSpec(convert_frag_tree_to_dict(fix_shortcut_level_spec(vgfrag(args...;kwargs...)))) +function vgplot(args...; kwargs...) + return VGSpec(convert_frag_tree_to_dict(fix_shortcut_level_spec(vgfrag(args...; kwargs...)))) end diff --git a/src/dsl_vlplot_macro/dsl_vlplot_macro.jl b/src/dsl_vlplot_macro/dsl_vlplot_macro.jl index 27f12d3..719396d 100644 --- a/src/dsl_vlplot_macro/dsl_vlplot_macro.jl +++ b/src/dsl_vlplot_macro/dsl_vlplot_macro.jl @@ -3,7 +3,7 @@ function convert_curly_style_array(exprs, fragtype) for ex in exprs if ex isa Expr && ex.head == :braces - push!(res.args, :( $(convert_curly_style(ex.args, fragtype)) )) + push!(res.args, :($(convert_curly_style(ex.args, fragtype)))) else push!(res.args, esc(ex)) end @@ -21,11 +21,11 @@ function convert_curly_style(exprs, fragtype) for ex in exprs if ex isa Expr && ex.head == :(=) if ex.args[2] isa Expr && ex.args[2].head == :braces - push!(named_args, :( $(string(ex.args[1])) => $(convert_curly_style(ex.args[2].args, fragtype)) )) + push!(named_args, :($(string(ex.args[1])) => $(convert_curly_style(ex.args[2].args, fragtype)))) elseif ex.args[2] isa Expr && ex.args[2].head == :vect - push!(named_args, :( $(string(ex.args[1])) => $(convert_curly_style_array(ex.args[2].args, fragtype)) )) + push!(named_args, :($(string(ex.args[1])) => $(convert_curly_style_array(ex.args[2].args, fragtype)))) else - push!(named_args, :( $(string(ex.args[1])) => $(esc(ex.args[2])) )) + push!(named_args, :($(string(ex.args[1])) => $(esc(ex.args[2])))) end elseif ex isa Expr && ex.head == :braces push!(pos_args, convert_curly_style(ex.args, fragtype)) @@ -40,7 +40,7 @@ end macro vgplot(ex...) new_ex = convert_curly_style(ex, VGFrag) - return :( Vega.VGSpec(convert_frag_tree_to_dict(fix_shortcut_level_spec($new_ex))) ) + return :(Vega.VGSpec(convert_frag_tree_to_dict(fix_shortcut_level_spec($new_ex)))) end macro vgfrag(ex...) diff --git a/src/rendering/io.jl b/src/rendering/io.jl index 10b3cb7..2ae688c 100644 --- a/src/rendering/io.jl +++ b/src/rendering/io.jl @@ -40,5 +40,5 @@ end savespec(filename::AbstractString, v::AbstractVegaSpec; kwargs...) = open(filename, "w") do io - savespec(io, v; kwargs...) -end + savespec(io, v; kwargs...) + end diff --git a/src/rendering/render.jl b/src/rendering/render.jl index 12e169b..612f368 100644 --- a/src/rendering/render.jl +++ b/src/rendering/render.jl @@ -7,14 +7,14 @@ using JSON asset(url...) = normpath(vegalite_app_path("minified", url...)) -const package_json = Ref{Dict{String, Any}}() +const package_json = Ref{Dict{String,Any}}() function version(package) - if !isassigned(package_json) - package_json[] = JSON.parsefile(vegalite_app_path("package.json")) - end + if !isassigned(package_json) + package_json[] = JSON.parsefile(vegalite_app_path("package.json")) + end - return package_json[]["dependencies"][package] + return package_json[]["dependencies"][package] end # Vega Scaffold: https://github.com/vega/vega/wiki/Runtime @@ -23,47 +23,50 @@ function writehtml_full(io::IO, spec::VGSpec; title="Vega plot") divid = "vg" * randstring(3) println(io, - """ - - - $title - - - - - -
- - - - - + + + +
+ + + + + + - - """) + +""" + ) end function writehtml_full(spec::VGSpec; title="Vega plot") @@ -81,35 +84,41 @@ Creates a HTML script + div block for showing the plot (typically for Pluto). VegaLite js files are loaded from the web using script tags. """ function writehtml_partial_script(io::IO, spec::VGSpec; title="VegaLite plot") - divid = "vg" * randstring(3) - print(io, """ - - - - - -
- - - """) + divid = "vg" * randstring(3) + print( + io, + """ + + + + + +
+ + +""" + ) end @@ -120,9 +129,9 @@ function launch_browser(tmppath::String) if Sys.isapple() run(`open $tmppath`) elseif Sys.iswindows() - run(`cmd /c start $tmppath`) - elseif Sys.islinux() - run(`xdg-open $tmppath`) + run(`cmd /c start $tmppath`) + elseif Sys.islinux() + run(`xdg-open $tmppath`) end end diff --git a/src/rendering/show.jl b/src/rendering/show.jl index bbd5ccf..ec9a195 100644 --- a/src/rendering/show.jl +++ b/src/rendering/show.jl @@ -28,9 +28,9 @@ function convert_vg_to_x(v::VGSpec, fileformat; cmd_args="") reader = @async read(p, String) wait(writer) - wait(p) + wait(p) res = fetch(reader) - + if p.exitcode != 0 throw(ArgumentError("Invalid spec")) end diff --git a/src/rendering/to_julia.jl b/src/rendering/to_julia.jl index 9e52be7..5c8a5da 100644 --- a/src/rendering/to_julia.jl +++ b/src/rendering/to_julia.jl @@ -2,11 +2,11 @@ function print_datavaluesnode_as_julia(io, it, col_names) print(io, "[") for (row_index, row) in enumerate(it) - row_index==1 || print(io, ",") + row_index == 1 || print(io, ",") print(io, "{") for (col_index, col_value) in enumerate(row) - col_index==1 || print(io, ",") + col_index == 1 || print(io, ",") print(io, col_names[col_index]) print(io, "=") print(io, col_value isa DataValue ? get(col_value, nothing) : col_value) @@ -22,66 +22,66 @@ function print_vspec_as_julia(io::IO, d::DataValuesNode, indent, indent_level, n col_names = collect(keys(d.columns)) it = TableTraitsUtils.create_tableiterator(collect(values(d.columns)), col_names) - if include_data==:short + if include_data == :short print(io, "...") else print_datavaluesnode_as_julia(io, it, col_names) end end -function print_vspec_as_julia(io::IO, vect::AbstractVector, indent, indent_level, newlines, include_data) - for (i,v) in enumerate(vect) - i>1 && print(io, ",", newlines ? "\n" : "") +function print_vspec_as_julia(io::IO, vect::AbstractVector, indent, indent_level, newlines, include_data) + for (i, v) in enumerate(vect) + i > 1 && print(io, ",", newlines ? "\n" : "") if v isa AbstractDict - print(io, " "^(indent*indent_level), "{") + print(io, " "^(indent * indent_level), "{") newlines && println(io) - print_vspec_as_julia(io, v, indent, indent_level+1, newlines, include_data) + print_vspec_as_julia(io, v, indent, indent_level + 1, newlines, include_data) newlines && println(io) - print(io, " "^(indent*indent_level), "}") + print(io, " "^(indent * indent_level), "}") elseif v isa AbstractVector print(io, "[") newlines && println(io) - print_vspec_as_julia(io, v, indent, indent_level+1, newlines, include_data) + print_vspec_as_julia(io, v, indent, indent_level + 1, newlines, include_data) newlines && println(io) - print(io, " "^(indent*indent_level), "]") + print(io, " "^(indent * indent_level), "]") elseif v isa DataValuesNode print_vspec_as_julia(io, v, indent, indent_level, newlines, include_data) elseif v isa AbstractString || v isa Symbol - print(io, " "^(indent*indent_level)) + print(io, " "^(indent * indent_level)) print(io, "\"") print(io, string(v)) print(io, "\"") else - print(io, " "^(indent*indent_level)) + print(io, " "^(indent * indent_level)) print(io, v) end end end function print_vspec_as_julia(io::IO, dict::AbstractDict, indent, indent_level, newlines, include_data) - dict = (include_data==:short || include_data) ? dict : OrderedDict{String,Any}(i for i in dict if i[1]!="data") - for (i,(k,v)) in enumerate(dict) - i>1 && print(io, ",", newlines ? "\n" : "") - print(io, " "^(indent*indent_level), k) + dict = (include_data == :short || include_data) ? dict : OrderedDict{String,Any}(i for i in dict if i[1] != "data") + for (i, (k, v)) in enumerate(dict) + i > 1 && print(io, ",", newlines ? "\n" : "") + print(io, " "^(indent * indent_level), k) print(io, "=") if v isa AbstractDict print(io, "{") newlines && println(io) - print_vspec_as_julia(io, v, indent, indent_level+1, newlines, include_data) + print_vspec_as_julia(io, v, indent, indent_level + 1, newlines, include_data) newlines && println(io) - print(io, " "^(indent*indent_level), "}") + print(io, " "^(indent * indent_level), "}") elseif v isa AbstractVector print(io, "[") newlines && println(io) - print_vspec_as_julia(io, v, indent, indent_level+1, newlines, include_data) + print_vspec_as_julia(io, v, indent, indent_level + 1, newlines, include_data) newlines && println(io) - print(io, " "^(indent*indent_level), "]") + print(io, " "^(indent * indent_level), "]") elseif v isa AbstractString || v isa Symbol print(io, "\"") print(io, string(v)) print(io, "\"") elseif v isa DataValuesNode - print_vspec_as_julia(io, v, indent, indent_level+1, newlines, include_data) + print_vspec_as_julia(io, v, indent, indent_level + 1, newlines, include_data) else print(io, v) end @@ -100,8 +100,8 @@ in the output. `indent` controls whether the code is printed in one line function printrep end function printrepr(io::IO, v::VGSpec; indent=nothing, include_data=false) - newlines = indent===nothing ? false : true - indent = indent===nothing ? 0 : indent + newlines = indent === nothing ? false : true + indent = indent === nothing ? 0 : indent print(io, "@vgplot(") newlines && println(io) diff --git a/src/spec_utils.jl b/src/spec_utils.jl index 5188fe2..53735f5 100644 --- a/src/spec_utils.jl +++ b/src/spec_utils.jl @@ -4,7 +4,7 @@ end abstract type AbstractVegaSpec end -Base.copy(spec::T) where {T <: AbstractVegaSpec} = T(copy(getparams(spec))) +Base.copy(spec::T) where {T<:AbstractVegaSpec} = T(copy(getparams(spec))) const ObjectLike = Union{AbstractVegaSpec,ObjectProxy} @@ -51,11 +51,11 @@ struct DataValuesNode function DataValuesNode(it) col_values, col_names = TableTraitsUtils.create_columns_from_iterabletable(it) - + return new(OrderedDict{Symbol,AbstractVector}(i[1] => i[2] for i in zip(col_names, col_values))) end end - + Base.:(==)(a::DataValuesNode, b::DataValuesNode) = a.columns == b.columns function our_show_json(io, it, col_names) diff --git a/test/test_io.jl b/test/test_io.jl index 5ad3875..3895f88 100644 --- a/test/test_io.jl +++ b/test/test_io.jl @@ -1,11 +1,11 @@ @testitem "io" begin - using Vega:getparams + using Vega: getparams using DataFrames using Dates using FileIO include("testhelper_create_vg_plot.jl") - + vgp = getvgplot() # @testset "$fmt (indent=$(repr(indent)))" for (fmt, plt) in [ diff --git a/test/test_macro.jl b/test/test_macro.jl index c95c488..8ded6ed 100644 --- a/test/test_macro.jl +++ b/test/test_macro.jl @@ -1,37 +1,37 @@ @testitem "macro" begin - using Vega:getparams - using JSON - - spec = vg""" - { - "data": { - "values": [ - {"a": "A","b": 28}, {"a": "B","b": 55} - ] - }, - "mark": "bar", - "encoding": { - "x": {"field": "a", "type": "ordinal"}, - "y": {"field": "b", "type": "quantitative"} + using Vega: getparams + using JSON + + spec = vg""" + { + "data": { + "values": [ + {"a": "A","b": 28}, {"a": "B","b": 55} + ] + }, + "mark": "bar", + "encoding": { + "x": {"field": "a", "type": "ordinal"}, + "y": {"field": "b", "type": "quantitative"} + } } - } - """ + """ - @test isa(spec, Vega.VGSpec) - @test getparams(spec) == JSON.parse(""" - { - "data": { - "values": [ - {"a": "A","b": 28}, {"a": "B","b": 55} - ] - }, - "mark": "bar", - "encoding": { - "x": {"field": "a", "type": "ordinal"}, - "y": {"field": "b", "type": "quantitative"} + @test isa(spec, Vega.VGSpec) + @test getparams(spec) == JSON.parse(""" + { + "data": { + "values": [ + {"a": "A","b": 28}, {"a": "B","b": 55} + ] + }, + "mark": "bar", + "encoding": { + "x": {"field": "a", "type": "ordinal"}, + "y": {"field": "b", "type": "quantitative"} + } } - } - """) + """) end diff --git a/test/test_properties.jl b/test/test_properties.jl index 4f87e54..4a57b93 100644 --- a/test/test_properties.jl +++ b/test/test_properties.jl @@ -2,7 +2,7 @@ using Setfield include("testhelper_create_vg_plot.jl") - + vgp = getvgplot() @test vgp.width isa Number @static if VERSION >= v"1.3" diff --git a/test/test_vg.jl b/test/test_vg.jl index 5b4436b..7bde8e8 100644 --- a/test/test_vg.jl +++ b/test/test_vg.jl @@ -5,7 +5,7 @@ using VegaDatasets using Dates using Vega: getparams - + include("testhelper_create_vg_plot.jl") @test vg"""{ "data": [ { "name": "test" } ] }"""(URI("http://www.foo.com/bar.json"), "test") == vg""" @@ -37,7 +37,7 @@ """ end - df = DataFrame(a=[1.,2.], b=["A", "B"], c=[Date(2000), Date(2001)]) + df = DataFrame(a=[1., 2.], b=["A", "B"], c=[Date(2000), Date(2001)]) p1 = getvgplot()