Skip to content

Commit 22298ba

Browse files
authored
Merge pull request #256 from demopath/patch-1
feat: Move the current line by percentage
2 parents b225230 + 45de161 commit 22298ba

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lua/neominimap/window/util.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ local default_winopt = {
6060
signcolumn = "auto",
6161
number = false,
6262
relativenumber = false,
63-
scrolloff = 99999, -- To center minimap
63+
scrolloff = 0,
6464
sidescrolloff = 0,
6565
winblend = 0,
6666
cursorline = true,
@@ -79,6 +79,23 @@ M.set_winopt = function(opt, winid)
7979
config.winopt(opt, winid)
8080
end
8181

82+
---@param winid integer
83+
---@param row integer
84+
local function set_line(winid, row)
85+
local win_h = M.win_get_true_height(winid)
86+
local bufnr = api.nvim_win_get_buf(winid)
87+
local line_cnt = api.nvim_buf_line_count(bufnr)
88+
local topline = math.floor(row - (row * win_h) / line_cnt)
89+
row = math.max(1, math.min(row, line_cnt))
90+
topline = math.max(1, math.min(topline, line_cnt))
91+
return function()
92+
local view = vim.fn.winsaveview()
93+
view.topline = topline
94+
view.lnum = row - 1
95+
vim.fn.winrestview(view)
96+
end
97+
end
98+
8299
---@param swinid integer
83100
---@param mwinid integer
84101
M.sync_to_minimap = function(swinid, mwinid)
@@ -139,7 +156,7 @@ M.sync_to_source = function(swinid, mwinid)
139156
if row <= line_cnt then
140157
local util = require("neominimap.util")
141158
vim.schedule(function()
142-
local ok = util.noautocmd(pcall)(vim.api.nvim_win_set_cursor, mwinid, { row, 0 })
159+
local ok = util.noautocmd(pcall)(api.nvim_win_call, mwinid, set_line(mwinid, row))
143160
if not ok then
144161
logger.log.error("Failed to set cursor")
145162
end

0 commit comments

Comments
 (0)