server_state.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 import extra_conf_store
  22. from ycm.completers.general.general_completer_store import GeneralCompleterStore
  23. class ServerState( object ):
  24. def __init__( self, user_options ):
  25. self._user_options = user_options
  26. self._filetype_completers = {}
  27. self._gencomp = GeneralCompleterStore( self._user_options )
  28. extra_conf_store.CallExtraConfYcmCorePreloadIfExists()
  29. @property
  30. def user_options( self ):
  31. return self._user_options
  32. def Shutdown( self ):
  33. for completer in self._filetype_completers.itervalues():
  34. if completer:
  35. completer.Shutdown()
  36. self._gencomp.Shutdown()
  37. extra_conf_store.Shutdown()
  38. def _GetFiletypeCompleterForFiletype( self, filetype ):
  39. try:
  40. return self._filetype_completers[ filetype ]
  41. except KeyError:
  42. pass
  43. module_path = _PathToFiletypeCompleterPluginLoader( filetype )
  44. completer = None
  45. supported_filetypes = [ filetype ]
  46. if os.path.exists( module_path ):
  47. module = imp.load_source( filetype, module_path )
  48. completer = module.GetCompleter( self._user_options )
  49. if completer:
  50. supported_filetypes.extend( completer.SupportedFiletypes() )
  51. for supported_filetype in supported_filetypes:
  52. self._filetype_completers[ supported_filetype ] = completer
  53. return completer
  54. def GetFiletypeCompleter( self, current_filetypes ):
  55. completers = [ self._GetFiletypeCompleterForFiletype( filetype )
  56. for filetype in current_filetypes ]
  57. for completer in completers:
  58. if completer:
  59. return completer
  60. raise ValueError( 'No semantic completer exists for filetypes: {}'.format(
  61. current_filetypes ) )
  62. def FiletypeCompletionAvailable( self, filetypes ):
  63. try:
  64. self.GetFiletypeCompleter( filetypes )
  65. return True
  66. except:
  67. return False
  68. def FiletypeCompletionUsable( self, filetypes ):
  69. return ( self.CurrentFiletypeCompletionEnabled( filetypes ) and
  70. self.FiletypeCompletionAvailable( filetypes ) )
  71. def ShouldUseGeneralCompleter( self, request_data ):
  72. return self._gencomp.ShouldUseNow( request_data )
  73. def ShouldUseFiletypeCompleter( self, request_data ):
  74. filetypes = request_data[ 'filetypes' ]
  75. if self.FiletypeCompletionUsable( filetypes ):
  76. return self.GetFiletypeCompleter( filetypes ).ShouldUseNow( request_data )
  77. return False
  78. def GetGeneralCompleter( self ):
  79. return self._gencomp
  80. def CurrentFiletypeCompletionEnabled( self, current_filetypes ):
  81. filetype_to_disable = self._user_options[
  82. 'filetype_specific_completion_to_disable' ]
  83. return not all([ x in filetype_to_disable for x in current_filetypes ])
  84. def _PathToCompletersFolder():
  85. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  86. return os.path.join( dir_of_current_script, '..', 'completers' )
  87. def _PathToFiletypeCompleterPluginLoader( filetype ):
  88. return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' )