Skip to content

Commit

Permalink
LibGUI: Fix wrong cursor position after undoing RemoveTextCommand
Browse files Browse the repository at this point in the history
When you undo some forward delete shortcuts like <Del> or <Ctrl-Del>,
the cursor will be put at the end of the text deleted, while the right
position should be the start of those text.
  • Loading branch information
lixk28 committed Jun 6, 2023
1 parent 162a2b6 commit 50c2235
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
7 changes: 4 additions & 3 deletions Userland/Libraries/LibGUI/TextDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,11 @@ void InsertTextCommand::undo()
m_document.set_all_cursors(m_range.start());
}

RemoveTextCommand::RemoveTextCommand(TextDocument& document, DeprecatedString const& text, TextRange const& range)
RemoveTextCommand::RemoveTextCommand(TextDocument& document, DeprecatedString const& text, TextRange const& range, TextPosition const& original_cursor_position)
: TextDocumentUndoCommand(document)
, m_text(text)
, m_range(range)
, m_original_cursor_position(original_cursor_position)
{
}

Expand Down Expand Up @@ -1006,8 +1007,8 @@ void RemoveTextCommand::redo()

void RemoveTextCommand::undo()
{
auto new_cursor = m_document.insert_at(m_range.start(), m_text);
m_document.set_all_cursors(new_cursor);
m_document.insert_at(m_range.start(), m_text);
m_document.set_all_cursors(m_original_cursor_position);
}

InsertLineCommand::InsertLineCommand(TextDocument& document, TextPosition cursor, DeprecatedString&& text, InsertPosition pos)
Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibGUI/TextDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class InsertTextCommand : public TextDocumentUndoCommand {

class RemoveTextCommand : public TextDocumentUndoCommand {
public:
RemoveTextCommand(TextDocument&, DeprecatedString const&, TextRange const&);
RemoveTextCommand(TextDocument&, DeprecatedString const&, TextRange const&, TextPosition const&);
virtual ~RemoveTextCommand() = default;
virtual void undo() override;
virtual void redo() override;
Expand All @@ -265,6 +265,7 @@ class RemoveTextCommand : public TextDocumentUndoCommand {
private:
DeprecatedString m_text;
TextRange m_range;
TextPosition m_original_cursor_position;
};

class InsertLineCommand : public TextDocumentUndoCommand {
Expand Down
26 changes: 13 additions & 13 deletions Userland/Libraries/LibGUI/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ void TextEditor::keydown_event(KeyEvent& event)
erase_count = grapheme_break_position - m_cursor.column();
}
TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + erase_count });
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, erased_range.start());
return;
}
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
Expand All @@ -1136,7 +1136,7 @@ void TextEditor::keydown_event(KeyEvent& event)
erase_count = document().first_word_break_after({ m_cursor.line() + 1, 0 }).column();
}
TextRange erased_range(m_cursor, { m_cursor.line() + 1, erase_count });
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, erased_range.end());
return;
}
return;
Expand Down Expand Up @@ -1172,14 +1172,14 @@ void TextEditor::keydown_event(KeyEvent& event)
// Backspace within line
TextRange erased_range({ m_cursor.line(), m_cursor.column() - erase_count }, m_cursor);
auto erased_text = document().text_in_range(erased_range);
execute<RemoveTextCommand>(erased_text, erased_range);
execute<RemoveTextCommand>(erased_text, erased_range, erased_range.end());
return;
}
if (m_cursor.column() == 0 && m_cursor.line() != 0) {
// Backspace at column 0; merge with previous line
size_t previous_length = line(m_cursor.line() - 1).length();
TextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor);
execute<RemoveTextCommand>("\n", erased_range);
execute<RemoveTextCommand>("\n", erased_range, erased_range.end());
return;
}
return;
Expand Down Expand Up @@ -1278,7 +1278,7 @@ void TextEditor::unindent_line()
void TextEditor::delete_previous_word()
{
TextRange to_erase(document().first_word_before(m_cursor, true), m_cursor);
execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase);
execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase, to_erase.end());
}

void TextEditor::delete_current_line()
Expand All @@ -1300,7 +1300,7 @@ void TextEditor::delete_current_line()
}

TextRange erased_range(start, end);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, m_cursor);
}

void TextEditor::delete_previous_char()
Expand All @@ -1317,14 +1317,14 @@ void TextEditor::delete_previous_char()
to_erase.set_start({ m_cursor.line() - 1, prev_line_len });
}

execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase);
execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase, to_erase.end());
}

void TextEditor::delete_from_line_start_to_cursor()
{
TextPosition start(m_cursor.line(), current_line().first_non_whitespace_column());
TextRange to_erase(start, m_cursor);
execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase);
execute<RemoveTextCommand>(document().text_in_range(to_erase), to_erase, m_cursor);
}

void TextEditor::do_delete()
Expand All @@ -1338,13 +1338,13 @@ void TextEditor::do_delete()
if (m_cursor.column() < current_line().length()) {
// Delete within line
TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, erased_range.start());
return;
}
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
// Delete at end of line; merge with next line
TextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, erased_range.start());
return;
}
}
Expand Down Expand Up @@ -1755,7 +1755,7 @@ void TextEditor::delete_selection()
auto selection = normalized_selection();
auto selected = selected_text();
m_selection.clear();
execute<RemoveTextCommand>(selected, selection);
execute<RemoveTextCommand>(selected, selection, selection.end());
did_update_selection();
did_change();
set_cursor(selection.start());
Expand All @@ -1765,7 +1765,7 @@ void TextEditor::delete_selection()
void TextEditor::delete_text_range(TextRange range)
{
auto normalized_range = range.normalized();
execute<RemoveTextCommand>(document().text_in_range(normalized_range), normalized_range);
execute<RemoveTextCommand>(document().text_in_range(normalized_range), normalized_range, normalized_range.end());
did_change();
set_cursor(normalized_range.start());
update();
Expand All @@ -1791,7 +1791,7 @@ void TextEditor::insert_at_cursor_or_replace_selection(StringView text)
TextPosition start(original_cursor_position.line() - 1, 0);
TextPosition end(original_cursor_position.line() - 1, clear_length);
TextRange erased_range(start, end);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range, erased_range.end());
set_cursor(original_cursor_position);
}
}
Expand Down

0 comments on commit 50c2235

Please sign in to comment.