Add general completers support

This commit is contained in:
Stanislav Golovanov 2013-04-01 15:44:41 +04:00 committed by JazzCore
parent c3b7e55762
commit 48cda3bb8f
6 changed files with 250 additions and 14 deletions

View File

@ -431,7 +431,7 @@ function! s:CompletionsForQuery( query, use_filetype_completer,
if a:use_filetype_completer
py completer = ycm_state.GetFiletypeCompleter()
else
py completer = ycm_state.GetIdentifierCompleter()
py completer = ycm_state.GetGeneralCompleter()
endif
py completer.CandidatesForQueryAsync( vim.eval( 'a:query' ),
@ -480,7 +480,7 @@ function! youcompleteme#Complete( findstart, base )
\ s:completion_start_column . ')' )
if !s:should_use_filetype_completion &&
\ !pyeval( 'ycm_state.ShouldUseIdentifierCompleter(' .
\ !pyeval( 'ycm_state.ShouldUseGeneralCompleter(' .
\ s:completion_start_column . ')' )
" for vim, -2 means not found but don't trigger an error message
" see :h complete-functions
@ -544,7 +544,7 @@ function! s:CompleterCommand(...)
if a:1 == 'ft=ycm:omni'
py completer = ycm_state.GetOmniCompleter()
elseif a:1 == 'ft=ycm:ident'
py completer = ycm_state.GetIdentifierCompleter()
py completer = ycm_state.GetGeneralCompleter()
else
py completer = ycm_state.GetFiletypeCompleterForFiletype(
\ vim.eval('a:1').lstrip('ft=') )

View File

@ -21,6 +21,7 @@ import abc
import vim
import vimsupport
import ycm_core
import ycm
from collections import defaultdict
NO_USER_COMMANDS = 'This completer does not define any commands.'

View File

View File

@ -0,0 +1,214 @@
#!/usr/bin/env python
#
# Copyright (C) 2013 Stanislav Golovanov <stgolovanov@gmail.com>
#
# 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/>.
from completers.completer import Completer
from completers.all.identifier_completer import IdentifierCompleter
from threading import Thread, Event
import vimsupport
import inspect
import fnmatch
import os
class GeneralCompleterStore( Completer ):
"""
Main class that holds a list of completers that can be used in all filetypes.
This class creates a single GeneralCompleterInstance() class instance
for each general completer and makes a separate thread for each completer.
It overrides all Competer API methods so that specific calls to
GeneralCompleterStore are passed to all general completers.
This class doesnt maintain a cache because it will make a problems for
some completers like identifier completer. Caching is done in a general
completers itself.
"""
def __init__( self ):
super( GeneralCompleterStore, self ).__init__()
self.completers = self.InitCompleters()
self.query = None
self._candidates = []
self.threads = []
self.StartThreads()
def _start_completion_thread( self, completer ):
thread = Thread( target=self.SetCandidates, args=(completer,) )
thread.daemon = True
thread.start()
self.threads.append( thread )
def InitCompleters( self ):
# This method creates objects of main completers class.
completers = []
modules = [ module for module in os.listdir( '.' )
if fnmatch.fnmatch(module, '*.py')
and not 'general_completer' in module
and not '__init__' in module
and not 'hook' in module ]
for module in modules:
# We need to specify full path to the module
fullpath = 'completers.general.' + module[:-3]
try:
module = __import__( fullpath, fromlist=[''] )
except ImportError as error:
vimsupport.PostVimMessage( 'Import of general completer "{0}" has '
'failed, skipping. Full error: {1}'.format(
module, str( error ) ) )
continue
for _, ClassObject in inspect.getmembers( module, inspect.isclass ):
# Iterate over all classes in a module and select main class
if hasattr( ClassObject, 'CandidatesForQueryAsyncInner' ):
classInstance = ClassObject
# Init selected class and store class object
completers.append( GeneralCompleterInstance( classInstance() ) )
# append IdentifierCompleter
completers.append( GeneralCompleterInstance( IdentifierCompleter() ) )
return completers
def SupportedFiletypes( self ):
# magic token meaning all filetypes
return set( [ 'ycm_all' ] )
def ShouldUseNow( self, start_column ):
# Query all completers and set flag to True if any of completers returns
# True. Also update flags in completers classes
flag = False
for completer in self.completers:
ShouldUse = completer.completer.ShouldUseNow( start_column )
completer.ShouldUse = ShouldUse
if ShouldUse:
flag = True
return flag
def CandidatesForQueryAsync( self, query, start_column ):
self.query = query
self._candidates = []
# if completer should be used start thread by setting Event flag
for completer in self.completers:
completer.finished.clear()
if completer.ShouldUse and not completer.should_start.is_set():
completer.should_start.set()
def AsyncCandidateRequestReady( self ):
# Return True when all completers that should be used are finished their work.
for completer in self.completers:
if not completer.finished.is_set() and completer.ShouldUse:
return False
return True
def CandidatesFromStoredRequest( self ):
for completer in self.completers:
if completer.ShouldUse and completer.finished.is_set():
self._candidates += completer.results.pop()
return self._candidates
def SetCandidates( self, completer ):
while True:
# sleep until ShouldUseNow returns True
WaitAndClear( completer.should_start )
completer.completer.CandidatesForQueryAsync( self.query,
self.completion_start_column )
while not completer.completer.AsyncCandidateRequestReady():
continue
completer.results.append( completer.completer.CandidatesFromStoredRequest() )
completer.finished.set()
def StartThreads( self ):
for completer in self.completers:
self._start_completion_thread( completer )
def OnFileReadyToParse( self ):
# Process all parsing methods of completers. Needed by identifier completer
for completer in self.completers:
# clear all stored completion results
completer.results = []
completer.completer.OnFileReadyToParse()
def OnCursorMovedInsertMode( self ):
for completer in self.completers:
completer.completer.OnCursorMovedInsertMode()
def OnCursorMovedNormalMode( self ):
for completer in self.completers:
completer.completer.OnCursorMovedNormalMode()
def OnBufferVisit( self ):
for completer in self.completers:
completer.completer.OnBufferVisit()
def OnBufferDelete( self, deleted_buffer_file ):
for completer in self.completers:
completer.completer.OnBufferDelete( deleted_buffer_file )
def OnCursorHold( self ):
for completer in self.completers:
completer.completer.OnCursorHold()
def OnInsertLeave( self ):
for completer in self.completers:
completer.completer.OnInsertLeave()
class GeneralCompleterInstance( object ):
"""
Class that holds all meta information about specific general completer
"""
def __init__( self, completer ):
self.completer = completer
self.should_start = Event()
self.ShouldUse = False
self.finished = Event()
self.results = []
def WaitAndClear( event, timeout=None ):
flag_is_set = event.wait( timeout )
if flag_is_set:
event.clear()
return flag_is_set

View File

@ -0,0 +1,21 @@
# Copyright (C) 2013 Stanislav Golovanov <stgolovanov@gmail.com>
#
# 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/>.
from general_completer import GeneralCompleterStore
def GetCompleter():
return GeneralCompleterStore()

View File

@ -34,8 +34,8 @@ except ImportError as e:
os.path.dirname( os.path.abspath( __file__ ) ), str( e ) ) )
from completers.all.identifier_completer import IdentifierCompleter
from completers.all.omni_completer import OmniCompleter
from completers.general.general_completer import GeneralCompleterStore
FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval(
@ -44,13 +44,13 @@ FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval(
class YouCompleteMe( object ):
def __init__( self ):
self.identcomp = IdentifierCompleter()
self.gencomp = GeneralCompleterStore()
self.omnicomp = OmniCompleter()
self.filetype_completers = {}
def GetIdentifierCompleter( self ):
return self.identcomp
def GetGeneralCompleter( self ):
return self.gencomp
def GetOmniCompleter( self ):
@ -102,8 +102,8 @@ class YouCompleteMe( object ):
return completer
def ShouldUseIdentifierCompleter( self, start_column ):
return self.identcomp.ShouldUseNow( start_column )
def ShouldUseGeneralCompleter( self, start_column ):
return self.gencomp.ShouldUseNow( start_column )
def ShouldUseFiletypeCompleter( self, start_column ):
@ -133,21 +133,21 @@ class YouCompleteMe( object ):
def OnFileReadyToParse( self ):
self.identcomp.OnFileReadyToParse()
self.gencomp.OnFileReadyToParse()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnFileReadyToParse()
def OnBufferDelete( self, deleted_buffer_file ):
self.identcomp.OnBufferDelete( deleted_buffer_file )
self.gencomp.OnBufferDelete( deleted_buffer_file )
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnBufferDelete( deleted_buffer_file )
def OnInsertLeave( self ):
self.identcomp.OnInsertLeave()
self.gencomp.OnInsertLeave()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnInsertLeave()
@ -177,7 +177,7 @@ class YouCompleteMe( object ):
def OnCurrentIdentifierFinished( self ):
self.identcomp.OnCurrentIdentifierFinished()
self.gencomp.OnCurrentIdentifierFinished()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnCurrentIdentifierFinished()
@ -185,7 +185,7 @@ class YouCompleteMe( object ):
def DebugInfo( self ):
completers = set( self.filetype_completers.values() )
completers.add( self.identcomp )
completers.add( self.gencomp )
output = []
for completer in completers:
if not completer: