Skip to content

Commit

Permalink
Remove scrollable and scroll implementation more primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Jan 13, 2021
1 parent 5d0b90d commit cd59582
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 48 deletions.
91 changes: 44 additions & 47 deletions autoload/vital/__vital__/VS/Vim/Window.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ function! s:do(winid, func) abort
noautocmd keepalt keepjumps call win_gotoid(l:curr_winid)
endfunction

"
" scrollable
"
function! s:scrollable(win, ...) abort
if len(a:000) == 1
call s:set_var(a:win, 'scrollable', a:000[0])
else
return s:get_var(a:win, 'scrollable')
endif
endfunction

"
" set_var
"
Expand All @@ -63,50 +52,58 @@ function! s:get_var(win, key) abort
return v:null
endfunction

"
" info
"
function! s:info(win) abort
if exists('*popup_list') && index(popup_list(), a:win) >= 0
let l:i = popup_getpos(a:win)
return {
\ 'row': l:i.line - 1,
\ 'col': l:i.col - 1,
\ 'width': l:i.width,
\ 'height': l:i.height,
\ 'topline': l:i.firstline
\ }
endif
let l:i = getwininfo(a:win)[0]
return {
\ 'row': l:i.winrow - 1,
\ 'col': l:i.wincol - 1,
\ 'width': l:i.width,
\ 'height': l:i.height,
\ 'topline': l:i.topline,
\ }
endfunction

"
" scroll
"
" NOTE: We can't test it because it uses timer.
"
function! s:scroll(delta) abort
if a:delta == 0
return
endif
function! s:scroll(win, topline) abort
let l:ctx = {}
function! l:ctx.callback(win, topline) abort
let l:wininfo = s:info(a:win)
let l:topline = a:topline
let l:topline = max([l:topline, 1])
let l:topline = min([l:topline, line('$') - l:wininfo.height + 1])

let l:wins = []
let l:wins += map(range(1, tabpagewinnr(tabpagenr(), '$')), 'win_getid(v:val)')
let l:wins += exists('*popup_list') ? popup_list() : []
for l:win in l:wins
if s:scrollable(l:win)
let l:ctx = {}
function! l:ctx.callback(win, delta) abort
let l:height = line('w$') - line('w0')
let l:topline = line('w0') + a:delta
let l:topline = max([l:topline, 1])
let l:topline = min([l:topline, line('$') - l:height])
let l:delta = l:topline - line('w0')
if l:delta == 0
return
endif
if l:topline == l:wininfo.topline
return
endif

if exists('*popup_create') && !empty(popup_getpos(a:win))
call popup_setoptions(a:win, {
\ 'firstline': l:topline,
\ })
else
execute printf('normal! %s%s%s%s',
\ abs(l:delta),
\ l:delta >= 0 ? "\<C-e>" : "\<C-y>",
\ abs(l:delta),
\ l:delta >= 0 ? 'j' : 'k',
\ )
endif
endfunction
call timer_start(0, { -> s:do(l:win, { -> l:ctx.callback(l:win, a:delta) }) })
break
if exists('*popup_list') && index(popup_list(), a:win) >= 0
call popup_setoptions(a:win, {
\ 'firstline': l:topline,
\ })
else
let l:delta = l:topline - l:wininfo.topline
let l:key = l:delta > 0 ? "\<C-e>" : "\<C-y>"
execute printf('normal! %s', repeat(l:key, abs(l:delta)))
endif
endfor
return "\<Ignore>"
endfunction
call s:do(a:win, { -> l:ctx.callback(a:win, a:topline) })
endfunction

"
Expand Down
83 changes: 83 additions & 0 deletions autoload/vital/__vital__/VS/Vim/Window.vimspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let s:expect = themis#helper('expect')
let s:Window = vital#vital#import('VS.Vim.Window')
let s:FloatingWindow = vital#vital#import('VS.Vim.Window.FloatingWindow')

Describe vital#__vital__#VS#Vim#Window

Expand All @@ -21,5 +22,87 @@ Describe vital#__vital__#VS#Vim#Window

End

Describe #info

It should return normal window info
call s:expect(s:Window.info(win_getid())).to_equal({
\ 'width': winwidth(0),
\ 'height': winheight(0),
\ 'topline': 1,
\ 'row': 0,
\ 'col': 0,
\ })
End

It should return floating window info
let l:bufnr = bufnr('test', v:true)
call bufload(l:bufnr)
call setbufline(l:bufnr, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

let l:window = s:FloatingWindow.new()
call l:window.open({
\ 'row': 0,
\ 'col': 0,
\ 'width': 10,
\ 'height': 10,
\ 'bufnr': l:bufnr,
\ })

call s:expect(s:Window.info(l:window.win)).to_equal({
\ 'width': 10,
\ 'height': 10,
\ 'topline': 1,
\ 'row': 0,
\ 'col': 0,
\ })

call l:window.close()
End

End

Describe #scroll
It should scroll normal window
vnew
resize 5
call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

let l:win = win_getid()
call s:Window.scroll(l:win, 1)
call s:expect(s:Window.info(l:win).topline).to_equal(1)
call s:Window.scroll(l:win, s:Window.info(l:win).topline + 4)
call s:expect(s:Window.info(l:win).topline).to_equal(5)
call s:Window.scroll(l:win, s:Window.info(l:win).topline + 4)
call s:expect(s:Window.info(l:win).topline).to_equal(7)

quit
End

It should scroll floating window
let l:bufnr = bufnr('test', v:true)
call bufload(l:bufnr)
call setbufline(l:bufnr, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

let l:window = s:FloatingWindow.new()
call l:window.open({
\ 'row': 0,
\ 'col': 0,
\ 'width': &columns,
\ 'height': 5,
\ 'bufnr': l:bufnr,
\ })

let l:win = l:window.win
call s:Window.scroll(l:win, 1)
call s:expect(s:Window.info(l:win).topline).to_equal(1)
call s:Window.scroll(l:win, s:Window.info(l:win).topline + 4)
call s:expect(s:Window.info(l:win).topline).to_equal(5)
call s:Window.scroll(l:win, s:Window.info(l:win).topline + 4)
call s:expect(s:Window.info(l:win).topline).to_equal(7)

call l:window.close()
End
End

End

Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function! s:MarkupContent.open(row, col, contents) abort
\ })
call setwinvar(self.window.win, '&wrap', 1)
call setwinvar(self.window.win, '&conceallevel', 2)
call s:Window.scrollable(self.window.win, v:true)
call s:Window.do(self.window.win, { -> s:Markdown.apply() })
endfunction

Expand Down

0 comments on commit cd59582

Please sign in to comment.