Skip to content

Commit

Permalink
fix(scroll): don't execute the callback if interrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
declancm committed Jul 21, 2024
1 parent c9d18ea commit 9874a4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,45 @@ 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
line = false,
-- 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,
},
}
```
Expand Down
33 changes: 18 additions & 15 deletions lua/cinnamon/scroll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ H.scroller = {
vim.fn.winrestview(original_view)
self:start()
else
self:cleanup()
self:execute_callback()
self.locked = false
end
end,

Expand Down Expand Up @@ -149,6 +150,7 @@ H.scroller = {
)
then
self:stop()
self.locked = false
return
end

Expand Down Expand Up @@ -204,6 +206,8 @@ H.scroller = {
then
self:move_to_target()
self:stop()
self:execute_callback()
self.locked = false
return
end
end,
Expand All @@ -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()
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9874a4a

Please sign in to comment.