From 48cda3bb8f2ad089d1b5afbc899e960a6c966a7c Mon Sep 17 00:00:00 2001 From: Stanislav Golovanov Date: Mon, 1 Apr 2013 15:44:41 +0400 Subject: [PATCH 1/6] Add general completers support --- autoload/youcompleteme.vim | 6 +- python/completers/completer.py | 1 + python/completers/general/__init__.py | 0 .../completers/general/general_completer.py | 214 ++++++++++++++++++ python/completers/general/hook.py | 21 ++ python/ycm.py | 22 +- 6 files changed, 250 insertions(+), 14 deletions(-) create mode 100644 python/completers/general/__init__.py create mode 100644 python/completers/general/general_completer.py create mode 100644 python/completers/general/hook.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 69c5ce44..756aa733 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -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=') ) diff --git a/python/completers/completer.py b/python/completers/completer.py index d991e774..e5e18675 100644 --- a/python/completers/completer.py +++ b/python/completers/completer.py @@ -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.' diff --git a/python/completers/general/__init__.py b/python/completers/general/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/completers/general/general_completer.py b/python/completers/general/general_completer.py new file mode 100644 index 00000000..25a0c243 --- /dev/null +++ b/python/completers/general/general_completer.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Stanislav Golovanov +# +# 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 . + +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 diff --git a/python/completers/general/hook.py b/python/completers/general/hook.py new file mode 100644 index 00000000..e84cf950 --- /dev/null +++ b/python/completers/general/hook.py @@ -0,0 +1,21 @@ +# Copyright (C) 2013 Stanislav Golovanov +# +# 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 . + +from general_completer import GeneralCompleterStore + +def GetCompleter(): + return GeneralCompleterStore() diff --git a/python/ycm.py b/python/ycm.py index c8d346aa..1cdfc354 100644 --- a/python/ycm.py +++ b/python/ycm.py @@ -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: From 5d37c40feab2ca43cc8a452bf8729fd1d48fa0c9 Mon Sep 17 00:00:00 2001 From: Stanislav Golovanov Date: Thu, 11 Apr 2013 15:40:15 +0400 Subject: [PATCH 2/6] Add general UltiSnips snippet completer --- .../completers/general/ultisnips_completer.py | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 python/completers/general/ultisnips_completer.py diff --git a/python/completers/general/ultisnips_completer.py b/python/completers/general/ultisnips_completer.py new file mode 100644 index 00000000..367e4b08 --- /dev/null +++ b/python/completers/general/ultisnips_completer.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Stanislav Golovanov +# +# 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 . + +from completers.completer import Completer, CompletionsCache +from UltiSnips import UltiSnips_Manager +import vimsupport + +MIN_NUM_CHARS = int( vimsupport.GetVariableValue( + "g:ycm_min_num_of_chars_for_completion" ) ) + +class UltiSnipsCompleter( Completer ): + """ + General completer that provides UltiSnips snippet names in completions. + + This completer makes a cache of all snippets for filetype because each + call to _snips() is quite long and it is much faster to cache all snippets + once and then filter them. Cache is invalidated on buffer switching. + """ + + def __init__( self ): + super( UltiSnipsCompleter, self ).__init__() + self._candidates = 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 ): + query_length = vimsupport.CurrentColumn() - start_column + return query_length >= MIN_NUM_CHARS + + + def SupportedFiletypes( self ): + # magic token meaning all filetypes + return set( [ 'ycm_all' ] ) + + + def CandidatesForQueryAsync( self, query, start_column ): + self.completion_start_column = start_column + + if self.completions_cache: + self.completions_cache.filtered_completions = ( + self.FilterAndSortCandidates( + self.completions_cache.raw_completions, + query ) ) + else: + self.completions_cache = None + self.CandidatesForQueryAsyncInner( query, start_column ) + + + def CandidatesForQueryAsyncInner( self, query, start_column ): + self._query = query + + + def AsyncCandidateRequestReadyInner( self ): + return self.flag + + + # We need to override this because we need to store all snippets but return + # filtered results on first call. + def CandidatesFromStoredRequest( self ): + 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 = self.completion_start_column + return self.FilterAndSortCandidates( self._candidates, self._query ) + + + def CandidatesFromStoredRequestInner( self ): + return self._candidates + + + def SetCandidates( self ): + try: + # get all snippets for filetype + rawsnips = UltiSnips_Manager._snips( '', 1 ) + self._candidates = [ { 'word': str( snip.trigger ), + 'menu': str( ' ' + snip.description ) } + for snip in rawsnips ] + except: + self._candidates = [] + self.flag = True + + + def OnFileReadyToParse( self ): + # Invalidate cache on buffer switch + self.completions_cache = None + self.SetCandidates() From bb5839dd74c76e502572e54a50b48eb56c785b2b Mon Sep 17 00:00:00 2001 From: Stanislav Golovanov Date: Thu, 11 Apr 2013 15:42:36 +0400 Subject: [PATCH 3/6] Add general filename completer & other stuff --- python/completers/all/identifier_completer.py | 14 +-- python/completers/completer.py | 48 +++++++++- .../completers/general/filename_completer.py | 94 +++++++++++++++++++ .../completers/general/general_completer.py | 78 +++++++-------- python/completers/general/hook.py | 21 ----- .../completers/general/ultisnips_completer.py | 37 +++----- 6 files changed, 184 insertions(+), 108 deletions(-) create mode 100644 python/completers/general/filename_completer.py delete mode 100644 python/completers/general/hook.py diff --git a/python/completers/all/identifier_completer.py b/python/completers/all/identifier_completer.py index fcd3f07f..ba0e82e0 100644 --- a/python/completers/all/identifier_completer.py +++ b/python/completers/all/identifier_completer.py @@ -17,32 +17,24 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import Completer +from completers.completer import GeneralCompleter import vim import vimsupport import ycm_core import ycm_utils as utils MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10 -MIN_NUM_CHARS = int( vimsupport.GetVariableValue( - "g:ycm_min_num_of_chars_for_completion" ) ) -class IdentifierCompleter( Completer ): +class IdentifierCompleter( GeneralCompleter ): def __init__( self ): super( IdentifierCompleter, self ).__init__() self.completer = ycm_core.IdentifierCompleter() self.completer.EnableThreading() - def SupportedFiletypes( self ): - # magic token meaning all filetypes - return set( [ 'ycm_all' ] ) - - def ShouldUseNow( self, start_column ): - query_length = vimsupport.CurrentColumn() - start_column - return query_length >= MIN_NUM_CHARS + return self.QueryLengthAboveMinThreshold( start_column ) def CandidatesForQueryAsync( self, query, unused_start_column ): diff --git a/python/completers/completer.py b/python/completers/completer.py index e5e18675..9a3868de 100644 --- a/python/completers/completer.py +++ b/python/completers/completer.py @@ -21,11 +21,14 @@ import abc import vim import vimsupport import ycm_core -import ycm from collections import defaultdict +from threading import Event NO_USER_COMMANDS = 'This completer does not define any commands.' +MIN_NUM_CHARS = int( vimsupport.GetVariableValue( + "g:ycm_min_num_of_chars_for_completion" ) ) + class Completer( object ): """A base class for all Completers in YCM. @@ -154,6 +157,11 @@ class Completer( object ): return False + def QueryLengthAboveMinThreshold( self, start_column ): + query_length = vimsupport.CurrentColumn() - start_column + return query_length >= MIN_NUM_CHARS + + # It's highly likely you DON'T want to override this function but the *Inner # version of it. def CandidatesForQueryAsync( self, query, start_column ): @@ -178,10 +186,15 @@ class Completer( object ): candidates = candidates.words items_are_objects = 'word' in candidates[ 0 ] - return ycm_core.FilterAndSortCandidates( - candidates, - 'word' if items_are_objects else '', - query ) + try: + matches = ycm_core.FilterAndSortCandidates( + candidates, + 'word' if items_are_objects else '', + query ) + except: + matches = [] + + return matches def CandidatesForQueryAsyncInner( self, query, start_column ): @@ -295,6 +308,31 @@ class Completer( object ): return '' +class GeneralCompleter( Completer ): + """ + A base class for General completers in YCM. + + Because this is a subclass of Completer class, you should refer to the + dpcumentation of Completer API. + + Only exception is that GeneralCompleterStore class that collects and controls + all general completers already adds threading for completers, so there + is no need to add a threading to new general completers. + + added __init__ fields are for GeneralCompleterStore internal use only. + """ + def __init__( self ): + super( GeneralCompleter, self ).__init__() + self._should_start = Event() + self._should_use = False + self._finished = Event() + self._results = [] + + + def SupportedFiletypes( self ): + return set() + + class CompletionsCache( object ): def __init__( self ): self.line = -1 diff --git a/python/completers/general/filename_completer.py b/python/completers/general/filename_completer.py new file mode 100644 index 00000000..f1d1d158 --- /dev/null +++ b/python/completers/general/filename_completer.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Stanislav Golovanov +# +# +# 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 . + +from completers.completer import GeneralCompleter, CompletionsCache +import vimsupport +import ycm_core +import vim +import os +import re + + +class FilenameCompleter( GeneralCompleter ): + """ + General completer that provides filename and filepath completions. + + It maintains a cache of completions which is invalidated on each '/' symbol. + """ + def __init__(self): + super( FilenameCompleter, self ).__init__() + self._candidates = [] + self._query = None + self._should_use = False + + # TODO look into vim-style path globbing, NCC has a nice implementation + self._path_regex = re.compile( """(?:[A-z]+:/|[/~]|\./|\.+/)+ # 1 or more 'D:/'-like token or '/' or '~' or './' or '../' + (?:[ /a-zA-Z0-9()$+_~.\x80-\xff-\[\]]| # any alphanumeric symbal and space literal + [^\x20-\x7E]| # skip any special symbols + \\.)* # backslash and 1 char after it. + matches 1 or more of whole group + """, re.X ) + + + def ShouldUseNowInner( self, start_column ): + token = vim.current.line[ start_column - 1 ] + if token == '/' or self._should_use: + self._should_use = True + return True + else: + return False + + + def CandidatesForQueryAsyncInner( self, query, start_column ): + self._candidates = [] + self._query = query + self._completions_ready = False + self.line = str( vim.current.line.strip() ) + self.SetCandidates() + + + def AsyncCandidateRequestReadyInner( self ): + return self._completions_ready + + + def OnInsertLeave( self ): + # TODO this a hackish way to keep results when typing 2-3rd char after slash + # because identifier completer will kick in and replace results for 1 char. + # Need to do something better + self._should_use = False + + + def CandidatesFromStoredRequestInner( self ): + return self._candidates + + + def SetCandidates( self ): + path = self._path_regex.search( self.line ) + self._working_dir = os.path.expanduser( path.group() ) if path else '' + + try: + paths = os.listdir( self._working_dir ) + except: + paths = [] + + self._candidates = [ {'word': path, + 'dup': 1, + 'menu': '[Dir]' if os.path.isdir( self._working_dir + \ + '/' + path ) else '[File]' + } for path in paths ] + + self._completions_ready = True diff --git a/python/completers/general/general_completer.py b/python/completers/general/general_completer.py index 25a0c243..d625c8fe 100644 --- a/python/completers/general/general_completer.py +++ b/python/completers/general/general_completer.py @@ -19,7 +19,7 @@ from completers.completer import Completer from completers.all.identifier_completer import IdentifierCompleter -from threading import Thread, Event +from threading import Thread import vimsupport import inspect import fnmatch @@ -58,11 +58,10 @@ class GeneralCompleterStore( Completer ): def InitCompleters( self ): # This method creates objects of main completers class. completers = [] - modules = [ module for module in os.listdir( '.' ) + modules = [ module for module in os.listdir( os.path.dirname(__file__) ) if fnmatch.fnmatch(module, '*.py') and not 'general_completer' in module - and not '__init__' in module - and not 'hook' in module ] + and not '__init__' in module ] for module in modules: @@ -79,20 +78,21 @@ class GeneralCompleterStore( Completer ): for _, ClassObject in inspect.getmembers( module, inspect.isclass ): # Iterate over all classes in a module and select main class - if hasattr( ClassObject, 'CandidatesForQueryAsyncInner' ): + if not __name__ in str(ClassObject) and 'general' in str(ClassObject): + classInstance = ClassObject - # Init selected class and store class object - completers.append( GeneralCompleterInstance( classInstance() ) ) - # append IdentifierCompleter - completers.append( GeneralCompleterInstance( IdentifierCompleter() ) ) + # Init selected class and store class object + completers.append( classInstance() ) + + completers.append( IdentifierCompleter() ) + return completers def SupportedFiletypes( self ): - # magic token meaning all filetypes - return set( [ 'ycm_all' ] ) + return set() def ShouldUseNow( self, start_column ): @@ -100,9 +100,9 @@ class GeneralCompleterStore( Completer ): # 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: + _should_use = completer.ShouldUseNow( start_column ) + completer._should_use = _should_use + if _should_use: flag = True return flag @@ -114,23 +114,23 @@ class GeneralCompleterStore( Completer ): # 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() + completer._finished.clear() + if completer._should_use 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: + if not completer._finished.is_set() and completer._should_use: 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() + if completer._should_use and completer._finished.is_set(): + self._candidates += completer._results.pop() return self._candidates @@ -139,17 +139,17 @@ class GeneralCompleterStore( Completer ): while True: # sleep until ShouldUseNow returns True - WaitAndClear( completer.should_start ) + WaitAndClear( completer._should_start ) - completer.completer.CandidatesForQueryAsync( self.query, + completer.CandidatesForQueryAsync( self.query, self.completion_start_column ) - while not completer.completer.AsyncCandidateRequestReady(): + while not completer.AsyncCandidateRequestReady(): continue - completer.results.append( completer.completer.CandidatesFromStoredRequest() ) + completer._results.append( completer.CandidatesFromStoredRequest() ) - completer.finished.set() + completer._finished.set() def StartThreads( self ): @@ -161,50 +161,38 @@ class GeneralCompleterStore( Completer ): # 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() + completer._results = [] + completer.OnFileReadyToParse() def OnCursorMovedInsertMode( self ): for completer in self.completers: - completer.completer.OnCursorMovedInsertMode() + completer.OnCursorMovedInsertMode() def OnCursorMovedNormalMode( self ): for completer in self.completers: - completer.completer.OnCursorMovedNormalMode() + completer.OnCursorMovedNormalMode() def OnBufferVisit( self ): for completer in self.completers: - completer.completer.OnBufferVisit() + completer.OnBufferVisit() def OnBufferDelete( self, deleted_buffer_file ): for completer in self.completers: - completer.completer.OnBufferDelete( deleted_buffer_file ) + completer.OnBufferDelete( deleted_buffer_file ) def OnCursorHold( self ): for completer in self.completers: - completer.completer.OnCursorHold() + 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 = [] + completer.OnInsertLeave() def WaitAndClear( event, timeout=None ): diff --git a/python/completers/general/hook.py b/python/completers/general/hook.py deleted file mode 100644 index e84cf950..00000000 --- a/python/completers/general/hook.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright (C) 2013 Stanislav Golovanov -# -# 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 . - -from general_completer import GeneralCompleterStore - -def GetCompleter(): - return GeneralCompleterStore() diff --git a/python/completers/general/ultisnips_completer.py b/python/completers/general/ultisnips_completer.py index 367e4b08..1c31f7de 100644 --- a/python/completers/general/ultisnips_completer.py +++ b/python/completers/general/ultisnips_completer.py @@ -17,14 +17,12 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import Completer, CompletionsCache +from completers.completer import GeneralCompleter, CompletionsCache from UltiSnips import UltiSnips_Manager import vimsupport -MIN_NUM_CHARS = int( vimsupport.GetVariableValue( - "g:ycm_min_num_of_chars_for_completion" ) ) -class UltiSnipsCompleter( Completer ): +class UltiSnipsCompleter( GeneralCompleter ): """ General completer that provides UltiSnips snippet names in completions. @@ -47,15 +45,11 @@ class UltiSnipsCompleter( Completer ): def ShouldUseNowInner( self, start_column ): - query_length = vimsupport.CurrentColumn() - start_column - return query_length >= MIN_NUM_CHARS - - - def SupportedFiletypes( self ): - # magic token meaning all filetypes - return set( [ 'ycm_all' ] ) + return self.QueryLengthAboveMinThreshold( start_column ) + # We need to override this because Completer version invalidates cache on + # empty query and we want to invalidate cache only on buffer switch. def CandidatesForQueryAsync( self, query, start_column ): self.completion_start_column = start_column @@ -77,27 +71,17 @@ class UltiSnipsCompleter( Completer ): return self.flag - # We need to override this because we need to store all snippets but return - # filtered results on first call. - def CandidatesFromStoredRequest( self ): - 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 = self.completion_start_column - return self.FilterAndSortCandidates( self._candidates, self._query ) - - def CandidatesFromStoredRequestInner( self ): return self._candidates def SetCandidates( self ): try: - # get all snippets for filetype rawsnips = UltiSnips_Manager._snips( '', 1 ) + + # UltiSnips_Manager._snips() returns a class instance where: + # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) + # class.description - description of the snippet self._candidates = [ { 'word': str( snip.trigger ), 'menu': str( ' ' + snip.description ) } for snip in rawsnips ] @@ -108,5 +92,6 @@ class UltiSnipsCompleter( Completer ): def OnFileReadyToParse( self ): # Invalidate cache on buffer switch - self.completions_cache = None + self.completions_cache = CompletionsCache() self.SetCandidates() + self.completions_cache.raw_completions = self._candidates From aa9127e3dcb8654a44b5c17471910912300907d3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 22 Apr 2013 21:23:49 -0700 Subject: [PATCH 4/6] Rewriting the code from the pull request This implements the filename completer and introduces integration with UltiSnips. The user will now see snippets in the completion menu. After selecting a snippet, the user should invoke the UltiSnips trigger key (which should be changed from the default of TAB) to trigger the snippet expansion. Fixes #77, Fixes #36 --- python/completers/all/identifier_completer.py | 2 + python/completers/completer.py | 28 +-- .../completers/general/filename_completer.py | 74 ++++---- .../completers/general/general_completer.py | 177 ++++++------------ .../completers/general/ultisnips_completer.py | 79 +++----- 5 files changed, 120 insertions(+), 240 deletions(-) diff --git a/python/completers/all/identifier_completer.py b/python/completers/all/identifier_completer.py index ba0e82e0..820fd518 100644 --- a/python/completers/all/identifier_completer.py +++ b/python/completers/all/identifier_completer.py @@ -24,6 +24,8 @@ import ycm_core import ycm_utils as utils MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10 +MIN_NUM_CHARS = int( vimsupport.GetVariableValue( + "g:ycm_min_num_of_chars_for_completion" ) ) class IdentifierCompleter( GeneralCompleter ): diff --git a/python/completers/completer.py b/python/completers/completer.py index 9a3868de..56649f2d 100644 --- a/python/completers/completer.py +++ b/python/completers/completer.py @@ -22,7 +22,6 @@ import vim import vimsupport import ycm_core from collections import defaultdict -from threading import Event NO_USER_COMMANDS = 'This completer does not define any commands.' @@ -161,7 +160,6 @@ class Completer( object ): query_length = vimsupport.CurrentColumn() - start_column return query_length >= MIN_NUM_CHARS - # It's highly likely you DON'T want to override this function but the *Inner # version of it. def CandidatesForQueryAsync( self, query, start_column ): @@ -186,13 +184,10 @@ class Completer( object ): candidates = candidates.words items_are_objects = 'word' in candidates[ 0 ] - try: - matches = ycm_core.FilterAndSortCandidates( - candidates, - 'word' if items_are_objects else '', - query ) - except: - matches = [] + matches = ycm_core.FilterAndSortCandidates( + candidates, + 'word' if items_are_objects else '', + query ) return matches @@ -310,23 +305,16 @@ class Completer( object ): class GeneralCompleter( Completer ): """ - A base class for General completers in YCM. + A base class for General completers in YCM. A general completer is used in all + filetypes. Because this is a subclass of Completer class, you should refer to the - dpcumentation of Completer API. + Completer class documentation. Do NOT use this class for semantic completers! + Subclass Completer directly. - Only exception is that GeneralCompleterStore class that collects and controls - all general completers already adds threading for completers, so there - is no need to add a threading to new general completers. - - added __init__ fields are for GeneralCompleterStore internal use only. """ def __init__( self ): super( GeneralCompleter, self ).__init__() - self._should_start = Event() - self._should_use = False - self._finished = Event() - self._results = [] def SupportedFiletypes( self ): diff --git a/python/completers/general/filename_completer.py b/python/completers/general/filename_completer.py index f1d1d158..d3e5534b 100644 --- a/python/completers/general/filename_completer.py +++ b/python/completers/general/filename_completer.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # # Copyright (C) 2013 Stanislav Golovanov -# +# Strahinja Val Markovic # # YouCompleteMe is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,9 +16,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import GeneralCompleter, CompletionsCache -import vimsupport -import ycm_core +from completers.completer import GeneralCompleter import vim import os import re @@ -27,68 +25,60 @@ import re class FilenameCompleter( GeneralCompleter ): """ General completer that provides filename and filepath completions. - - It maintains a cache of completions which is invalidated on each '/' symbol. """ + def __init__(self): super( FilenameCompleter, self ).__init__() self._candidates = [] - self._query = None - self._should_use = False - # TODO look into vim-style path globbing, NCC has a nice implementation - self._path_regex = re.compile( """(?:[A-z]+:/|[/~]|\./|\.+/)+ # 1 or more 'D:/'-like token or '/' or '~' or './' or '../' - (?:[ /a-zA-Z0-9()$+_~.\x80-\xff-\[\]]| # any alphanumeric symbal and space literal - [^\x20-\x7E]| # skip any special symbols - \\.)* # backslash and 1 char after it. + matches 1 or more of whole group - """, re.X ) + self._path_regex = re.compile(""" + # 1 or more 'D:/'-like token or '/' or '~' or './' or '../' + (?:[A-z]+:/|[/~]|\./|\.+/)+ + + # any alphanumeric symbal and space literal + (?:[ /a-zA-Z0-9()$+_~.\x80-\xff-\[\]]| + + # skip any special symbols + [^\x20-\x7E]| + + # backslash and 1 char after it. + matches 1 or more of whole group + \\.)*$ + """, re.X ) def ShouldUseNowInner( self, start_column ): - token = vim.current.line[ start_column - 1 ] - if token == '/' or self._should_use: - self._should_use = True - return True - else: - return False + return vim.current.line[ start_column - 1 ] == '/' def CandidatesForQueryAsyncInner( self, query, start_column ): self._candidates = [] - self._query = query - self._completions_ready = False - self.line = str( vim.current.line.strip() ) - self.SetCandidates() + self.ComputePaths( start_column ) def AsyncCandidateRequestReadyInner( self ): - return self._completions_ready - - - def OnInsertLeave( self ): - # TODO this a hackish way to keep results when typing 2-3rd char after slash - # because identifier completer will kick in and replace results for 1 char. - # Need to do something better - self._should_use = False + return True def CandidatesFromStoredRequestInner( self ): return self._candidates - def SetCandidates( self ): - path = self._path_regex.search( self.line ) - self._working_dir = os.path.expanduser( path.group() ) if path else '' + def ComputePaths( self, start_column ): + def GenerateCandidateForPath( path, path_dir ): + is_dir = os.path.isdir( os.path.join( path_dir, path ) ) + return { 'word': path, + 'dup': 1, + 'menu': '[Dir]' if is_dir else '[File]' } + + line = vim.current.line[ :start_column ] + match = self._path_regex.search( line ) + path_dir = os.path.expanduser( match.group() ) if match else '' try: - paths = os.listdir( self._working_dir ) + paths = os.listdir( path_dir ) except: paths = [] - self._candidates = [ {'word': path, - 'dup': 1, - 'menu': '[Dir]' if os.path.isdir( self._working_dir + \ - '/' + path ) else '[File]' - } for path in paths ] - + self._candidates = [ GenerateCandidateForPath( path, path_dir ) for path + in paths ] self._completions_ready = True diff --git a/python/completers/general/general_completer.py b/python/completers/general/general_completer.py index d625c8fe..0817055d 100644 --- a/python/completers/general/general_completer.py +++ b/python/completers/general/general_completer.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # # Copyright (C) 2013 Stanislav Golovanov +# Strahinja Val Markovic # # This file is part of YouCompleteMe. # @@ -19,76 +20,38 @@ from completers.completer import Completer from completers.all.identifier_completer import IdentifierCompleter -from threading import Thread -import vimsupport -import inspect -import fnmatch -import os +from filename_completer import FilenameCompleter + +try: + from ultisnips_completer import UltiSnipsCompleter + USE_ULTISNIPS_COMPLETER = True +except ImportError: + USE_ULTISNIPS_COMPLETER = False + 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. + Holds a list of completers that can be used in all filetypes. 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( os.path.dirname(__file__) ) - if fnmatch.fnmatch(module, '*.py') - and not 'general_completer' in module - and not '__init__' 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 not __name__ in str(ClassObject) and 'general' in str(ClassObject): - - classInstance = ClassObject - - - # Init selected class and store class object - completers.append( classInstance() ) - - completers.append( IdentifierCompleter() ) - - return completers + self._identifier_completer = IdentifierCompleter() + self._filename_completer = FilenameCompleter() + self._ultisnips_completer = ( UltiSnipsCompleter() + if USE_ULTISNIPS_COMPLETER else None ) + self._non_filename_completers = filter( lambda x: x, + [ self._ultisnips_completer, + self._identifier_completer ] ) + self._all_completers = filter( lambda x: x, + [ self._identifier_completer, + self._filename_completer, + self._ultisnips_completer ] ) + self._current_query_completers = [] def SupportedFiletypes( self ): @@ -96,107 +59,73 @@ class GeneralCompleterStore( Completer ): 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: - _should_use = completer.ShouldUseNow( start_column ) - completer._should_use = _should_use - if _should_use: - flag = True + self._current_query_completers = [] - return flag + if self._filename_completer.ShouldUseNow( start_column ): + self._current_query_completers = [ self._filename_completer ] + return True + + should_use_now = False + + for completer in self._non_filename_completers: + should_use_this_completer = completer.ShouldUseNow( start_column ) + should_use_now = should_use_now or should_use_this_completer + + if should_use_this_completer: + self._current_query_completers.append( completer ) + + return should_use_now 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._should_use and not completer._should_start.is_set(): - completer._should_start.set() + for completer in self._current_query_completers: + completer.CandidatesForQueryAsync( query, start_column ) 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._should_use: - return False - return True + return all( x.AsyncCandidateRequestReady() for x in + self._current_query_completers ) def CandidatesFromStoredRequest( self ): - for completer in self.completers: - if completer._should_use and completer._finished.is_set(): - self._candidates += completer._results.pop() + candidates = [] + for completer in self._current_query_completers: + candidates += completer.CandidatesFromStoredRequest() - return self._candidates - - - def SetCandidates( self, completer ): - while True: - - # sleep until ShouldUseNow returns True - WaitAndClear( completer._should_start ) - - completer.CandidatesForQueryAsync( self.query, - self.completion_start_column ) - - while not completer.AsyncCandidateRequestReady(): - continue - - completer._results.append( completer.CandidatesFromStoredRequest() ) - - completer._finished.set() - - - def StartThreads( self ): - for completer in self.completers: - self._start_completion_thread( completer ) + return candidates 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 = [] + for completer in self._all_completers: completer.OnFileReadyToParse() def OnCursorMovedInsertMode( self ): - for completer in self.completers: + for completer in self._all_completers: completer.OnCursorMovedInsertMode() def OnCursorMovedNormalMode( self ): - for completer in self.completers: + for completer in self._all_completers: completer.OnCursorMovedNormalMode() def OnBufferVisit( self ): - for completer in self.completers: + for completer in self._all_completers: completer.OnBufferVisit() def OnBufferDelete( self, deleted_buffer_file ): - for completer in self.completers: + for completer in self._all_completers: completer.OnBufferDelete( deleted_buffer_file ) def OnCursorHold( self ): - for completer in self.completers: + for completer in self._all_completers: completer.OnCursorHold() def OnInsertLeave( self ): - for completer in self.completers: + for completer in self._all_completers: completer.OnInsertLeave() - -def WaitAndClear( event, timeout=None ): - flag_is_set = event.wait( timeout ) - if flag_is_set: - event.clear() - return flag_is_set diff --git a/python/completers/general/ultisnips_completer.py b/python/completers/general/ultisnips_completer.py index 1c31f7de..0c58a08c 100644 --- a/python/completers/general/ultisnips_completer.py +++ b/python/completers/general/ultisnips_completer.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # # Copyright (C) 2013 Stanislav Golovanov +# Strahinja Val Markovic # # This file is part of YouCompleteMe. # @@ -17,81 +18,51 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import GeneralCompleter, CompletionsCache +from completers.completer import GeneralCompleter from UltiSnips import UltiSnips_Manager -import vimsupport class UltiSnipsCompleter( GeneralCompleter ): """ General completer that provides UltiSnips snippet names in completions. - - This completer makes a cache of all snippets for filetype because each - call to _snips() is quite long and it is much faster to cache all snippets - once and then filter them. Cache is invalidated on buffer switching. """ def __init__( self ): super( UltiSnipsCompleter, self ).__init__() self._candidates = 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 + self._filtered_candidates = None def ShouldUseNowInner( self, start_column ): return self.QueryLengthAboveMinThreshold( start_column ) - # We need to override this because Completer version invalidates cache on - # empty query and we want to invalidate cache only on buffer switch. - def CandidatesForQueryAsync( self, query, start_column ): - self.completion_start_column = start_column - - if self.completions_cache: - self.completions_cache.filtered_completions = ( - self.FilterAndSortCandidates( - self.completions_cache.raw_completions, - query ) ) - else: - self.completions_cache = None - self.CandidatesForQueryAsyncInner( query, start_column ) + def CandidatesForQueryAsync( self, query, unused_start_column ): + self._filtered_candidates = self.FilterAndSortCandidates( self._candidates, + query ) - def CandidatesForQueryAsyncInner( self, query, start_column ): - self._query = query + def AsyncCandidateRequestReady( self ): + return bool( self._candidates ) - def AsyncCandidateRequestReadyInner( self ): - return self.flag - - - def CandidatesFromStoredRequestInner( self ): - return self._candidates - - - def SetCandidates( self ): - try: - rawsnips = UltiSnips_Manager._snips( '', 1 ) - - # UltiSnips_Manager._snips() returns a class instance where: - # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) - # class.description - description of the snippet - self._candidates = [ { 'word': str( snip.trigger ), - 'menu': str( ' ' + snip.description ) } - for snip in rawsnips ] - except: - self._candidates = [] - self.flag = True + def CandidatesFromStoredRequest( self ): + return self._filtered_candidates def OnFileReadyToParse( self ): - # Invalidate cache on buffer switch - self.completions_cache = CompletionsCache() - self.SetCandidates() - self.completions_cache.raw_completions = self._candidates + self._candidates = _GetCandidates() + + +def _GetCandidates(): + try: + rawsnips = UltiSnips_Manager._snips( '', 1 ) + + # UltiSnips_Manager._snips() returns a class instance where: + # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) + # class.description - description of the snippet + return [ { 'word': str( snip.trigger ), + 'menu': str( ' ' + snip.description ) } + for snip in rawsnips ] + except: + return [] From 8d20637295ede470afa4bb01fc4663fc994c11ef Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 22 Apr 2013 21:44:18 -0700 Subject: [PATCH 5/6] User can now pick the root for relative paths --- README.md | 16 ++++++++++++++++ plugin/youcompleteme.vim | 3 +++ python/completers/general/filename_completer.py | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/README.md b/README.md index c554ad49..85f9a4f1 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ YCM also provides semantic go-to-definition/declaration commands for C-family languages. Expect more IDE features powered by the various YCM semantic engines in the future. +You'll also find that YCM has filepath completers (try typing `./` in a file) +and a completer that integrates with [UltiSnips][]. + + Mac OS X super-quick installation --------------------------------- @@ -861,6 +865,17 @@ Default: `[]` let g:ycm_extra_conf_globlist = [] +### The `g:ycm_filepath_completion_use_working_dir` option + +By default, YCM's filepath completion will interpret relative paths like `../` +as being relative to the folder of the file of the currently active buffer. +Setting this option will force YCM to always interpret relative paths as being +relative to Vim's current working directory. + +Default: `0` + + let g:ycm_filepath_completion_use_working_dir = 0 + ### The `g:ycm_semantic_triggers` option This option controls the character-based triggers for the various semantic @@ -1140,3 +1155,4 @@ This software is licensed under the [GPL v3 license][gpl]. [win-wiki]: https://github.com/Valloric/YouCompleteMe/wiki/Windows-Installation-Guide [eclim]: http://eclim.org/ [jedi]: https://github.com/davidhalter/jedi +[ultisnips]: https://github.com/SirVer/ultisnips/blob/master/doc/UltiSnips.txt diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index b846aefc..006e8dd3 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -121,6 +121,9 @@ let g:ycm_confirm_extra_conf = let g:ycm_extra_conf_globlist = \ get( g:, 'ycm_extra_conf_globlist', [] ) +let g:ycm_filepath_completion_use_working_dir = + \ get( g:, 'ycm_filepath_completion_use_working_dir', 0 ) + let g:ycm_semantic_triggers = \ get( g:, 'ycm_semantic_triggers', { \ 'c' : ['->', '.'], diff --git a/python/completers/general/filename_completer.py b/python/completers/general/filename_completer.py index d3e5534b..e2c524e9 100644 --- a/python/completers/general/filename_completer.py +++ b/python/completers/general/filename_completer.py @@ -18,9 +18,12 @@ from completers.completer import GeneralCompleter import vim +import vimsupport import os import re +USE_WORKING_DIR = vimsupport.GetBoolValue( + 'g:ycm_filepath_completion_use_working_dir' ) class FilenameCompleter( GeneralCompleter ): """ @@ -74,6 +77,10 @@ class FilenameCompleter( GeneralCompleter ): match = self._path_regex.search( line ) path_dir = os.path.expanduser( match.group() ) if match else '' + if not USE_WORKING_DIR and not path_dir.startswith( '/' ): + path_dir = os.path.join( os.path.dirname( vim.current.buffer.name ), + path_dir ) + try: paths = os.listdir( path_dir ) except: From 196228217ff138aab75b327679e77a7fdf2cfbf3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 22 Apr 2013 22:19:26 -0700 Subject: [PATCH 6/6] Filename completer now uses threads This was done by introducing a new ThreadedCompleter class that descends from Completer. Both JediCompleter and FilenameCompleter descend from ThreadedCompleter. --- python/completers/all/identifier_completer.py | 2 +- python/completers/completer.py | 18 ---- .../completers/general/filename_completer.py | 24 ++---- ...ompleter.py => general_completer_store.py} | 0 .../completers/general/ultisnips_completer.py | 2 +- python/completers/general_completer.py | 37 +++++++++ python/completers/python/jedi_completer.py | 69 ++++----------- python/completers/threaded_completer.py | 83 +++++++++++++++++++ python/ycm.py | 2 +- python/ycm_utils.py | 1 + 10 files changed, 144 insertions(+), 94 deletions(-) rename python/completers/general/{general_completer.py => general_completer_store.py} (100%) create mode 100644 python/completers/general_completer.py create mode 100644 python/completers/threaded_completer.py diff --git a/python/completers/all/identifier_completer.py b/python/completers/all/identifier_completer.py index 820fd518..858fd34e 100644 --- a/python/completers/all/identifier_completer.py +++ b/python/completers/all/identifier_completer.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import GeneralCompleter +from completers.general_completer import GeneralCompleter import vim import vimsupport import ycm_core diff --git a/python/completers/completer.py b/python/completers/completer.py index 56649f2d..7ebffb24 100644 --- a/python/completers/completer.py +++ b/python/completers/completer.py @@ -303,24 +303,6 @@ class Completer( object ): return '' -class GeneralCompleter( Completer ): - """ - A base class for General completers in YCM. A general completer is used in all - filetypes. - - Because this is a subclass of Completer class, you should refer to the - Completer class documentation. Do NOT use this class for semantic completers! - Subclass Completer directly. - - """ - def __init__( self ): - super( GeneralCompleter, self ).__init__() - - - def SupportedFiletypes( self ): - return set() - - class CompletionsCache( object ): def __init__( self ): self.line = -1 diff --git a/python/completers/general/filename_completer.py b/python/completers/general/filename_completer.py index e2c524e9..f3b1a1c4 100644 --- a/python/completers/general/filename_completer.py +++ b/python/completers/general/filename_completer.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import GeneralCompleter +from completers.threaded_completer import ThreadedCompleter import vim import vimsupport import os @@ -25,14 +25,13 @@ import re USE_WORKING_DIR = vimsupport.GetBoolValue( 'g:ycm_filepath_completion_use_working_dir' ) -class FilenameCompleter( GeneralCompleter ): +class FilenameCompleter( ThreadedCompleter ): """ General completer that provides filename and filepath completions. """ def __init__(self): super( FilenameCompleter, self ).__init__() - self._candidates = [] self._path_regex = re.compile(""" # 1 or more 'D:/'-like token or '/' or '~' or './' or '../' @@ -53,20 +52,11 @@ class FilenameCompleter( GeneralCompleter ): return vim.current.line[ start_column - 1 ] == '/' - def CandidatesForQueryAsyncInner( self, query, start_column ): - self._candidates = [] - self.ComputePaths( start_column ) + def SupportedFiletypes( self ): + return [] - def AsyncCandidateRequestReadyInner( self ): - return True - - - def CandidatesFromStoredRequestInner( self ): - return self._candidates - - - def ComputePaths( self, start_column ): + def ComputeCandidates( self, unused_query, start_column ): def GenerateCandidateForPath( path, path_dir ): is_dir = os.path.isdir( os.path.join( path_dir, path ) ) return { 'word': path, @@ -86,6 +76,4 @@ class FilenameCompleter( GeneralCompleter ): except: paths = [] - self._candidates = [ GenerateCandidateForPath( path, path_dir ) for path - in paths ] - self._completions_ready = True + return [ GenerateCandidateForPath( path, path_dir ) for path in paths ] diff --git a/python/completers/general/general_completer.py b/python/completers/general/general_completer_store.py similarity index 100% rename from python/completers/general/general_completer.py rename to python/completers/general/general_completer_store.py diff --git a/python/completers/general/ultisnips_completer.py b/python/completers/general/ultisnips_completer.py index 0c58a08c..84192bdc 100644 --- a/python/completers/general/ultisnips_completer.py +++ b/python/completers/general/ultisnips_completer.py @@ -18,7 +18,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from completers.completer import GeneralCompleter +from completers.general_completer import GeneralCompleter from UltiSnips import UltiSnips_Manager diff --git a/python/completers/general_completer.py b/python/completers/general_completer.py new file mode 100644 index 00000000..ba8de867 --- /dev/null +++ b/python/completers/general_completer.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011, 2012 Strahinja Val Markovic +# +# 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 . + +from completer import Completer + +class GeneralCompleter( Completer ): + """ + A base class for General completers in YCM. A general completer is used in all + filetypes. + + Because this is a subclass of Completer class, you should refer to the + Completer class documentation. Do NOT use this class for semantic completers! + Subclass Completer directly. + + """ + def __init__( self ): + super( GeneralCompleter, self ).__init__() + + + def SupportedFiletypes( self ): + return set() diff --git a/python/completers/python/jedi_completer.py b/python/completers/python/jedi_completer.py index be80268b..cec39408 100644 --- a/python/completers/python/jedi_completer.py +++ b/python/completers/python/jedi_completer.py @@ -19,8 +19,7 @@ # along with YouCompleteMe. If not, see . import vim -from threading import Thread, Event -from completers.completer import Completer +from completers.threaded_completer import ThreadedCompleter import vimsupport import sys @@ -39,7 +38,7 @@ except ImportError: sys.path.pop( 0 ) -class JediCompleter( Completer ): +class JediCompleter( ThreadedCompleter ): """ A Completer that uses the Jedi completion engine. https://jedi.readthedocs.org/en/latest/ @@ -47,16 +46,6 @@ class JediCompleter( Completer ): def __init__( self ): super( JediCompleter, self ).__init__() - self._query_ready = Event() - self._candidates_ready = Event() - self._candidates = None - self._start_completion_thread() - - - def _start_completion_thread( self ): - self._completion_thread = Thread( target=self.SetCandidates ) - self._completion_thread.daemon = True - self._completion_thread.start() def SupportedFiletypes( self ): @@ -64,47 +53,17 @@ class JediCompleter( Completer ): return [ 'python' ] - def CandidatesForQueryAsyncInner( self, unused_query, unused_start_column ): - self._candidates = None - self._candidates_ready.clear() - self._query_ready.set() + def ComputeCandidates( self, unused_query, unused_start_column ): + filename = vim.current.buffer.name + line, column = vimsupport.CurrentLineAndColumn() + # Jedi expects lines to start at 1, not 0 + line += 1 + contents = '\n'.join( vim.current.buffer ) + script = Script( contents, line, column, filename ) + + return [ { 'word': str( completion.word ), + 'menu': str( completion.description ), + 'info': str( completion.doc ) } + for completion in script.complete() ] - def AsyncCandidateRequestReadyInner( self ): - return WaitAndClear( self._candidates_ready, timeout=0.005 ) - - - def CandidatesFromStoredRequestInner( self ): - return self._candidates or [] - - - def SetCandidates( self ): - while True: - try: - WaitAndClear( self._query_ready ) - - filename = vim.current.buffer.name - line, column = vimsupport.CurrentLineAndColumn() - # Jedi expects lines to start at 1, not 0 - line += 1 - contents = '\n'.join( vim.current.buffer ) - script = Script( contents, line, column, filename ) - - self._candidates = [ { 'word': str( completion.word ), - 'menu': str( completion.description ), - 'info': str( completion.doc ) } - for completion in script.complete() ] - except: - self._query_ready.clear() - self._candidates = [] - self._candidates_ready.set() - - -def WaitAndClear( event, timeout=None ): - # We can't just do flag_is_set = event.wait( timeout ) because that breaks on - # Python 2.6 - event.wait( timeout ) - flag_is_set = event.is_set() - if flag_is_set: - event.clear() - return flag_is_set diff --git a/python/completers/threaded_completer.py b/python/completers/threaded_completer.py new file mode 100644 index 00000000..81d39ef7 --- /dev/null +++ b/python/completers/threaded_completer.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011, 2012 Strahinja Val Markovic +# +# 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 . + +import abc +from threading import Thread, Event +from completer import Completer + +class ThreadedCompleter( Completer ): + def __init__( self ): + super( ThreadedCompleter, self ).__init__() + self._query_ready = Event() + self._candidates_ready = Event() + self._candidates = None + self._start_completion_thread() + + + def _start_completion_thread( self ): + self._completion_thread = Thread( target=self.SetCandidates ) + self._completion_thread.daemon = True + self._completion_thread.start() + + + def CandidatesForQueryAsyncInner( self, query, start_column ): + self._candidates = None + self._candidates_ready.clear() + self._query = query + self._start_column = start_column + self._query_ready.set() + + + def AsyncCandidateRequestReadyInner( self ): + return WaitAndClearIfSet( self._candidates_ready, timeout=0.005 ) + + + def CandidatesFromStoredRequestInner( self ): + return self._candidates or [] + + + @abc.abstractmethod + def ComputeCandidates( self, query, start_column ): + pass + + + def SetCandidates( self ): + while True: + try: + WaitAndClearIfSet( self._query_ready ) + self._candidates = self.ComputeCandidates( self._query, + self._start_column ) + except: + self._query_ready.clear() + self._candidates = [] + self._candidates_ready.set() + + +def WaitAndClearIfSet( event, timeout=None ): + """Given an |event| and a |timeout|, waits for the event a maximum of timeout + seconds. After waiting, clears the event if it's set and returns the state of + the event before it was cleared.""" + + # We can't just do flag_is_set = event.wait( timeout ) because that breaks on + # Python 2.6 + event.wait( timeout ) + flag_is_set = event.is_set() + if flag_is_set: + event.clear() + return flag_is_set diff --git a/python/ycm.py b/python/ycm.py index 1cdfc354..72d4d4f4 100644 --- a/python/ycm.py +++ b/python/ycm.py @@ -35,7 +35,7 @@ except ImportError as e: from completers.all.omni_completer import OmniCompleter -from completers.general.general_completer import GeneralCompleterStore +from completers.general.general_completer_store import GeneralCompleterStore FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval( diff --git a/python/ycm_utils.py b/python/ycm_utils.py index 68744e50..d1fe5035 100644 --- a/python/ycm_utils.py +++ b/python/ycm_utils.py @@ -23,3 +23,4 @@ def IsIdentifierChar( char ): def SanitizeQuery( query ): return query.strip() +