2014-01-13 14:08:43 -05:00
|
|
|
# Copyright (C) 2011, 2012, 2013 Google Inc.
|
2013-02-10 22:55:05 -05:00
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2017-03-09 09:57:27 -05:00
|
|
|
# Not installing aliases from python-future; it's unreliable and slow.
|
2016-02-27 19:12:24 -05:00
|
|
|
from builtins import * # noqa
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
import vim
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm import vimsupport
|
2017-07-07 08:11:45 -04:00
|
|
|
from ycmd import utils
|
2014-05-13 16:09:19 -04:00
|
|
|
from ycmd.completers.completer import Completer
|
2017-05-17 05:12:00 -04:00
|
|
|
from ycm.client.base_request import BaseRequest
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2013-03-22 13:22:50 -04:00
|
|
|
OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
|
|
|
|
OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
|
|
|
|
' list when expected.' )
|
|
|
|
|
2016-03-06 12:13:43 -05:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
class OmniCompleter( Completer ):
|
2013-09-02 17:45:53 -04:00
|
|
|
def __init__( self, user_options ):
|
|
|
|
super( OmniCompleter, self ).__init__( user_options )
|
2013-10-07 18:47:48 -04:00
|
|
|
self._omnifunc = None
|
2013-02-10 22:55:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2013-04-08 16:26:34 -04:00
|
|
|
def ShouldUseCache( self ):
|
2013-09-02 17:45:53 -04:00
|
|
|
return bool( self.user_options[ 'cache_omnifunc' ] )
|
2013-04-08 16:26:34 -04:00
|
|
|
|
|
|
|
|
2014-05-27 20:38:34 -04:00
|
|
|
def ShouldUseNow( self, request_data ):
|
2017-07-07 08:11:45 -04:00
|
|
|
self._omnifunc = utils.ToUnicode( vim.eval( '&omnifunc' ) )
|
2013-10-14 16:29:28 -04:00
|
|
|
if not self._omnifunc:
|
|
|
|
return False
|
2013-04-08 16:26:34 -04:00
|
|
|
if self.ShouldUseCache():
|
2013-09-06 02:43:14 -04:00
|
|
|
return super( OmniCompleter, self ).ShouldUseNow( request_data )
|
|
|
|
return self.ShouldUseNowInner( request_data )
|
2013-04-08 16:26:34 -04:00
|
|
|
|
2013-06-09 22:00:49 -04:00
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def ShouldUseNowInner( self, request_data ):
|
2018-03-26 10:51:07 -04:00
|
|
|
if request_data[ 'force_semantic' ]:
|
2017-02-04 15:46:54 -05:00
|
|
|
return True
|
2018-03-26 10:51:07 -04:00
|
|
|
disabled_filetypes = self.user_options[
|
|
|
|
'filetype_specific_completion_to_disable' ]
|
|
|
|
if not vimsupport.CurrentFiletypesEnabled( disabled_filetypes ):
|
|
|
|
return False
|
2013-09-06 02:43:14 -04:00
|
|
|
return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
|
2013-02-13 13:27:40 -05:00
|
|
|
|
|
|
|
|
2013-10-07 18:47:48 -04:00
|
|
|
def ComputeCandidates( self, request_data ):
|
2013-04-08 16:26:34 -04:00
|
|
|
if self.ShouldUseCache():
|
2016-04-24 09:22:52 -04:00
|
|
|
return super( OmniCompleter, self ).ComputeCandidates( request_data )
|
2016-11-30 15:06:51 -05:00
|
|
|
if self.ShouldUseNowInner( request_data ):
|
|
|
|
return self.ComputeCandidatesInner( request_data )
|
|
|
|
return []
|
2013-04-08 16:26:34 -04:00
|
|
|
|
2013-06-09 22:00:49 -04:00
|
|
|
|
2013-10-07 18:47:48 -04:00
|
|
|
def ComputeCandidatesInner( self, request_data ):
|
|
|
|
if not self._omnifunc:
|
|
|
|
return []
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2018-08-13 11:34:04 -04:00
|
|
|
# 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 the omnifunc calls.
|
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
|
|
|
|
2013-03-22 13:22:50 -04:00
|
|
|
try:
|
2018-08-09 08:42:12 -04:00
|
|
|
start_column = vimsupport.GetIntValue( self._omnifunc + '(1,"")' )
|
2018-08-16 11:21:50 -04:00
|
|
|
|
|
|
|
# Vim only stops completion if the value returned by the omnifunc is -3 or
|
|
|
|
# -2. In other cases, if the value is negative or greater than the current
|
|
|
|
# column, the start column is set to the current column; otherwise, the
|
|
|
|
# value is used as the start column.
|
|
|
|
if start_column in ( -3, -2 ):
|
2013-10-07 18:47:48 -04:00
|
|
|
return []
|
2018-08-16 11:21:50 -04:00
|
|
|
if start_column < 0 or start_column > column:
|
|
|
|
start_column = column
|
2013-03-22 13:22:50 -04:00
|
|
|
|
2017-06-27 17:40:11 -04:00
|
|
|
# Use the start column calculated by the omnifunc, rather than our own
|
|
|
|
# interpretation. This is important for certain languages where our
|
|
|
|
# identifier detection is either incorrect or not compatible with the
|
|
|
|
# behaviour of the omnifunc. Note: do this before calling the omnifunc
|
2018-08-09 08:42:12 -04:00
|
|
|
# because it affects the value returned by 'query'.
|
|
|
|
request_data[ 'start_column' ] = start_column + 1
|
2017-06-27 17:40:11 -04:00
|
|
|
|
2018-08-09 08:42:12 -04:00
|
|
|
# Vim internally moves the cursor to the start column before calling again
|
|
|
|
# the omnifunc. Some omnifuncs like the one defined by the
|
|
|
|
# LanguageClient-neovim plugin depend on this behavior to compute the list
|
|
|
|
# of candidates.
|
|
|
|
vimsupport.SetCurrentLineAndColumn( line, start_column )
|
|
|
|
|
2013-10-07 18:47:48 -04:00
|
|
|
omnifunc_call = [ self._omnifunc,
|
2013-03-22 13:22:50 -04:00
|
|
|
"(0,'",
|
2013-09-06 02:43:14 -04:00
|
|
|
vimsupport.EscapeForVim( request_data[ 'query' ] ),
|
2013-03-22 13:22:50 -04:00
|
|
|
"')" ]
|
|
|
|
items = vim.eval( ''.join( omnifunc_call ) )
|
2013-04-13 08:26:21 -04:00
|
|
|
|
2016-04-24 09:22:52 -04:00
|
|
|
if isinstance( items, dict ) and 'words' in items:
|
2013-10-07 18:47:48 -04:00
|
|
|
items = items[ 'words' ]
|
2016-03-25 23:40:17 -04:00
|
|
|
|
2013-03-22 13:22:50 -04:00
|
|
|
if not hasattr( items, '__iter__' ):
|
|
|
|
raise TypeError( OMNIFUNC_NOT_LIST )
|
|
|
|
|
2018-08-05 15:22:32 -04:00
|
|
|
# Vim allows each item of the list to be either a string or a dictionary
|
|
|
|
# but ycmd only supports lists where items are all strings or all
|
|
|
|
# dictionaries. Convert all strings into dictionaries.
|
|
|
|
for index, item in enumerate( items ):
|
|
|
|
if not isinstance( item, dict ):
|
|
|
|
items[ index ] = { 'word': item }
|
|
|
|
|
|
|
|
return items
|
2016-03-25 23:40:17 -04:00
|
|
|
|
2014-01-04 19:17:44 -05:00
|
|
|
except ( TypeError, ValueError, vim.error ) as error:
|
2013-03-22 13:22:50 -04:00
|
|
|
vimsupport.PostVimMessage(
|
|
|
|
OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
|
2013-10-07 18:47:48 -04:00
|
|
|
return []
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2018-08-13 11:34:04 -04:00
|
|
|
finally:
|
|
|
|
vimsupport.SetCurrentLineAndColumn( line, column )
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2016-02-24 16:43:03 -05:00
|
|
|
def FilterAndSortCandidatesInner( self, candidates, sort_property, query ):
|
|
|
|
request_data = {
|
|
|
|
'candidates': candidates,
|
|
|
|
'sort_property': sort_property,
|
|
|
|
'query': query
|
|
|
|
}
|
|
|
|
|
2018-04-05 11:53:54 -04:00
|
|
|
response = BaseRequest().PostDataToHandler( request_data,
|
|
|
|
'filter_and_sort_candidates' )
|
2018-04-17 11:39:25 -04:00
|
|
|
return response if response is not None else []
|