base.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright (C) 2011, 2012 Google Inc.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. from future import standard_library
  22. standard_library.install_aliases()
  23. from builtins import * # noqa
  24. from future.utils import iteritems
  25. from ycm import vimsupport
  26. from ycmd import user_options_store
  27. from ycmd import request_wrap
  28. from ycmd import identifier_utils
  29. YCM_VAR_PREFIX = 'ycm_'
  30. def BuildServerConf():
  31. """Builds a dictionary mapping YCM Vim user options to values. Option names
  32. don't have the 'ycm_' prefix."""
  33. # We only evaluate the keys of the vim globals and not the whole dictionary
  34. # to avoid unicode issues.
  35. # See https://github.com/Valloric/YouCompleteMe/pull/2151 for details.
  36. keys = vimsupport.GetVimGlobalsKeys()
  37. server_conf = {}
  38. for key in keys:
  39. if not key.startswith( YCM_VAR_PREFIX ):
  40. continue
  41. new_key = key[ len( YCM_VAR_PREFIX ): ]
  42. new_value = vimsupport.VimExpressionToPythonType( 'g:' + key )
  43. server_conf[ new_key ] = new_value
  44. return server_conf
  45. def LoadJsonDefaultsIntoVim():
  46. defaults = user_options_store.DefaultOptions()
  47. for key, value in iteritems( defaults ):
  48. new_key = 'g:ycm_' + key
  49. if not vimsupport.VariableExists( new_key ):
  50. vimsupport.SetVariableValue( new_key, value )
  51. def CompletionStartColumn():
  52. return ( request_wrap.CompletionStartColumn(
  53. vimsupport.CurrentLineContents(),
  54. vimsupport.CurrentColumn() + 1,
  55. vimsupport.CurrentFiletypes()[ 0 ] ) - 1 )
  56. def CurrentIdentifierFinished():
  57. current_column = vimsupport.CurrentColumn()
  58. previous_char_index = current_column - 1
  59. if previous_char_index < 0:
  60. return True
  61. line = vimsupport.CurrentLineContents()
  62. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  63. regex = identifier_utils.IdentifierRegexForFiletype( filetype )
  64. for match in regex.finditer( line ):
  65. if match.end() == previous_char_index:
  66. return True
  67. # If the whole line is whitespace, that means the user probably finished an
  68. # identifier on the previous line.
  69. return line[ : current_column ].isspace()
  70. def LastEnteredCharIsIdentifierChar():
  71. current_column = vimsupport.CurrentColumn()
  72. if current_column - 1 < 0:
  73. return False
  74. line = vimsupport.CurrentLineContents()
  75. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  76. return (
  77. identifier_utils.StartOfLongestIdentifierEndingAtIndex(
  78. line, current_column, filetype ) != current_column )
  79. def AdjustCandidateInsertionText( candidates ):
  80. """This function adjusts the candidate insertion text to take into account the
  81. text that's currently in front of the cursor.
  82. For instance ('|' represents the cursor):
  83. 1. Buffer state: 'foo.|bar'
  84. 2. A completion candidate of 'zoobar' is shown and the user selects it.
  85. 3. Buffer state: 'foo.zoobar|bar' instead of 'foo.zoo|bar' which is what the
  86. user wanted.
  87. This function changes candidates to resolve that issue.
  88. It could be argued that the user actually wants the final buffer state to be
  89. 'foo.zoobar|' (the cursor at the end), but that would be much more difficult
  90. to implement and is probably not worth doing.
  91. """
  92. def NewCandidateInsertionText( to_insert, text_after_cursor ):
  93. overlap_len = OverlapLength( to_insert, text_after_cursor )
  94. if overlap_len:
  95. return to_insert[ :-overlap_len ]
  96. return to_insert
  97. text_after_cursor = vimsupport.TextAfterCursor()
  98. if not text_after_cursor:
  99. return candidates
  100. new_candidates = []
  101. for candidate in candidates:
  102. if isinstance( candidate, dict ):
  103. new_candidate = candidate.copy()
  104. if 'abbr' not in new_candidate:
  105. new_candidate[ 'abbr' ] = new_candidate[ 'word' ]
  106. new_candidate[ 'word' ] = NewCandidateInsertionText(
  107. new_candidate[ 'word' ],
  108. text_after_cursor )
  109. new_candidates.append( new_candidate )
  110. elif isinstance( candidate, str ) or isinstance( candidate, bytes ):
  111. new_candidates.append(
  112. { 'abbr': candidate,
  113. 'word': NewCandidateInsertionText( candidate, text_after_cursor ) } )
  114. return new_candidates
  115. def OverlapLength( left_string, right_string ):
  116. """Returns the length of the overlap between two strings.
  117. Example: "foo baro" and "baro zoo" -> 4
  118. """
  119. left_string_length = len( left_string )
  120. right_string_length = len( right_string )
  121. if not left_string_length or not right_string_length:
  122. return 0
  123. # Truncate the longer string.
  124. if left_string_length > right_string_length:
  125. left_string = left_string[ -right_string_length: ]
  126. elif left_string_length < right_string_length:
  127. right_string = right_string[ :left_string_length ]
  128. if left_string == right_string:
  129. return min( left_string_length, right_string_length )
  130. # Start by looking for a single character match
  131. # and increase length until no match is found.
  132. best = 0
  133. length = 1
  134. while True:
  135. pattern = left_string[ -length: ]
  136. found = right_string.find( pattern )
  137. if found < 0:
  138. return best
  139. length += found
  140. if left_string[ -length: ] == right_string[ :length ]:
  141. best = length
  142. length += 1