From 1bdef479099d1e25c63ba7f8a9de281a09a8e8f8 Mon Sep 17 00:00:00 2001 From: tanvirtin Date: Fri, 5 Apr 2024 18:26:59 -0400 Subject: [PATCH] fix(#344): replace j, k with event CursorMoved --- .../features/screens/HistoryScreen/init.lua | 17 +++--- .../screens/ProjectCommitsScreen/init.lua | 21 +++----- .../screens/ProjectDiffScreen/init.lua | 36 ++++++------- .../screens/ProjectHunksScreen/init.lua | 54 ++++++++----------- 4 files changed, 52 insertions(+), 76 deletions(-) diff --git a/lua/vgit/features/screens/HistoryScreen/init.lua b/lua/vgit/features/screens/HistoryScreen/init.lua index e4389e95..2bc248b5 100644 --- a/lua/vgit/features/screens/HistoryScreen/init.lua +++ b/lua/vgit/features/screens/HistoryScreen/init.lua @@ -65,6 +65,11 @@ function HistoryScreen:hunk_down() return self end +function HistoryScreen:handle_list_move(direction) + self.store:set_index(self.table_view:move(direction)) + self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) +end + function HistoryScreen:show() local buffer = Buffer(0) local err = self.store:fetch(self.layout_type, buffer.filename) @@ -106,21 +111,17 @@ function HistoryScreen:show() { mode = 'n', key = 'j', - handler = loop.coroutine(function() - self.store:set_index(self.table_view:move('down')) - self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) - end), + handler = loop.coroutine(function() self:handle_list_move('down') end), }, { mode = 'n', key = 'k', - handler = loop.coroutine(function() - self.store:set_index(self.table_view:move('up')) - self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) - end), + handler = loop.coroutine(function() self:handle_list_move('up') end), }, }) + self.table_view.scene:get('table').buffer:on('CursorMoved', loop.coroutine(function() self:handle_list_move() end)) + return true end diff --git a/lua/vgit/features/screens/ProjectCommitsScreen/init.lua b/lua/vgit/features/screens/ProjectCommitsScreen/init.lua index c8b222e5..4fa43c5f 100644 --- a/lua/vgit/features/screens/ProjectCommitsScreen/init.lua +++ b/lua/vgit/features/screens/ProjectCommitsScreen/init.lua @@ -59,8 +59,8 @@ function ProjectCommitsScreen:hunk_up() self.diff_view:prev() end function ProjectCommitsScreen:hunk_down() self.diff_view:next() end -function ProjectCommitsScreen:handle_list_move_down() - local list_item = self.foldable_list_view:move('down') +function ProjectCommitsScreen:handle_list_move(direction) + local list_item = self.foldable_list_view:move(direction) if not list_item then return @@ -70,17 +70,6 @@ function ProjectCommitsScreen:handle_list_move_down() self.diff_view:render_debounced(loop.coroutine(function() self.diff_view:navigate_to_mark(1) end)) end -function ProjectCommitsScreen:handle_list_move_up() - local list_item = self.foldable_list_view:move('up') - - if not list_item then - return - end - - self.store:set_id(list_item.id) - self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) -end - function ProjectCommitsScreen:handle_on_enter() local _, filename = self.store:get_filename() @@ -176,12 +165,12 @@ function ProjectCommitsScreen:show(args) { mode = 'n', key = 'j', - handler = loop.coroutine(function() self:handle_list_move_down() end), + handler = loop.coroutine(function() self:handle_list_move('down') end), }, { mode = 'n', key = 'k', - handler = loop.coroutine(function() self:handle_list_move_up() end), + handler = loop.coroutine(function() self:handle_list_move('up') end), }, { mode = 'n', @@ -190,6 +179,8 @@ function ProjectCommitsScreen:show(args) }, }) + self.foldable_list_view.scene:get('list').buffer:on('CursorMoved', loop.coroutine(function() self:handle_list_move() end)) + local list_item = self.foldable_list_view:move_to(function(node) local filename = node.path and node.path.file and node.path.file.filename or nil return filename == target_filename diff --git a/lua/vgit/features/screens/ProjectDiffScreen/init.lua b/lua/vgit/features/screens/ProjectDiffScreen/init.lua index 1a826b3b..0c2a0ec0 100644 --- a/lua/vgit/features/screens/ProjectDiffScreen/init.lua +++ b/lua/vgit/features/screens/ProjectDiffScreen/init.lua @@ -379,11 +379,21 @@ function ProjectDiffScreen:make_help_bar() return self end +function ProjectDiffScreen:handle_list_move(direction) + local list_item = self.foldable_list_view:move(direction) + + if not list_item then + return + end + + self.store:set_id(list_item.id) + self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) +end + function ProjectDiffScreen:show() loop.free_textlock() local err, data = self.store:fetch(self.layout_type) - loop.free_textlock() if err then console.debug.error(err).error(err) @@ -479,30 +489,12 @@ function ProjectDiffScreen:show() { mode = 'n', key = 'j', - handler = loop.coroutine(function() - local list_item = self.foldable_list_view:move('down') - - if not list_item then - return - end - - self.store:set_id(list_item.id) - self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) - end), + handler = loop.coroutine(function() self:handle_list_move('down') end), }, { mode = 'n', key = 'k', - handler = loop.coroutine(function() - local list_item = self.foldable_list_view:move('up') - - if not list_item then - return - end - - self.store:set_id(list_item.id) - self.diff_view:render_debounced(function() self.diff_view:navigate_to_mark(1) end) - end), + handler = loop.coroutine(function() self:handle_list_move('up') end), }, { mode = 'n', @@ -531,6 +523,8 @@ function ProjectDiffScreen:show() }, }) + self.foldable_list_view.scene:get('list').buffer:on('CursorMoved', loop.coroutine(function() self:handle_list_move() end)) + self:make_help_bar() local list_item = self.foldable_list_view:move_to(function(node) diff --git a/lua/vgit/features/screens/ProjectHunksScreen/init.lua b/lua/vgit/features/screens/ProjectHunksScreen/init.lua index 3b5b6205..98a46398 100644 --- a/lua/vgit/features/screens/ProjectHunksScreen/init.lua +++ b/lua/vgit/features/screens/ProjectHunksScreen/init.lua @@ -84,6 +84,23 @@ function ProjectHunksScreen:hunk_down() return self end +function ProjectHunksScreen:handle_list_move(direction) + local list_item = self.foldable_list_view:move(direction) + + if not list_item then + return + end + + self.store:set_id(list_item.id) + self.diff_view:render_debounced(loop.coroutine(function() + local _, data = self.store:get_data() + + if data then + self.diff_view:navigate_to_mark(data.mark_index) + end + end)) +end + function ProjectHunksScreen:show(opts) opts = opts or {} @@ -133,42 +150,12 @@ function ProjectHunksScreen:show(opts) { mode = 'n', key = 'j', - handler = loop.coroutine(function() - local list_item = self.foldable_list_view:move('down') - - if not list_item then - return - end - - self.store:set_id(list_item.id) - self.diff_view:render_debounced(loop.coroutine(function() - local _, data = self.store:get_data() - - if data then - self.diff_view:navigate_to_mark(data.mark_index) - end - end)) - end), + handler = loop.coroutine(function() self:handle_list_move('down') end), }, { mode = 'n', key = 'k', - handler = loop.coroutine(function() - local list_item = self.foldable_list_view:move('up') - - if not list_item then - return - end - - self.store:set_id(list_item.id) - self.diff_view:render_debounced(loop.coroutine(function() - local _, data = self.store:get_data() - - if data then - self.diff_view:navigate_to_mark(data.mark_index) - end - end)) - end), + handler = loop.coroutine(function() self:handle_list_move('up') end), }, { mode = 'n', @@ -196,6 +183,9 @@ function ProjectHunksScreen:show(opts) end), }, }) + + self.foldable_list_view.scene:get('list').buffer:on('CursorMoved', loop.coroutine(function() self:handle_list_move() end)) + return true end