Skip to content

Commit

Permalink
New API: Pluto.is_single_expression (#3134)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Jan 17, 2025
1 parent 550f7be commit ae3dfc3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/analysis/Parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ end
preprocess_expr(val::Any) = val


"""
Does this `String` contain a single expression? If this function returns `false`, then Pluto will show a "multiple expressions in one cell" error in the editor.
!!! compat "Pluto 0.20.5"
This function is new in Pluto 0.20.5.
"""
function is_single_expression(s::String)
n = Pluto.Notebook([Pluto.Cell(s)])
e = parse_custom(n, n.cells[1])
bad = Meta.isexpr(e, :toplevel, 2) && Meta.isexpr(e.args[2], :call, 2) && e.args[2].args[1] == :(PlutoRunner.throw_syntax_error) && e.args[2].args[2] isa String && startswith(e.args[2].args[2], "extra token after end of expression")


return !bad
end



function updated_topology(old_topology::NotebookTopology{Cell}, notebook::Notebook, updated_cells)
get_code_str(cell::Cell) = cell.code
get_code_expr(cell::Cell) = parse_custom(notebook, cell)
Expand Down
43 changes: 43 additions & 0 deletions test/misc API.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@testset "Misc API" begin


@testset "is_single_expression" begin

@test Pluto.is_single_expression("")
@test Pluto.is_single_expression("a")
@test Pluto.is_single_expression("a + 1")
@test Pluto.is_single_expression("a; a + 1")
@test !Pluto.is_single_expression("""
a = 1
a + 1
""")

@test Pluto.is_single_expression("""
"yooo"
function f(x)
X
C \\ c
end
""")


@test Pluto.is_single_expression("""
# asdf
"yooo"
function f(x)
X
C \\ c
end; # aasasdf
""")



@test Pluto.is_single_expression("""
a a a a a // / // / 123 1 21 1313
""")



end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ verify_no_running_processes()
@timeit_include("DependencyCache.jl")
@timeit_include("Throttled.jl")
@timeit_include("cell_disabling.jl")
@timeit_include("misc API.jl")

verify_no_running_processes()

Expand Down

0 comments on commit ae3dfc3

Please sign in to comment.