summaryrefslogtreecommitdiffstats
path: root/plugin/50-smooth_scroll.lua
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-05-07 13:25:59 +0200
committerThomas Vanbesien <tvanbesi@proton.me>2026-05-07 13:25:59 +0200
commit960273f216af90706f841bb00bd862639848a2c3 (patch)
tree6596238bcfe1b10bb60e59b97ee4bc5cca375bf1 /plugin/50-smooth_scroll.lua
parent263ff8e8ca1c82e59a63fac56b13b312271644bc (diff)
downloadnvim-config-960273f216af90706f841bb00bd862639848a2c3.tar.gz
nvim-config-960273f216af90706f841bb00bd862639848a2c3.zip
feat(nvim): smooth scrolling plugin
Diffstat (limited to 'plugin/50-smooth_scroll.lua')
-rw-r--r--plugin/50-smooth_scroll.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/plugin/50-smooth_scroll.lua b/plugin/50-smooth_scroll.lua
new file mode 100644
index 0000000..84824a3
--- /dev/null
+++ b/plugin/50-smooth_scroll.lua
@@ -0,0 +1,35 @@
+local Dir = {
+ UP = 1,
+ DOWN = 2,
+}
+
+local function smooth_scroll(dir)
+ local view, start_pos, buf_line_count, win_height =
+ vim.fn.winsaveview(),
+ vim.api.nvim_win_get_cursor(0),
+ vim.api.nvim_buf_line_count(0),
+ vim.api.nvim_win_get_height(0)
+ local duration, sleep_duration = 100, 16 -- 60 fps
+ local max_distance = dir == Dir.DOWN and buf_line_count - start_pos[1] or start_pos[1] - 1
+ local distance = math.min(max_distance, math.floor(win_height / 2))
+ local steps_number = math.ceil(duration / sleep_duration) -- We want a movement every `sleep_duration` msec
+ local step = distance / steps_number
+ for i = 1, steps_number do
+ vim.defer_fn(function()
+ local offset = math.ceil(step * i) * (dir == Dir.DOWN and 1 or -1)
+ local new_pos = { start_pos[1] + offset, start_pos[2] }
+ vim.api.nvim_win_set_cursor(0, new_pos)
+ local new_topline = math.max(1, view.topline + offset)
+ if buf_line_count - new_topline >= win_height then -- don't move viewport beyond eof (DOWN) or first line (UP)
+ vim.fn.winrestview({ lnum = view.lnum + offset, topline = new_topline }) -- move viewport
+ end
+ end, sleep_duration * i)
+ end
+end
+
+vim.api.nvim_create_user_command("ScrollSmoothDown", function()
+ smooth_scroll(Dir.DOWN)
+end, { desc = "Scroll down smoothly" })
+vim.api.nvim_create_user_command("ScrollSmoothUp", function()
+ smooth_scroll(Dir.UP)
+end, { desc = "Scroll up smoothly" })