1
0

identifier_completer.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 vim
  20. import ycm_core
  21. from ycm.completers.general_completer import GeneralCompleter
  22. from ycm import vimsupport
  23. from ycm import utils
  24. MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10
  25. MIN_NUM_CHARS = int( vimsupport.GetVariableValue(
  26. "g:ycm_min_num_of_chars_for_completion" ) )
  27. class IdentifierCompleter( GeneralCompleter ):
  28. def __init__( self ):
  29. super( IdentifierCompleter, self ).__init__()
  30. self.completer = ycm_core.IdentifierCompleter()
  31. self.completer.EnableThreading()
  32. def ShouldUseNow( self, start_column ):
  33. return self.QueryLengthAboveMinThreshold( start_column )
  34. def CandidatesForQueryAsync( self, query, unused_start_column ):
  35. filetype = vim.eval( "&filetype" )
  36. self.completions_future = self.completer.CandidatesForQueryAndTypeAsync(
  37. utils.SanitizeQuery( query ),
  38. filetype )
  39. def AddIdentifier( self, identifier ):
  40. filetype = vim.eval( "&filetype" )
  41. filepath = vim.eval( "expand('%:p')" )
  42. if not filetype or not filepath or not identifier:
  43. return
  44. vector = ycm_core.StringVec()
  45. vector.append( identifier )
  46. self.completer.AddCandidatesToDatabase( vector,
  47. filetype,
  48. filepath )
  49. def AddPreviousIdentifier( self ):
  50. self.AddIdentifier( PreviousIdentifier() )
  51. def AddIdentifierUnderCursor( self ):
  52. cursor_identifier = vim.eval( 'expand("<cword>")' )
  53. if not cursor_identifier:
  54. return
  55. stripped_cursor_identifier = ''.join( ( x for x in
  56. cursor_identifier if
  57. utils.IsIdentifierChar( x ) ) )
  58. if not stripped_cursor_identifier:
  59. return
  60. self.AddIdentifier( stripped_cursor_identifier )
  61. def AddBufferIdentifiers( self ):
  62. filetype = vim.eval( "&filetype" )
  63. filepath = vim.eval( "expand('%:p')" )
  64. collect_from_comments_and_strings = vimsupport.GetBoolValue(
  65. "g:ycm_collect_identifiers_from_comments_and_strings" )
  66. if not filetype or not filepath:
  67. return
  68. text = "\n".join( vim.current.buffer )
  69. self.completer.AddCandidatesToDatabaseFromBufferAsync(
  70. text,
  71. filetype,
  72. filepath,
  73. collect_from_comments_and_strings )
  74. def OnFileReadyToParse( self ):
  75. self.AddBufferIdentifiers()
  76. def OnInsertLeave( self ):
  77. self.AddIdentifierUnderCursor()
  78. def OnCurrentIdentifierFinished( self ):
  79. self.AddPreviousIdentifier()
  80. def CandidatesFromStoredRequest( self ):
  81. if not self.completions_future:
  82. return []
  83. completions = self.completions_future.GetResults()[
  84. : MAX_IDENTIFIER_COMPLETIONS_RETURNED ]
  85. # We will never have duplicates in completions so with 'dup':1 we tell Vim
  86. # to add this candidate even if it's a duplicate of an existing one (which
  87. # will never happen). This saves us some expensive string matching
  88. # operations in Vim.
  89. return [ { 'word': x, 'dup': 1 } for x in completions ]
  90. def PreviousIdentifier():
  91. line_num, column_num = vimsupport.CurrentLineAndColumn()
  92. buffer = vim.current.buffer
  93. line = buffer[ line_num ]
  94. end_column = column_num
  95. while end_column > 0 and not utils.IsIdentifierChar( line[ end_column - 1 ] ):
  96. end_column -= 1
  97. # Look at the previous line if we reached the end of the current one
  98. if end_column == 0:
  99. try:
  100. line = buffer[ line_num - 1]
  101. except:
  102. return ""
  103. end_column = len( line )
  104. while end_column > 0 and not utils.IsIdentifierChar(
  105. line[ end_column - 1 ] ):
  106. end_column -= 1
  107. start_column = end_column
  108. while start_column > 0 and utils.IsIdentifierChar( line[ start_column - 1 ] ):
  109. start_column -= 1
  110. if end_column - start_column < MIN_NUM_CHARS:
  111. return ""
  112. return line[ start_column : end_column ]