2012-08-04 20:46:54 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import abc
|
2013-02-10 22:55:05 -05:00
|
|
|
import vim
|
|
|
|
import vimsupport
|
|
|
|
import ycm_core
|
|
|
|
|
|
|
|
|
|
|
|
class CompletionsCache( object ):
|
|
|
|
def __init__( self ):
|
|
|
|
self.line = -1
|
|
|
|
self.column = -1
|
|
|
|
self.raw_completions = []
|
|
|
|
self.filtered_completions = []
|
|
|
|
|
|
|
|
|
|
|
|
def CacheValid( self ):
|
|
|
|
completion_line, _ = vimsupport.CurrentLineAndColumn()
|
|
|
|
completion_column = int( vim.eval( "s:completion_start_column" ) )
|
|
|
|
return completion_line == self.line and completion_column == self.column
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Completer( object ):
|
|
|
|
__metaclass__ = abc.ABCMeta
|
|
|
|
|
|
|
|
|
|
|
|
def __init__( self ):
|
2013-01-23 20:23:51 -05:00
|
|
|
self.completions_future = None
|
2013-02-10 22:55:05 -05:00
|
|
|
self.completions_cache = None
|
|
|
|
|
|
|
|
|
|
|
|
def ShouldUseNow( self, start_column ):
|
|
|
|
inner_says_yes = self.ShouldUseNowInner( start_column )
|
|
|
|
previous_results_were_empty = ( self.completions_cache and
|
|
|
|
not self.completions_cache.raw_completions )
|
|
|
|
return inner_says_yes and not previous_results_were_empty
|
|
|
|
|
|
|
|
|
|
|
|
def ShouldUseNowInner( self, start_column ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def CandidatesForQueryAsync( self, query ):
|
|
|
|
if query and self.completions_cache and self.completions_cache.CacheValid():
|
|
|
|
self.completions_cache.filtered_completions = (
|
|
|
|
self.FilterAndSortCandidates(
|
|
|
|
self.completions_cache.raw_completions,
|
|
|
|
query ) )
|
|
|
|
else:
|
|
|
|
self.completions_cache = None
|
|
|
|
self.CandidatesForQueryAsyncInner( query )
|
|
|
|
|
|
|
|
|
|
|
|
def FilterAndSortCandidates( self, candidates, query ):
|
|
|
|
if not candidates:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if hasattr( candidates, 'words' ):
|
|
|
|
candidates = candidates.words
|
|
|
|
items_are_objects = 'word' in candidates[ 0 ]
|
|
|
|
|
|
|
|
return ycm_core.FilterAndSortCandidates(
|
|
|
|
candidates,
|
|
|
|
'word' if items_are_objects else '',
|
|
|
|
query )
|
|
|
|
|
|
|
|
|
|
|
|
def CandidatesForQueryAsyncInner( self, query ):
|
|
|
|
pass
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def AsyncCandidateRequestReady( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.completions_cache:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return self.AsyncCandidateRequestReadyInner()
|
|
|
|
|
|
|
|
|
|
|
|
def AsyncCandidateRequestReadyInner( self ):
|
2013-01-23 20:23:51 -05:00
|
|
|
if not self.completions_future:
|
2012-08-04 20:46:54 -04:00
|
|
|
# We return True so that the caller can extract the default value from the
|
|
|
|
# future
|
|
|
|
return True
|
2013-01-23 20:23:51 -05:00
|
|
|
return self.completions_future.ResultsReady()
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def CandidatesFromStoredRequest( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.completions_cache:
|
|
|
|
return self.completions_cache.filtered_completions
|
|
|
|
else:
|
|
|
|
self.completions_cache = CompletionsCache()
|
|
|
|
self.completions_cache.raw_completions = self.CandidatesFromStoredRequestInner()
|
|
|
|
self.completions_cache.line, _ = vimsupport.CurrentLineAndColumn()
|
|
|
|
self.completions_cache.column = int(
|
|
|
|
vim.eval( "s:completion_start_column" ) )
|
|
|
|
return self.completions_cache.raw_completions
|
|
|
|
|
|
|
|
|
|
|
|
def CandidatesFromStoredRequestInner( self ):
|
2013-01-23 20:23:51 -05:00
|
|
|
if not self.completions_future:
|
2012-08-04 20:46:54 -04:00
|
|
|
return []
|
2013-01-23 20:23:51 -05:00
|
|
|
return self.completions_future.GetResults()
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnFileReadyToParse( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnCursorMovedInsertMode( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnCursorMovedNormalMode( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnBufferVisit( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnCursorHold( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnInsertLeave( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def OnCurrentIdentifierFinished( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def DiagnosticsForCurrentFileReady( self ):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def GetDiagnosticsForCurrentFile( self ):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
def ShowDetailedDiagnostic( self ):
|
|
|
|
pass
|
|
|
|
|
2013-02-06 00:22:50 -05:00
|
|
|
def GettingCompletions( self ):
|
|
|
|
return False
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
@abc.abstractmethod
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
def DebugInfo( self ):
|
|
|
|
return ''
|