Restore cursor position after omnifunc call

Calling directly the omnifunc may move the cursor position. This is the case
with the default Vim omnifunc for C-family languages (ccomplete#Complete) which
calls searchdecl to find a declaration. This function is supposed to move the
cursor to the found declaration but it doesn't when called through the omni
completion mapping (CTRL-X CTRL-O). So, we restore the cursor position after
calling the omnifunc.
This commit is contained in:
micbou 2017-07-03 14:52:39 +02:00
parent c9be11ab81
commit 83bb78a2d6
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
2 changed files with 16 additions and 1 deletions

View File

@ -90,13 +90,22 @@ class OmniCompleter( Completer ):
# because it affects the value returned by 'query' # because it affects the value returned by 'query'
request_data[ 'start_column' ] = return_value + 1 request_data[ 'start_column' ] = return_value + 1
# Calling directly the omnifunc may move the cursor position. This is the
# case with the default Vim omnifunc for C-family languages
# (ccomplete#Complete) which calls searchdecl to find a declaration. This
# function is supposed to move the cursor to the found declaration but it
# doesn't when called through the omni completion mapping (CTRL-X CTRL-O).
# So, we restore the cursor position after calling the omnifunc.
line, column = vimsupport.CurrentLineAndColumn()
omnifunc_call = [ self._omnifunc, omnifunc_call = [ self._omnifunc,
"(0,'", "(0,'",
vimsupport.EscapeForVim( request_data[ 'query' ] ), vimsupport.EscapeForVim( request_data[ 'query' ] ),
"')" ] "')" ]
items = vim.eval( ''.join( omnifunc_call ) ) items = vim.eval( ''.join( omnifunc_call ) )
vimsupport.SetCurrentLineAndColumn( line, column )
if isinstance( items, dict ) and 'words' in items: if isinstance( items, dict ) and 'words' in items:
items = items[ 'words' ] items = items[ 'words' ]

View File

@ -56,6 +56,12 @@ def CurrentLineAndColumn():
return line, column return line, column
def SetCurrentLineAndColumn( line, column ):
"""Sets the cursor position to the 0-based line and 0-based column."""
# Line from vim.current.window.cursor is 1-based.
vim.current.window.cursor = ( line + 1, column )
def CurrentColumn(): def CurrentColumn():
"""Returns the 0-based current column. Do NOT access the CurrentColumn in """Returns the 0-based current column. Do NOT access the CurrentColumn in
vim.current.line. It doesn't exist yet when the cursor is at the end of the vim.current.line. It doesn't exist yet when the cursor is at the end of the