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