Skip to content

Commit 16c21e0

Browse files
afterlookdoorgan
andauthored
fix: utf8_prefix should take into account empty lines (#164)
* fix: utf8_prefix should take into account empty lines If empty line is provided, the index gets out of bounds Resolves #163 Signed-off-by: afterlook <[email protected]> * test: add test for updates on empty document * refactor: avoid pipe in min/max calc --------- Signed-off-by: afterlook <[email protected]> Co-authored-by: doorgan <[email protected]>
1 parent db19710 commit 16c21e0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

apps/expert/test/forge/document_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ defmodule Forge.DocumentTest do
260260
assert doc.version == 0
261261
end
262262

263+
test "update on empty document" do
264+
assert {:ok, doc} = run_changes("", [], version: 1)
265+
assert "" == text(doc)
266+
assert doc.version == 0
267+
end
268+
263269
test "setting the version" do
264270
assert {:ok, doc} = run_changes("abc123", [edit("mornin")], version: 3)
265271
assert "mornin" == text(doc)

apps/forge/lib/forge/document.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ defmodule Forge.Document do
373373
end
374374

375375
defp utf8_prefix(line(text: text), start_code_unit) do
376-
length = max(0, start_code_unit - 1)
376+
byte_count = byte_size(text)
377+
desired_length = start_code_unit - 1
378+
length = min(max(0, desired_length), byte_count)
379+
377380
binary_part(text, 0, length)
378381
end
379382

0 commit comments

Comments
 (0)