Normalize newlines in initial smart cell editor content (#1400)

This commit is contained in:
Jonatan Kłosko 2022-09-13 11:15:25 +02:00 committed by GitHub
parent 0b9f53a122
commit 9434cf57b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 1 deletions

View file

@ -1072,6 +1072,15 @@ defmodule Livebook.Session do
end
def handle_info({:runtime_smart_cell_started, id, info}, state) do
info =
if info.editor do
normalize_newlines = &String.replace(&1, "\r\n", "\n")
info = update_in(info.source, normalize_newlines)
update_in(info.editor.source, normalize_newlines)
else
info
end
case Notebook.fetch_cell_and_section(state.data.notebook, id) do
{:ok, cell, _section} ->
delta = Livebook.JSInterop.diff(cell.source, info.source)

View file

@ -736,7 +736,7 @@ defmodule Livebook.LiveMarkdown.ImportTest do
end
test "imports snippets with output info string" do
# We now explicitly mark every output sinppet with <!-- livebook:{"output":true} -->
# We now explicitly mark every output snippet with <!-- livebook:{"output":true} -->
# and use empty snippets for textual outputs, however previously
# we supported ```output too, so let's ensure they still work

View file

@ -694,6 +694,48 @@ defmodule Livebook.SessionTest do
assert_receive {:editor_source, "content!"}
end
test "normalizes line endings in smart cells having an editor" do
# Prior to Livebook 0.7.0 the editor would use system line endings,
# hence smart cells having editor may have CRLF in their persisted
# source, so we want to normalize it upfront
smart_cell = %{Notebook.Cell.new(:smart) | kind: "text", source: ""}
notebook = %{Notebook.new() | sections: [%{Notebook.Section.new() | cells: [smart_cell]}]}
session = start_session(notebook: notebook)
runtime = connected_noop_runtime()
Session.set_runtime(session.pid, runtime)
send(
session.pid,
{:runtime_smart_cell_definitions, [%{kind: "text", name: "Text", requirement: nil}]}
)
server_pid = self()
send(
session.pid,
{:runtime_smart_cell_started, smart_cell.id,
%{
source: "content\r\nmultiline",
js_view: %{ref: smart_cell.id, pid: server_pid, assets: %{}},
editor: %{language: nil, placement: :bottom, source: "content\r\nmultiline"}
}}
)
assert %{
notebook: %{
sections: [
%{
cells: [
%{source: "content\nmultiline", editor: %{source: "content\nmultiline"}}
]
}
]
}
} = Session.get_data(session.pid)
end
test "pings the smart cell before evaluation to await all incoming messages" do
smart_cell = %{Notebook.Cell.new(:smart) | kind: "text", source: "1"}
notebook = %{Notebook.new() | sections: [%{Notebook.Section.new() | cells: [smart_cell]}]}