Skip to content

Commit

Permalink
Rewind the first time new text is inserted in the processed region
Browse files Browse the repository at this point in the history
The CursorMovedI event is triggered when new text is inserted, which is better
than InsertEnter that would resync the buffer on the next insert.

Also fix an off by one error around changing the last character in a statement,
or jumping to the last character in a statement.
  • Loading branch information
Kyle Stemen authored and Kyle Stemen committed Sep 24, 2017
1 parent 17d6617 commit dc87938
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 18 additions & 2 deletions autoload/coquille.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def sync(self):
self._reset()
else:
(line, col) = self.saved_sync.pos()
self.rewind_to(line - 1, col) # vim indexes from lines 1, coquille from 0
# vim indexes from lines 1, coquille from 0
self.rewind_to(line - 1, col - 1)
self.saved_sync = curr_sync

def _reset(self):
Expand Down Expand Up @@ -173,7 +174,9 @@ def coq_to_cursor(self):
else self.coq_top.states[-1].end)

if cline < line or (cline == line and ccol < col):
self.rewind_to(cline - 1, ccol)
# Add 1 to the column to leave whatever is at the
# cursor as sent.
self.rewind_to(cline - 1, ccol + 1)
else:
while True:
r = self._get_message_range((line, col))
Expand Down Expand Up @@ -422,12 +425,25 @@ def reset_color(self):
self.source_buffer.number)

def rewind_to(self, line, col):
""" Go backwards to the specified position
line and col are 0-based and point to the first position to
remove from the sent region.
"""
print("rewinding to", line, col)
print("last state", self.coq_top.states[-1].end)
if self.coq_top.coqtop is None:
print('Internal error: vimbufsync is still being called but coqtop\
appears to be down.')
print('Please report.')
return

if self.coq_top.states and self.coq_top.states[-1].end <= (line, col):
# The caller asked to rewind to a position after what has been
# processed. This quick path exits without having to search the
# state list.
return

predicate = lambda x: x.end <= (line, col)
lst = filter(predicate, self.coq_top.states)
steps = len(self.coq_top.states) - len(list(lst))
Expand Down
18 changes: 10 additions & 8 deletions autoload/coquille.vim
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ function! coquille#OpenSupportingBuffers(bufid, ...)
call setbufvar(a:bufid, "errors", -1)
execute "autocmd BufUnload <buffer=" . a:bufid .
\ "> call coquille#DetachSupportingBuffers(". a:bufid .")"
" Automatically sync the buffer when entering insert mode: this is usefull
" when we edit the portion of the buffer which has already been sent to coq,
" we can then rewind to the appropriate point.
" It's still incomplete though, the plugin won't sync when you undo or
" delete some part of your buffer. So the highlighting will be wrong, but
" nothing really problematic will happen, as sync will be called the next
" time you explicitly call a command (be it 'rewind' or 'interp')
execute "autocmd InsertEnter <buffer=" . a:bufid .
" Automatically sync the buffer when the cursor moves while in insert
" mode. Typically this happens when the buffer is modified. Syncing
" the buffer is useful when we edit the portion of the buffer which
" has already been sent to coq, we can then rewind to the appropriate
" point. It's still incomplete though, the plugin won't sync when you
" undo or delete some part of your buffer. So the highlighting will be
" wrong, but nothing really problematic will happen, as sync will be
" called the next time you explicitly call a command (be it 'rewind'
" or 'interp')
execute "autocmd CursorMovedI <buffer=" . a:bufid .
\ "> call coquille#Python('"
\ "coquille.BufferState.lookup_bufid(".
\ a:bufid . ").sync()')"
Expand Down

0 comments on commit dc87938

Please sign in to comment.