Skip to content

Commit e630067

Browse files
Make JuliaFormatter ignore bind macro in notebook file (#3072)
1 parent 68ee31f commit e630067

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/notebook/saving and loading.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Have a look at our [JuliaCon 2020 presentation](https://youtu.be/IAF8DjrQSSk?t=1
3232
function save_notebook(io::IO, notebook::Notebook)
3333
println(io, _notebook_header)
3434
println(io, "# ", PLUTO_VERSION_STR)
35-
35+
3636
# Notebook metadata
3737
let nb_metadata_toml = strip(sprint(TOML.print, get_metadata_no_default(notebook)))
3838
if !isempty(nb_metadata_toml)
@@ -56,7 +56,7 @@ function save_notebook(io::IO, notebook::Notebook)
5656
println(io)
5757

5858
cells_ordered = collect(topological_order(notebook))
59-
59+
6060
# NOTE: the notebook topological is cached on every update_dependency! call
6161
# .... so it is possible that a cell was added/removed since this last update.
6262
# .... in this case, it will not contain that cell since it is build from its
@@ -91,13 +91,13 @@ function save_notebook(io::IO, notebook::Notebook)
9191
end
9292
end
9393

94-
94+
9595
using_plutopkg = notebook.nbpkg_ctx !== nothing
96-
96+
9797
write_package = if using_plutopkg
9898
ptoml_contents = PkgCompat.read_project_file(notebook)
9999
mtoml_contents = PkgCompat.read_manifest_file(notebook)
100-
100+
101101
!isempty(strip(ptoml_contents))
102102
else
103103
false
@@ -109,14 +109,14 @@ function save_notebook(io::IO, notebook::Notebook)
109109
write(io, ptoml_contents)
110110
print(io, "\"\"\"")
111111
print(io, _cell_suffix)
112-
112+
113113
println(io, _cell_id_delimiter, string(_mtoml_cell_id))
114114
print(io, "PLUTO_MANIFEST_TOML_CONTENTS = \"\"\"\n")
115115
write(io, mtoml_contents)
116116
print(io, "\"\"\"")
117117
print(io, _cell_suffix)
118118
end
119-
119+
120120

121121
println(io, _cell_id_delimiter, "Cell order:")
122122
for c in notebook.cells
@@ -137,7 +137,7 @@ function write_buffered(fn::Function, path)
137137
file_content = sprint(fn)
138138
write(path, file_content)
139139
end
140-
140+
141141
function save_notebook(notebook::Notebook, path::String)
142142
# @warn "Saving to file!!" exception=(ErrorException(""), backtrace())
143143
notebook.last_save_time = time()

src/runner/PlutoRunner/src/bonds.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function transform_bond_value(s::Symbol, value_from_js)
1010
catch e
1111
@error "🚨 AbstractPlutoDingetjes: Bond value transformation errored." exception=(e, catch_backtrace())
1212
(;
13-
message=Text("🚨 AbstractPlutoDingetjes: Bond value transformation errored."),
13+
message=Text("🚨 AbstractPlutoDingetjes: Bond value transformation errored."),
1414
exception=Text(
1515
sprint(showerror, e, stacktrace(catch_backtrace()))
1616
),
@@ -35,16 +35,16 @@ function possible_bond_values(s::Symbol; get_length::Bool=false)
3535
:InfinitePossibilities
3636
elseif (possible_values isa AbstractPlutoDingetjes.Bonds.NotGiven)
3737
# error("Bond \"$s\" did not specify its possible values with `AbstractPlutoDingetjes.Bond.possible_values()`. Try using PlutoUI for the `@bind` values.")
38-
38+
3939
# If you change this, change it everywhere in this file.
4040
:NotGiven
4141
else
42-
get_length ?
42+
get_length ?
4343
try
4444
length(possible_values)
4545
catch
4646
length(make_serializable(possible_values))
47-
end :
47+
end :
4848
make_serializable(possible_values)
4949
end
5050
end
@@ -118,7 +118,7 @@ x^2
118118
The first cell will show a slider as the cell's output, ranging from 0 until 100.
119119
The second cell will show the square of `x`, and is updated in real-time as the slider is moved.
120120
"""
121-
macro bind(def, element)
121+
macro bind(def, element)
122122
if def isa Symbol
123123
quote
124124
$(load_integrations_if_needed)()
@@ -133,13 +133,16 @@ end
133133

134134
"""
135135
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.
136+
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
136137
"""
137138
const fake_bind = """macro bind(def, element)
139+
#! format: off
138140
quote
139141
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
140142
local el = \$(esc(element))
141143
global \$(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
142144
el
143145
end
146+
#! format: on
144147
end"""
145148

0 commit comments

Comments
 (0)