From 2ed6dcbe8502ff3d9f77adf6dadc0c9ad2670db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Obr=C4=99bski?= Date: Fri, 11 Oct 2024 09:33:03 +0200 Subject: [PATCH] Add clear history textarea feature --- docs/dev/Lua API.rst | 4 +++ library/lua/gui/widgets/text_area.lua | 12 ++++--- .../gui/widgets/text_area/history_store.lua | 5 +++ .../widgets/text_area/text_area_content.lua | 8 ++--- test/library/gui/widgets.TextArea.lua | 32 ++++++++++++++++--- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index a47474c932..ecf816c91f 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -5568,6 +5568,10 @@ Functions: This is useful for automatically scrolling when the user moves the cursor beyond the visible region of the text area. +* ``textarea:clearHistory()`` + + Clear undo/redo history of the widget. + Functionality: - Cursor Control: Navigate through text using arrow keys (Left, Right, Up, diff --git a/library/lua/gui/widgets/text_area.lua b/library/lua/gui/widgets/text_area.lua index 52eac62d1f..e4984e2e76 100644 --- a/library/lua/gui/widgets/text_area.lua +++ b/library/lua/gui/widgets/text_area.lua @@ -64,6 +64,14 @@ function TextArea:getCursor() return self.text_area.cursor end +function TextArea:setCursor(cursor_offset) + return self.text_area:setCursor(cursor_offset) +end + +function TextArea:clearHistory() + return self.text_area.history:clear() +end + function TextArea:onCursorChange(cursor) local x, y = self.text_area.wrapped_text:indexToCoords( self.text_area.cursor @@ -91,10 +99,6 @@ function TextArea:scrollToCursor(cursor_offset) end end -function TextArea:setCursor(cursor_offset) - return self.text_area:setCursor(cursor_offset) -end - function TextArea:getPreferredFocusState() return self.parent_view.focus end diff --git a/library/lua/gui/widgets/text_area/history_store.lua b/library/lua/gui/widgets/text_area/history_store.lua index 4537d9bf69..abaf2de3ee 100644 --- a/library/lua/gui/widgets/text_area/history_store.lua +++ b/library/lua/gui/widgets/text_area/history_store.lua @@ -76,6 +76,11 @@ function HistoryStore:redo(curr_text, curr_cursor) return history_entry end +function HistoryStore:clear() + self.past = {} + self.future = {} +end + HistoryStore.HISTORY_ENTRY = HISTORY_ENTRY return HistoryStore diff --git a/library/lua/gui/widgets/text_area/text_area_content.lua b/library/lua/gui/widgets/text_area/text_area_content.lua index fcca75ac91..5c88902b7d 100644 --- a/library/lua/gui/widgets/text_area/text_area_content.lua +++ b/library/lua/gui/widgets/text_area/text_area_content.lua @@ -234,7 +234,7 @@ function TextAreaContent:onRenderBody(dc) and gui.blink_visible(530) ) - if (show_focus) then + if show_focus then local x, y = self.wrapped_text:indexToCoords(self.cursor) dc:seek(x - 1, y - 1) :char('_') @@ -403,12 +403,12 @@ function TextAreaContent:onInput(keys) self:copy() return true elseif keys.CUSTOM_CTRL_X then - self:cut() self.history:store(HISTORY_ENTRY.OTHER, self.text, self.cursor) + self:cut() return true elseif keys.CUSTOM_CTRL_V then - self:paste() self.history:store(HISTORY_ENTRY.OTHER, self.text, self.cursor) + self:paste() return true else return TextAreaContent.super.onInput(self, keys) @@ -608,7 +608,7 @@ function TextAreaContent:onTextManipulationInput(keys) return true elseif keys.CUSTOM_CTRL_A then -- select all - self:setSelection(1, #self.text) + self:setSelection(#self.text + 1, 1) return true elseif keys.CUSTOM_CTRL_U then -- delete current line diff --git a/test/library/gui/widgets.TextArea.lua b/test/library/gui/widgets.TextArea.lua index 2005c8e461..aac3da020f 100644 --- a/test/library/gui/widgets.TextArea.lua +++ b/test/library/gui/widgets.TextArea.lua @@ -1243,7 +1243,7 @@ function test.arrows_reset_selection() expect.eq(read_rendered_text(text_area), table.concat({ '60: Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '112: Sed consectetur, urna sit amet aliquet egestas, ante nibh ', - 'porttitor mi, vitae rutrum eros metus nec libero.', + 'porttitor mi, vitae rutrum eros metus nec libero._', }, '\n')); expect.eq(read_selected_text(text_area), table.concat({ @@ -1288,7 +1288,7 @@ function test.click_reset_selection() expect.eq(read_rendered_text(text_area), table.concat({ '60: Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '112: Sed consectetur, urna sit amet aliquet egestas, ante nibh ', - 'porttitor mi, vitae rutrum eros metus nec libero.', + 'porttitor mi, vitae rutrum eros metus nec libero._', }, '\n')); expect.eq(read_selected_text(text_area), table.concat({ @@ -1323,7 +1323,7 @@ function test.line_navigation_reset_selection() expect.eq(read_rendered_text(text_area), table.concat({ '60: Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '112: Sed consectetur, urna sit amet aliquet egestas, ante nibh ', - 'porttitor mi, vitae rutrum eros metus nec libero.', + 'porttitor mi, vitae rutrum eros metus nec libero._', }, '\n')); expect.eq(read_selected_text(text_area), table.concat({ @@ -1356,7 +1356,7 @@ function test.jump_begin_or_end_reset_selection() expect.eq(read_rendered_text(text_area), table.concat({ '60: Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '112: Sed consectetur, urna sit amet aliquet egestas, ante nibh ', - 'porttitor mi, vitae rutrum eros metus nec libero.', + 'porttitor mi, vitae rutrum eros metus nec libero._', }, '\n')); expect.eq(read_selected_text(text_area), table.concat({ @@ -2582,7 +2582,7 @@ function test.fast_rewind_reset_selection() expect.eq(read_rendered_text(text_area), table.concat({ '60: Lorem ipsum dolor sit amet, consectetur adipiscing elit.', '112: Sed consectetur, urna sit amet aliquet egestas, ante nibh ', - 'porttitor mi, vitae rutrum eros metus nec libero.', + 'porttitor mi, vitae rutrum eros metus nec libero._', }, '\n')); expect.eq(read_selected_text(text_area), table.concat({ @@ -2797,3 +2797,25 @@ function test.undo_redo_keyboard_changes() expect.eq(read_rendered_text(text_area), 'Lorem ipsum dolor sit _') screen:dismiss() end + +function test.clear_undo_redo_history() + local text_area, screen, window, widget = arrange_textarea({w=80}) + + local text = table.concat({ + 'Lorem ipsum dolor sit amet. ', + }, '\n') + + text_area:setText(text) + text_area:setCursor(#text + 1) + + simulate_input_text('A') + simulate_input_text(' ') + simulate_input_text('longer text') + + expect.eq(read_rendered_text(text_area), text .. 'A longer text_') + + widget:clearHistory() + simulate_input_keys('CUSTOM_CTRL_Z') + + expect.eq(read_rendered_text(text_area), text .. 'A longer text_') +end