From fad9301ad468fa6bc89718781a97cd7f0ff973b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 23 Sep 2021 13:20:49 +0200 Subject: [PATCH] Fix branching section evaluation when the parent section is empty (#560) * Fix branching section evaluation when the parent section is empty * Update changelog --- CHANGELOG.md | 3 ++ lib/livebook/session.ex | 10 +---- test/livebook/intellisense_test.exs | 1 + test/livebook/session_test.exs | 65 +++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b304b790..729c3575d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Improved intellisense to handle structs and sigils ([#513](https://github.com/livebook-dev/livebook/pull/513)) - Create new notebooks with an already focused code cell ([#527](https://github.com/livebook-dev/livebook/pull/527)) +- Switched base Docker image from alpine to debian-slim ([#552](https://github.com/livebook-dev/livebook/pull/552)) ### Fixed - Improved Markdown and math integration by migrating to remark ([#495](https://github.com/livebook-dev/livebook/pull/495)) - Improved the evaluator process to not consume user-submitted messages from inbox ([#502](https://github.com/livebook-dev/livebook/pull/502)) +- Improved sections panel UI to better handle numerous sections or long section names ([#534](https://github.com/livebook-dev/livebook/pull/534) and [#537](https://github.com/livebook-dev/livebook/pull/537)) +- Fixed branching section evaluation when the parent section is empty ([#560](https://github.com/livebook-dev/livebook/pull/560) ## [v0.2.3](https://github.com/livebook-dev/livebook/tree/v0.2.3) (2021-08-12) diff --git a/lib/livebook/session.ex b/lib/livebook/session.ex index d5dd76eb3..264a53bfa 100644 --- a/lib/livebook/session.ex +++ b/lib/livebook/session.ex @@ -1000,15 +1000,7 @@ defmodule Livebook.Session do """ @spec find_prev_locator(Notebook.t(), Cell.t(), Section.t()) :: Runtime.locator() def find_prev_locator(notebook, cell, section) do - default = - case section.parent_id do - nil -> - {container_ref_for_section(section), nil} - - parent_id -> - {:ok, parent} = Notebook.fetch_section(notebook, parent_id) - {container_ref_for_section(parent), nil} - end + default = {container_ref_for_section(section), nil} notebook |> Notebook.parent_cells_with_section(cell.id) diff --git a/test/livebook/intellisense_test.exs b/test/livebook/intellisense_test.exs index 3d8608f22..004eae965 100644 --- a/test/livebook/intellisense_test.exs +++ b/test/livebook/intellisense_test.exs @@ -1154,6 +1154,7 @@ defmodule Livebook.IntellisenseTest do assert content =~ "## Examples" end + @tag :erl_docs test "returns full Erlang docs" do {binding, env} = eval(do: nil) diff --git a/test/livebook/session_test.exs b/test/livebook/session_test.exs index abbd96d21..0a2a94ced 100644 --- a/test/livebook/session_test.exs +++ b/test/livebook/session_test.exs @@ -2,6 +2,7 @@ defmodule Livebook.SessionTest do use ExUnit.Case, async: true alias Livebook.{Session, Delta, Runtime, Utils, Notebook, FileSystem} + alias Livebook.Notebook.{Section, Cell} # Note: queueing evaluation in most of the tests below # requires the runtime to synchronously start first, @@ -549,6 +550,70 @@ defmodule Livebook.SessionTest do end end + describe "find_prev_locator/3" do + test "given cell in main flow returns previous Elixir cell" do + cell1 = %{Cell.new(:elixir) | id: "c1"} + cell2 = %{Cell.new(:markdown) | id: "c2"} + section1 = %{Section.new() | id: "s1", cells: [cell1, cell2]} + + cell3 = %{Cell.new(:elixir) | id: "c3"} + section2 = %{Section.new() | id: "s2", cells: [cell3]} + + notebook = %{Notebook.new() | sections: [section1, section2]} + + assert {:main_flow, "c1"} = Session.find_prev_locator(notebook, cell3, section2) + end + + test "given cell in branching section returns previous Elixir cell in that section" do + section1 = %{Section.new() | id: "s1"} + + cell1 = %{Cell.new(:elixir) | id: "c1"} + cell2 = %{Cell.new(:markdown) | id: "c2"} + cell3 = %{Cell.new(:elixir) | id: "c3"} + + section2 = %{ + Section.new() + | id: "s2", + parent_id: "s1", + cells: [cell1, cell2, cell3] + } + + notebook = %{Notebook.new() | sections: [section1, section2]} + + assert {"s2", "c1"} = Session.find_prev_locator(notebook, cell3, section2) + end + + test "given cell in main flow returns nil if there is no previous cell" do + cell1 = %{Cell.new(:markdown) | id: "c1"} + section1 = %{Section.new() | id: "s1", cells: [cell1]} + + cell2 = %{Cell.new(:elixir) | id: "c2"} + section2 = %{Section.new() | id: "s2", cells: [cell2]} + + notebook = %{Notebook.new() | sections: [section1, section2]} + + assert {:main_flow, nil} = Session.find_prev_locator(notebook, cell2, section2) + end + + test "given cell in branching section returns nil in that section if there is no previous cell" do + cell1 = %{Cell.new(:markdown) | id: "c1"} + section1 = %{Section.new() | id: "s1", cells: [cell1]} + + cell2 = %{Cell.new(:elixir) | id: "c2"} + + section2 = %{ + Section.new() + | id: "s2", + parent_id: "s1", + cells: [cell2] + } + + notebook = %{Notebook.new() | sections: [section1, section2]} + + assert {"s2", nil} = Session.find_prev_locator(notebook, cell2, section2) + end + end + defp start_session(opts \\ []) do session_id = Utils.random_id() {:ok, pid} = Session.start_link(Keyword.merge([id: session_id], opts))