Skip to content

Commit

Permalink
editor: Fix issue with splitting at index zero
Browse files Browse the repository at this point in the history
  • Loading branch information
mjakeman committed Aug 31, 2022
1 parent ff66bd3 commit 04650a6
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/editor/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,8 +1132,35 @@ text_editor_split_at_mark (TextEditor *self,

current = split->paragraph;

// Case 1: Split is happening on the last index
if (split->index == text_paragraph_get_length (current))
// Case 1: Split is happening on the first index
if (split->index == 0)
{
TextParagraph *temp;

// Move all runs in the current paragraph to a new paragraph
// after this one and make the current paragraph contain an
// empty run.

// In reality, we simply prepend a blank paragraph as that is
// faster.

new = text_paragraph_new ();
text_paragraph_append_run (new, text_run_new (""));

parent = text_node_get_parent (TEXT_NODE (current));
text_node_insert_child_before (parent, TEXT_NODE (new), TEXT_NODE (current));

// In order for mark distribution to work as expected, we need
// to pretend like we created a new paragraph after the current
// one and moved all runs to it. Hence we must swap the 'new' and
// 'current' variables.
temp = current;
current = new;
new = temp;
}

// Case 2: Split is happening on the last index
else if (split->index == text_paragraph_get_length (current))
{
// Append a new paragraph with empty run
new = text_paragraph_new ();
Expand All @@ -1143,7 +1170,7 @@ text_editor_split_at_mark (TextEditor *self,
text_node_insert_child_after (parent, TEXT_NODE (new), TEXT_NODE (current));
}

// Case 2: Split happens mid-paragraph
// Case 3: Split happens mid-paragraph
else
{
TextRun *start;
Expand Down Expand Up @@ -1209,7 +1236,7 @@ text_editor_split_at_mark (TextEditor *self,

// Mark is on split point
if (mark->paragraph == current &&
mark->index == split->index)
mark->index == split->index)
{
_distribute_mark (mark, current, split->index, new, 0);
continue;
Expand Down

0 comments on commit 04650a6

Please sign in to comment.