From 9874a4ac0423127576974e7639e60c7b3eb358d4 Mon Sep 17 00:00:00 2001 From: Declan Mullen Date: Sun, 21 Jul 2024 14:20:12 -0700 Subject: [PATCH] fix(scroll): don't execute the callback if interrupted --- README.md | 33 ++++++++++++++++++++------------- lua/cinnamon/scroll.lua | 33 ++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f2cbe30..9c0acba 100644 --- a/README.md +++ b/README.md @@ -58,24 +58,24 @@ A settings table can be passed into the setup function for custom options. return { -- Disable the plugin disabled = false, + keymaps = { -- Enable the provided 'basic' keymaps basic = false, -- Enable the provided 'extra' keymaps extra = false, }, + ---@class ScrollOptions options = { - -- Optional post-movement callback - callback = function() end, + -- The scrolling mode + -- `cursor`: animate cursor and window scrolling for any movement + -- `window`: animate window scrolling ONLY when the cursor moves out of view + mode = "cursor", + -- Delay between each movement step (in ms) delay = 5, - step_size = { - -- Number of cursor/window lines moved per step - vertical = 1, - -- Number of cursor/window columns moved per step - horizontal = 2, - }, + max_delta = { -- Maximum distance for line movements before scroll -- animation is skipped. Set to `false` to disable @@ -83,13 +83,20 @@ return { -- Maximum distance for column movements before scroll -- animation is skipped. Set to `false` to disable column = false, - -- Maximum duration for a movement (in ms). Automatically scales the delay and step size + -- Maximum duration for a movement (in ms). Automatically scales the + -- delay and step size time = 1000, }, - -- The scrolling mode - -- `cursor`: animate cursor and window scrolling for any movement - -- `window`: animate window scrolling ONLY when the cursor moves out of view - mode = "cursor", + + step_size = { + -- Number of cursor/window lines moved per step + vertical = 1, + -- Number of cursor/window columns moved per step + horizontal = 2, + }, + + -- Optional post-movement callback. Not called if the movement is interrupted + callback = function() end, }, } ``` diff --git a/lua/cinnamon/scroll.lua b/lua/cinnamon/scroll.lua index 9257f98..c482d26 100644 --- a/lua/cinnamon/scroll.lua +++ b/lua/cinnamon/scroll.lua @@ -85,7 +85,8 @@ H.scroller = { vim.fn.winrestview(original_view) self:start() else - self:cleanup() + self:execute_callback() + self.locked = false end end, @@ -149,6 +150,7 @@ H.scroller = { ) then self:stop() + self.locked = false return end @@ -204,6 +206,8 @@ H.scroller = { then self:move_to_target() self:stop() + self:execute_callback() + self.locked = false return end end, @@ -228,6 +232,16 @@ H.scroller = { vim.cmd.redraw() end, + execute_callback = function(self) + local callback = self.options.callback or self.options._weak_callback + if callback ~= nil then + local success, message = pcall(callback) + if not success then + utils.notify("Error executing callback: " .. message, "warn") + end + end + end, + stop = function(self) self.scroll_scheduler:close() self.timeout_timer:close() @@ -242,18 +256,6 @@ H.scroller = { vim.wo[self.window_id].virtualedit = self.saved_virtualedit vim.api.nvim_exec_autocmds("User", { pattern = "CinnamonScrollPost" }) - self:cleanup() - end, - - cleanup = function(self) - local callback = self.options.callback or self.options._weak_callback - if callback ~= nil then - local success, message = pcall(callback) - if not success then - utils.notify("Error executing callback: " .. message, "warn") - end - end - self.locked = false end, } @@ -297,7 +299,8 @@ end H.move_step = function(component, distance) local command = "normal! " local movement = "" - local count = math.floor(math.abs(distance)) + distance = (distance > 0) and math.floor(distance) or math.ceil(distance) + local count = math.abs(distance) if count == 0 then return 0 @@ -319,7 +322,7 @@ H.move_step = function(component, distance) vim.cmd(command .. movement) - return (distance < 0) and -count or count + return distance end ---@return Position