Skip to content

Commit

Permalink
Make JuliaFormatter ignore bind macro in notebook file (#3072)
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd authored Oct 29, 2024
1 parent 68ee31f commit e630067
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/notebook/saving and loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Have a look at our [JuliaCon 2020 presentation](https://youtu.be/IAF8DjrQSSk?t=1
function save_notebook(io::IO, notebook::Notebook)
println(io, _notebook_header)
println(io, "# ", PLUTO_VERSION_STR)

# Notebook metadata
let nb_metadata_toml = strip(sprint(TOML.print, get_metadata_no_default(notebook)))
if !isempty(nb_metadata_toml)
Expand All @@ -56,7 +56,7 @@ function save_notebook(io::IO, notebook::Notebook)
println(io)

cells_ordered = collect(topological_order(notebook))

# NOTE: the notebook topological is cached on every update_dependency! call
# .... so it is possible that a cell was added/removed since this last update.
# .... in this case, it will not contain that cell since it is build from its
Expand Down Expand Up @@ -91,13 +91,13 @@ function save_notebook(io::IO, notebook::Notebook)
end
end


using_plutopkg = notebook.nbpkg_ctx !== nothing

write_package = if using_plutopkg
ptoml_contents = PkgCompat.read_project_file(notebook)
mtoml_contents = PkgCompat.read_manifest_file(notebook)

!isempty(strip(ptoml_contents))
else
false
Expand All @@ -109,14 +109,14 @@ function save_notebook(io::IO, notebook::Notebook)
write(io, ptoml_contents)
print(io, "\"\"\"")
print(io, _cell_suffix)

println(io, _cell_id_delimiter, string(_mtoml_cell_id))
print(io, "PLUTO_MANIFEST_TOML_CONTENTS = \"\"\"\n")
write(io, mtoml_contents)
print(io, "\"\"\"")
print(io, _cell_suffix)
end


println(io, _cell_id_delimiter, "Cell order:")
for c in notebook.cells
Expand All @@ -137,7 +137,7 @@ function write_buffered(fn::Function, path)
file_content = sprint(fn)
write(path, file_content)
end

function save_notebook(notebook::Notebook, path::String)
# @warn "Saving to file!!" exception=(ErrorException(""), backtrace())
notebook.last_save_time = time()
Expand Down
13 changes: 8 additions & 5 deletions src/runner/PlutoRunner/src/bonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function transform_bond_value(s::Symbol, value_from_js)
catch e
@error "🚨 AbstractPlutoDingetjes: Bond value transformation errored." exception=(e, catch_backtrace())
(;
message=Text("🚨 AbstractPlutoDingetjes: Bond value transformation errored."),
message=Text("🚨 AbstractPlutoDingetjes: Bond value transformation errored."),
exception=Text(
sprint(showerror, e, stacktrace(catch_backtrace()))
),
Expand All @@ -35,16 +35,16 @@ function possible_bond_values(s::Symbol; get_length::Bool=false)
:InfinitePossibilities
elseif (possible_values isa AbstractPlutoDingetjes.Bonds.NotGiven)
# error("Bond \"$s\" did not specify its possible values with `AbstractPlutoDingetjes.Bond.possible_values()`. Try using PlutoUI for the `@bind` values.")

# If you change this, change it everywhere in this file.
:NotGiven
else
get_length ?
get_length ?
try
length(possible_values)
catch
length(make_serializable(possible_values))
end :
end :
make_serializable(possible_values)
end
end
Expand Down Expand Up @@ -118,7 +118,7 @@ x^2
The first cell will show a slider as the cell's output, ranging from 0 until 100.
The second cell will show the square of `x`, and is updated in real-time as the slider is moved.
"""
macro bind(def, element)
macro bind(def, element)
if def isa Symbol
quote
$(load_integrations_if_needed)()
Expand All @@ -133,13 +133,16 @@ end

"""
Will be inserted in saved notebooks that use the @bind macro, make sure that they still contain legal syntax when executed as a vanilla Julia script. Overloading `Base.get` for custom UI objects gives bound variables a sensible value.
Also turns off JuliaFormatter formatting to avoid issues with the formatter trying to change code that the user does not control. See https://domluna.github.io/JuliaFormatter.jl/stable/#Turn-off/on-formatting
"""
const fake_bind = """macro bind(def, element)
#! format: off
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = \$(esc(element))
global \$(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
#! format: on
end"""

0 comments on commit e630067

Please sign in to comment.