From 83bb78a2d61ed59b8fe09ddb6e65eb4e3f7b25e1 Mon Sep 17 00:00:00 2001 From: micbou Date: Mon, 3 Jul 2017 14:52:39 +0200 Subject: [PATCH] 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. --- python/ycm/omni_completer.py | 11 ++++++++++- python/ycm/vimsupport.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/python/ycm/omni_completer.py b/python/ycm/omni_completer.py index 17872832..6d605938 100644 --- a/python/ycm/omni_completer.py +++ b/python/ycm/omni_completer.py @@ -90,13 +90,22 @@ class OmniCompleter( Completer ): # because it affects the value returned by 'query' 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, "(0,'", vimsupport.EscapeForVim( request_data[ 'query' ] ), "')" ] - items = vim.eval( ''.join( omnifunc_call ) ) + vimsupport.SetCurrentLineAndColumn( line, column ) + if isinstance( items, dict ) and 'words' in items: items = items[ 'words' ] diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index ac67fd1c..02565bc7 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -56,6 +56,12 @@ def CurrentLineAndColumn(): 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(): """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