Skip to content

Commit

Permalink
VS.Vim.Window: fix Window.scroll with border (#25)
Browse files Browse the repository at this point in the history
* VS.Vim.Window: fix Window.scroll with border

* VS.Vim.Window: add max scroll test
  • Loading branch information
hrsh7th authored May 10, 2024
1 parent c80fa10 commit e759865
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
52 changes: 51 additions & 1 deletion autoload/vital/__vital__/VS/Vim/Window.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,55 @@ endfunction
if has('nvim')
function! s:info(winid) abort
let l:info = getwininfo(a:winid)[0]

if s:is_floating(a:winid)
let l:config = nvim_win_get_config(a:winid)
let l:config.border = get(l:config, 'border', 'none')
if type(l:config.border) !=# type([])
if index(['rounded', 'single', 'double', 'solid'], l:config.border) >= 0
let l:width_off = 2
let l:height_off = 2
elseif l:config.border ==# 'shadow'
let l:width_off = 1
let l:height_off = 1
else
let l:width_off = 0
let l:height_off = 0
endif
else
let l:has_top = v:false
let l:has_top = l:has_top || get(l:config.border, 0, '') !=# ''
let l:has_top = l:has_top || get(l:config.border, 1, '') !=# ''
let l:has_top = l:has_top || get(l:config.border, 2, '') !=# ''
let l:has_right = v:false
let l:has_right = l:has_right || get(l:config.border, 2, '') !=# ''
let l:has_right = l:has_right || get(l:config.border, 3, '') !=# ''
let l:has_right = l:has_right || get(l:config.border, 4, '') !=# ''
let l:has_bottom = v:false
let l:has_bottom = l:has_bottom || get(l:config.border, 4, '') !=# ''
let l:has_bottom = l:has_bottom || get(l:config.border, 5, '') !=# ''
let l:has_bottom = l:has_bottom || get(l:config.border, 6, '') !=# ''
let l:has_left = v:false
let l:has_left = l:has_left || get(l:config.border, 6, '') !=# ''
let l:has_left = l:has_left || get(l:config.border, 7, '') !=# ''
let l:has_left = l:has_left || get(l:config.border, 0, '') !=# ''

let l:width_off = (l:has_left ? 1 : 0) + (l:has_right ? 1 : 0)
let l:height_off = (l:has_top ? 1 : 0) + (l:has_bottom ? 1 : 0)
endif
let l:left = get(l:config, '')
let l:info.core_width = l:config.width - l:width_off
let l:info.core_height = l:config.height - l:height_off
else
let l:info.core_width = l:info.width
let l:info.core_height = l:info.height
endif

return {
\ 'width': l:info.width,
\ 'height': l:info.height,
\ 'core_width': l:info.core_width,
\ 'core_height': l:info.core_height,
\ 'topline': l:info.topline,
\ }
endfunction
Expand All @@ -49,6 +95,8 @@ else
return {
\ 'width': l:info.width,
\ 'height': l:info.height,
\ 'core_width': l:info.core_width,
\ 'core_height': l:info.core_height,
\ 'topline': l:info.firstline
\ }
endif
Expand All @@ -58,6 +106,8 @@ else
function! l:ctx.callback() abort
let self.info.width = winwidth(0)
let self.info.height = winheight(0)
let self.info.core_width = self.info.width
let self.info.core_height = self.info.height
let self.info.topline = line('w0')
endfunction
call s:do(a:winid, { -> l:ctx.callback() })
Expand Down Expand Up @@ -97,7 +147,7 @@ function! s:scroll(winid, topline) abort
function! l:ctx.callback(winid, topline) abort
let l:wininfo = s:info(a:winid)
let l:topline = a:topline
let l:topline = min([l:topline, line('$') - l:wininfo.height + 1])
let l:topline = min([l:topline, line('$') - l:wininfo.core_height + 1])
let l:topline = max([l:topline, 1])

if l:topline == l:wininfo.topline
Expand Down
14 changes: 11 additions & 3 deletions autoload/vital/__vital__/VS/Vim/Window.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Describe vital#__vital__#VS#Vim#Window
call s:expect(s:Window.info(win_getid())).to_equal({
\ 'width': winwidth(0),
\ 'height': winheight(0),
\ 'core_width': winwidth(0),
\ 'core_height': winheight(0),
\ 'topline': 1,
\ })
End
Expand All @@ -53,15 +55,19 @@ Describe vital#__vital__#VS#Vim#Window
let l:win = s:FloatingWindow.new()
call l:win.set_bufnr(l:bufnr)
call l:win.open({
\ 'border': v:true,
\ 'row': 0,
\ 'col': 0,
\ 'width': 10,
\ 'height': 10,
\ })

let l:compat_off = has('nvim') ? 0 : 2
call s:expect(s:Window.info(l:win._winid)).to_equal({
\ 'width': 10,
\ 'height': 10,
\ 'width': 10 + l:compat_off,
\ 'height': 10 + l:compat_off,
\ 'core_width': 8 + l:compat_off,
\ 'core_height': 8 + l:compat_off,
\ 'topline': 1,
\ })

Expand Down Expand Up @@ -121,18 +127,20 @@ Describe vital#__vital__#VS#Vim#Window
let l:win = s:FloatingWindow.new()
call l:win.set_bufnr(l:bufnr)
call l:win.open({
\ 'border': v:true,
\ 'row': 0,
\ 'col': 0,
\ 'width': &columns,
\ 'height': 5,
\ })

let l:compat_off = has('nvim') ? 2 : 0
call s:Window.scroll(l:win._winid, 1)
call s:expect(s:Window.info(l:win._winid).topline).to_equal(1)
call s:Window.scroll(l:win._winid, s:Window.info(l:win._winid).topline + 4)
call s:expect(s:Window.info(l:win._winid).topline).to_equal(5)
call s:Window.scroll(l:win._winid, s:Window.info(l:win._winid).topline + 4)
call s:expect(s:Window.info(l:win._winid).topline).to_equal(7)
call s:expect(s:Window.info(l:win._winid).topline).to_equal(7 + l:compat_off)
End

End
Expand Down

0 comments on commit e759865

Please sign in to comment.