fix(preview): stick to top left corner while scrolling in normal window

This commit is contained in:
kevinhwang91 2022-11-19 09:43:37 +08:00
parent dfa4a5b32f
commit a346e88c77
3 changed files with 14 additions and 18 deletions

View file

@ -4,9 +4,7 @@ DEPS ?= build
LUA_LS ?= $(DEPS)/lua-language-server LUA_LS ?= $(DEPS)/lua-language-server
LINT_LEVEL ?= Information LINT_LEVEL ?= Information
all: deps all:
deps: | $(LUA_LS)
lint: lint:
@rm -rf $(LUA_LS) @rm -rf $(LUA_LS)

View file

@ -11,7 +11,6 @@ local Buffer = {}
function Buffer:new(bufnr) function Buffer:new(bufnr)
local o = setmetatable({}, self) local o = setmetatable({}, self)
self.__index = self self.__index = self
o.id = vim.loop.hrtime()
o.bufnr = bufnr o.bufnr = bufnr
o:reload() o:reload()
return o return o

View file

@ -89,13 +89,13 @@ function Preview:trace(bufnr)
}) })
end end
function Preview:scroll(char) function Preview:scroll(char, toTopLeft)
if not self.validate() then if not self.validate() then
return return
end end
floatwin:call(function() floatwin:call(function()
local ctrlTbl = {B = 0x02, D = 0x04, E = 0x05, F = 0x06, U = 0x15, Y = 0x19} local ctrlTbl = {B = 0x02, D = 0x04, E = 0x05, F = 0x06, U = 0x15, Y = 0x19}
cmd(('norm! %c'):format(ctrlTbl[char])) cmd(('norm! %c%s'):format(ctrlTbl[char], toTopLeft and 'H_' or ''))
end) end)
self:viewChanged() self:viewChanged()
end end
@ -115,8 +115,9 @@ end
local function onBufRemap(bufnr, str) local function onBufRemap(bufnr, str)
local self = Preview local self = Preview
local isNormalBuf = bufnr == self.bufnr
if str == 'switch' then if str == 'switch' then
if bufnr == self.bufnr then if isNormalBuf then
api.nvim_set_current_win(floatwin.winid) api.nvim_set_current_win(floatwin.winid)
vim.wo.cul = true vim.wo.cul = true
else else
@ -129,17 +130,17 @@ local function onBufRemap(bufnr, str)
elseif str == 'close' then elseif str == 'close' then
self:close() self:close()
elseif str == 'scrollB' then elseif str == 'scrollB' then
self:scroll('B') self:scroll('B', isNormalBuf)
elseif str == 'scrollF' then elseif str == 'scrollF' then
self:scroll('F') self:scroll('F', isNormalBuf)
elseif str == 'scrollU' then elseif str == 'scrollU' then
self:scroll('U') self:scroll('U', isNormalBuf)
elseif str == 'scrollD' then elseif str == 'scrollD' then
self:scroll('D') self:scroll('D', isNormalBuf)
elseif str == 'scrollE' then elseif str == 'scrollE' then
self:scroll('E') self:scroll('E', isNormalBuf)
elseif str == 'scrollY' then elseif str == 'scrollY' then
self:scroll('Y') self:scroll('Y', isNormalBuf)
elseif str == 'wheelUp' or str == 'wheelDown' then elseif str == 'wheelUp' or str == 'wheelDown' then
promise.resolve():thenCall(function() promise.resolve():thenCall(function()
self:viewChanged() self:viewChanged()
@ -227,7 +228,7 @@ end
--- ---
---@param enter? boolean ---@param enter? boolean
---@param nextLineIncluded? boolean ---@param nextLineIncluded? boolean
---@return number? floatwinId ---@return number? floatWinId
function Preview:peekFoldedLinesUnderCursor(enter, nextLineIncluded) function Preview:peekFoldedLinesUnderCursor(enter, nextLineIncluded)
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
local fb = fold.get(bufnr) local fb = fold.get(bufnr)
@ -253,11 +254,9 @@ function Preview:peekFoldedLinesUnderCursor(enter, nextLineIncluded)
local text = fb:lines(lnum, endLnum) local text = fb:lines(lnum, endLnum)
self:display(enter, function() self:display(enter, function()
floatwin:setContent(text) floatwin:setContent(text)
api.nvim_win_set_cursor(floatwin.winid, {oLnum - lnum + 1, oCol})
if oLnum > lnum then if oLnum > lnum then
floatwin:call(function() floatwin:call(utils.zz)
api.nvim_win_set_cursor(0, {oLnum - lnum + 1, oCol})
utils.zz()
end)
end end
floatwin:refreshTopline() floatwin:refreshTopline()
end) end)