identifier_completer.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011, 2012 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 os
  20. import logging
  21. import ycm_core
  22. from collections import defaultdict
  23. from ycm.completers.general_completer import GeneralCompleter
  24. # from ycm.completers.general import syntax_parse
  25. from ycm import utils
  26. from ycm.utils import ToUtf8IfNeeded
  27. from ycm.server import responses
  28. MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10
  29. SYNTAX_FILENAME = 'YCM_PLACEHOLDER_FOR_SYNTAX'
  30. class IdentifierCompleter( GeneralCompleter ):
  31. def __init__( self, user_options ):
  32. super( IdentifierCompleter, self ).__init__( user_options )
  33. self._completer = ycm_core.IdentifierCompleter()
  34. self._tags_file_last_mtime = defaultdict( int )
  35. self._logger = logging.getLogger( __name__ )
  36. def ShouldUseNow( self, request_data ):
  37. return self.QueryLengthAboveMinThreshold( request_data )
  38. def ComputeCandidates( self, request_data ):
  39. if not self.ShouldUseNow( request_data ):
  40. return []
  41. completions = self._completer.CandidatesForQueryAndType(
  42. ToUtf8IfNeeded( utils.SanitizeQuery( request_data[ 'query' ] ) ),
  43. ToUtf8IfNeeded( request_data[ 'filetypes' ][ 0 ] ) )
  44. completions = completions[ : MAX_IDENTIFIER_COMPLETIONS_RETURNED ]
  45. completions = _RemoveSmallCandidates(
  46. completions, self.user_options[ 'min_num_identifier_candidate_chars' ] )
  47. return [ responses.BuildCompletionData( x ) for x in completions ]
  48. def AddIdentifier( self, identifier, request_data ):
  49. filetype = request_data[ 'filetypes' ][ 0 ]
  50. filepath = request_data[ 'filepath' ]
  51. if not filetype or not filepath or not identifier:
  52. return
  53. vector = ycm_core.StringVec()
  54. vector.append( ToUtf8IfNeeded( identifier ) )
  55. self._logger.info( 'Adding ONE buffer identifier for file: %s', filepath )
  56. self._completer.AddIdentifiersToDatabase( vector,
  57. ToUtf8IfNeeded( filetype ),
  58. ToUtf8IfNeeded( filepath ) )
  59. def AddPreviousIdentifier( self, request_data ):
  60. self.AddIdentifier(
  61. _PreviousIdentifier(
  62. self.user_options[ 'min_num_of_chars_for_completion' ],
  63. request_data ),
  64. request_data )
  65. def AddIdentifierUnderCursor( self, request_data ):
  66. cursor_identifier = _GetCursorIdentifier( request_data )
  67. if not cursor_identifier:
  68. return
  69. self.AddIdentifier( cursor_identifier, request_data )
  70. def AddBufferIdentifiers( self, request_data ):
  71. filetype = request_data[ 'filetypes' ][ 0 ]
  72. filepath = request_data[ 'filepath' ]
  73. collect_from_comments_and_strings = bool( self.user_options[
  74. 'collect_identifiers_from_comments_and_strings' ] )
  75. if not filetype or not filepath:
  76. return
  77. text = request_data[ 'file_data' ][ filepath ][ 'contents' ]
  78. self._logger.info( 'Adding buffer identifiers for file: %s', filepath )
  79. self._completer.AddIdentifiersToDatabaseFromBuffer(
  80. ToUtf8IfNeeded( text ),
  81. ToUtf8IfNeeded( filetype ),
  82. ToUtf8IfNeeded( filepath ),
  83. collect_from_comments_and_strings )
  84. def AddIdentifiersFromTagFiles( self, tag_files ):
  85. absolute_paths_to_tag_files = ycm_core.StringVec()
  86. for tag_file in tag_files:
  87. try:
  88. current_mtime = os.path.getmtime( tag_file )
  89. except:
  90. continue
  91. last_mtime = self._tags_file_last_mtime[ tag_file ]
  92. # We don't want to repeatedly process the same file over and over; we only
  93. # process if it's changed since the last time we looked at it
  94. if current_mtime <= last_mtime:
  95. continue
  96. self._tags_file_last_mtime[ tag_file ] = current_mtime
  97. absolute_paths_to_tag_files.append( ToUtf8IfNeeded( tag_file ) )
  98. if not absolute_paths_to_tag_files:
  99. return
  100. self._completer.AddIdentifiersToDatabaseFromTagFiles(
  101. absolute_paths_to_tag_files )
  102. def AddIdentifiersFromSyntax( self, keyword_list, filetypes ):
  103. keyword_vector = ycm_core.StringVec()
  104. for keyword in keyword_list:
  105. keyword_vector.append( ToUtf8IfNeeded( keyword ) )
  106. filepath = SYNTAX_FILENAME + filetypes[ 0 ]
  107. self._completer.AddIdentifiersToDatabase( keyword_vector,
  108. ToUtf8IfNeeded( filetypes[ 0 ] ),
  109. ToUtf8IfNeeded( filepath ) )
  110. def OnFileReadyToParse( self, request_data ):
  111. self.AddBufferIdentifiers( request_data )
  112. if 'tag_files' in request_data:
  113. self.AddIdentifiersFromTagFiles( request_data[ 'tag_files' ] )
  114. if 'syntax_keywords' in request_data:
  115. self.AddIdentifiersFromSyntax( request_data[ 'syntax_keywords' ],
  116. request_data[ 'filetypes' ] )
  117. def OnInsertLeave( self, request_data ):
  118. self.AddIdentifierUnderCursor( request_data )
  119. def OnCurrentIdentifierFinished( self, request_data ):
  120. self.AddPreviousIdentifier( request_data )
  121. def _PreviousIdentifier( min_num_completion_start_chars, request_data ):
  122. line_num = request_data[ 'line_num' ]
  123. column_num = request_data[ 'column_num' ]
  124. filepath = request_data[ 'filepath' ]
  125. contents_per_line = (
  126. request_data[ 'file_data' ][ filepath ][ 'contents' ].split( '\n' ) )
  127. line = contents_per_line[ line_num ]
  128. end_column = column_num
  129. while end_column > 0 and not utils.IsIdentifierChar( line[ end_column - 1 ] ):
  130. end_column -= 1
  131. # Look at the previous line if we reached the end of the current one
  132. if end_column == 0:
  133. try:
  134. line = contents_per_line[ line_num - 1 ]
  135. except:
  136. return ""
  137. end_column = len( line )
  138. while end_column > 0 and not utils.IsIdentifierChar(
  139. line[ end_column - 1 ] ):
  140. end_column -= 1
  141. start_column = end_column
  142. while start_column > 0 and utils.IsIdentifierChar( line[ start_column - 1 ] ):
  143. start_column -= 1
  144. if end_column - start_column < min_num_completion_start_chars:
  145. return ""
  146. return line[ start_column : end_column ]
  147. def _RemoveSmallCandidates( candidates, min_num_candidate_size_chars ):
  148. if min_num_candidate_size_chars == 0:
  149. return candidates
  150. return [ x for x in candidates if len( x ) >= min_num_candidate_size_chars ]
  151. # This is meant to behave like 'expand("<cword")' in Vim, thus starting at the
  152. # cursor column and returning the "cursor word". If the cursor is not on a valid
  153. # character, it searches forward until a valid identifier is found.
  154. def _GetCursorIdentifier( request_data ):
  155. def FindFirstValidChar( line, column ):
  156. current_column = column
  157. while not utils.IsIdentifierChar( line[ current_column ] ):
  158. current_column += 1
  159. return current_column
  160. def FindIdentifierStart( line, valid_char_column ):
  161. identifier_start = valid_char_column
  162. while identifier_start > 0 and utils.IsIdentifierChar( line[
  163. identifier_start - 1 ] ):
  164. identifier_start -= 1
  165. return identifier_start
  166. def FindIdentifierEnd( line, valid_char_column ):
  167. identifier_end = valid_char_column
  168. while identifier_end < len( line ) - 1 and utils.IsIdentifierChar( line[
  169. identifier_end + 1 ] ):
  170. identifier_end += 1
  171. return identifier_end + 1
  172. column_num = request_data[ 'column_num' ]
  173. line = request_data[ 'line_value' ]
  174. try:
  175. valid_char_column = FindFirstValidChar( line, column_num )
  176. return line[ FindIdentifierStart( line, valid_char_column ) :
  177. FindIdentifierEnd( line, valid_char_column ) ]
  178. except:
  179. return ''