server_state.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. import imp
  20. import os
  21. from ycm.completers.general.general_completer_store import GeneralCompleterStore
  22. class ServerState( object ):
  23. def __init__( self, user_options ):
  24. self._user_options = user_options
  25. self._filetype_completers = {}
  26. self._gencomp = GeneralCompleterStore( self._user_options )
  27. @property
  28. def user_options( self ):
  29. return self._user_options
  30. def _GetFiletypeCompleterForFiletype( self, filetype ):
  31. try:
  32. return self._filetype_completers[ filetype ]
  33. except KeyError:
  34. pass
  35. module_path = _PathToFiletypeCompleterPluginLoader( filetype )
  36. completer = None
  37. supported_filetypes = [ filetype ]
  38. if os.path.exists( module_path ):
  39. module = imp.load_source( filetype, module_path )
  40. completer = module.GetCompleter( self._user_options )
  41. if completer:
  42. supported_filetypes.extend( completer.SupportedFiletypes() )
  43. for supported_filetype in supported_filetypes:
  44. self._filetype_completers[ supported_filetype ] = completer
  45. return completer
  46. def GetFiletypeCompleter( self, current_filetypes ):
  47. completers = [ self._GetFiletypeCompleterForFiletype( filetype )
  48. for filetype in current_filetypes ]
  49. for completer in completers:
  50. if completer:
  51. return completer
  52. return None
  53. def FiletypeCompletionAvailable( self, filetypes ):
  54. return bool( self.GetFiletypeCompleter( filetypes ) )
  55. def FiletypeCompletionUsable( self, filetypes ):
  56. return ( self.CurrentFiletypeCompletionEnabled( filetypes ) and
  57. self.FiletypeCompletionAvailable( filetypes ) )
  58. def ShouldUseGeneralCompleter( self, request_data ):
  59. return self._gencomp.ShouldUseNow( request_data )
  60. def ShouldUseFiletypeCompleter( self, request_data ):
  61. filetypes = request_data[ 'filetypes' ]
  62. if self.FiletypeCompletionUsable( filetypes ):
  63. return self.GetFiletypeCompleter( filetypes ).ShouldUseNow( request_data )
  64. return False
  65. def GetGeneralCompleter( self ):
  66. return self._gencomp
  67. def CurrentFiletypeCompletionEnabled( self, current_filetypes ):
  68. filetype_to_disable = self._user_options[
  69. 'filetype_specific_completion_to_disable' ]
  70. return not all([ x in filetype_to_disable for x in current_filetypes ])
  71. def _PathToCompletersFolder():
  72. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  73. return os.path.join( dir_of_current_script, '..', 'completers' )
  74. def _PathToFiletypeCompleterPluginLoader( filetype ):
  75. return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' )