Skip to content

Commit

Permalink
Add clear history textarea feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Oct 11, 2024
1 parent ff41a5e commit 2ed6dcb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions library/lua/gui/widgets/text_area.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions library/lua/gui/widgets/text_area/history_store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions library/lua/gui/widgets/text_area/text_area_content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('_')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions test/library/gui/widgets.TextArea.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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

0 comments on commit 2ed6dcb

Please sign in to comment.