Skip to content

Commit 727ecaf

Browse files
Kyle StemenKyle Stemen
Kyle Stemen
authored and
Kyle Stemen
committed
Scroll the goal and info buffers back to the top whenever they change
1 parent 01a62bf commit 727ecaf

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

autoload/coquille.py

+26
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ def vim_repr(value):
4949
def make_vim_range(start, stop):
5050
return [[start[0] + 1, start[2] + 1], [stop[0] + 1, stop[2] + 1]]
5151

52+
# Return a list of all windows that are displaying the buffer, along with their
53+
# current cursor positions.
54+
def get_cursors_for_buffer(vim_buffer):
55+
result = []
56+
for win in vim.windows:
57+
if win.buffer is vim_buffer:
58+
result.append((win, win.cursor))
59+
return result
60+
61+
# Takes the list of window cursor positions from get_cursor_for_buffer. If the
62+
# cursor position is now lower for any of the windows, they are entered to
63+
# rescroll the window.
64+
def fix_scroll(cursors):
65+
refresh_now = None
66+
for win, (row, col) in cursors:
67+
if win.cursor[0] < row or win.cursor[1] < col:
68+
win.vars['coquille_needs_scroll_fix'] = 1
69+
if win.tabpage is vim.current.tabpage:
70+
vim.command("call coquille#FixWindowScrollTabWin(%d, %d)" %
71+
(win.tabpage.number, win.number))
72+
5273
# All the python side state associated with the vim source buffer
5374
class BufferState(object):
5475
# Dict mapping source buffer id to BufferState
@@ -274,6 +295,7 @@ def show_goal(self, response):
274295
modifiable = self.goal_buffer.options["modifiable"]
275296
self.goal_buffer.options["modifiable"] = True
276297
try:
298+
cursors = get_cursors_for_buffer(self.goal_buffer)
277299
del self.goal_buffer[:]
278300

279301
if response is None:
@@ -320,6 +342,8 @@ def show_goal(self, response):
320342
lines = map(lambda s: s.encode('utf-8'), ccl.split('\n'))
321343
self.goal_buffer.append(list(lines))
322344
self.goal_buffer.append('')
345+
346+
fix_scroll(cursors)
323347
finally:
324348
self.goal_buffer.options["modifiable"] = modifiable
325349
return True
@@ -329,6 +353,7 @@ def show_info(self, message):
329353
modifiable = self.info_buffer.options["modifiable"]
330354
self.info_buffer.options["modifiable"] = True
331355
try:
356+
cursors = get_cursors_for_buffer(self.info_buffer)
332357
del self.info_buffer[:]
333358
lst = []
334359
if message is not None:
@@ -346,6 +371,7 @@ def show_info(self, message):
346371
# extend function, and its append mostly behaves like extend.
347372
self.info_buffer[0] = lst[0]
348373
self.info_buffer.append(lst[1:])
374+
fix_scroll(cursors)
349375
finally:
350376
self.info_buffer.options["modifiable"] = modifiable
351377

autoload/coquille.vim

+32
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,36 @@ endfunction
682682

683683
let s:empty_range = [ { 'line': 0, 'col': 0}, { 'line': 0, 'col': 0} ]
684684

685+
function! coquille#FixWindowScroll(winid, hint_tabnr, hint_winnr)
686+
let l:cur_tab = tabpagenr()
687+
let l:cur_win = winnr()
688+
let l:tabwin = coquille#WinId2TabWin(a:winid, a:hint_tabnr, a:hint_winnr)
689+
if l:tabwin[0] != l:cur_tab
690+
" Only fix the scroll for windows in the current tab. The other
691+
" windows will be fixed when their tab is entered.
692+
return 0
693+
endif
694+
695+
if coquille#GetWinVar(a:winid, l:tabwin[0], l:tabwin[1],
696+
\ "coquille_needs_scroll_fix", 0)
697+
let l:cur_winid = coquille#WinGetId(l:cur_tab, l:cur_win)
698+
call coquille#SetWinVar(a:winid, l:tabwin[0], l:tabwin[1],
699+
\ "coquille_needs_scroll_fix", 0)
700+
" When the cursor is at the last line of the buffer, then the number
701+
" of lines in the buffer is reduced from python, vim leaves the window
702+
" scrolled past the end. Just entering the window is enough to fix it.
703+
call coquille#WinGoToId(a:winid, l:tabwin[0], l:tabwin[1])
704+
705+
call coquille#WinGoToId(l:cur_winid, l:cur_tab, l:cur_win)
706+
return 1
707+
endif
708+
endfunction
709+
710+
function! coquille#FixWindowScrollTabWin(tabnr, winnr)
711+
let l:winid = coquille#WinGetId(a:tabnr, a:winnr)
712+
return coquille#FixWindowScroll(l:winid, a:tabnr, a:winnr)
713+
endfunction
714+
685715
function! coquille#SyncWindowColors(winid, hint_tabnr, hint_winnr)
686716
let l:cur_tab = tabpagenr()
687717
let l:cur_win = winnr()
@@ -772,8 +802,10 @@ function! coquille#TabActivated()
772802
while l:cur_win <= tabpagewinnr(l:cur_tab, "$")
773803
let l:cur_winid = coquille#WinGetId(l:cur_tab, l:cur_win)
774804
call coquille#SyncWindowColors(l:cur_winid, l:cur_tab, l:cur_win)
805+
call coquille#FixWindowScroll(l:cur_winid, l:cur_tab, l:cur_win)
775806
let l:cur_win += 1
776807
endwhile
808+
777809
endfunction
778810

779811
function! coquille#WindowActivated(winid, hint_tabnr, hint_winnr)

0 commit comments

Comments
 (0)