base.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycm import vimsupport
  24. from ycmd import identifier_utils
  25. YCM_VAR_PREFIX = 'ycm_'
  26. def GetUserOptions():
  27. """Builds a dictionary mapping YCM Vim user options to values. Option names
  28. don't have the 'ycm_' prefix."""
  29. # We only evaluate the keys of the vim globals and not the whole dictionary
  30. # to avoid unicode issues.
  31. # See https://github.com/Valloric/YouCompleteMe/pull/2151 for details.
  32. keys = vimsupport.GetVimGlobalsKeys()
  33. user_options = {}
  34. for key in keys:
  35. if not key.startswith( YCM_VAR_PREFIX ):
  36. continue
  37. new_key = key[ len( YCM_VAR_PREFIX ): ]
  38. new_value = vimsupport.VimExpressionToPythonType( 'g:' + key )
  39. user_options[ new_key ] = new_value
  40. return user_options
  41. def CurrentIdentifierFinished():
  42. line, current_column = vimsupport.CurrentLineContentsAndCodepointColumn()
  43. previous_char_index = current_column - 1
  44. if previous_char_index < 0:
  45. return True
  46. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  47. regex = identifier_utils.IdentifierRegexForFiletype( filetype )
  48. for match in regex.finditer( line ):
  49. if match.end() == previous_char_index:
  50. return True
  51. # If the whole line is whitespace, that means the user probably finished an
  52. # identifier on the previous line.
  53. return line[ : current_column ].isspace()
  54. def LastEnteredCharIsIdentifierChar():
  55. line, current_column = vimsupport.CurrentLineContentsAndCodepointColumn()
  56. if current_column - 1 < 0:
  57. return False
  58. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  59. return (
  60. identifier_utils.StartOfLongestIdentifierEndingAtIndex(
  61. line, current_column, filetype ) != current_column )
  62. def AdjustCandidateInsertionText( candidates ):
  63. """This function adjusts the candidate insertion text to take into account the
  64. text that's currently in front of the cursor.
  65. For instance ('|' represents the cursor):
  66. 1. Buffer state: 'foo.|bar'
  67. 2. A completion candidate of 'zoobar' is shown and the user selects it.
  68. 3. Buffer state: 'foo.zoobar|bar' instead of 'foo.zoo|bar' which is what the
  69. user wanted.
  70. This function changes candidates to resolve that issue.
  71. It could be argued that the user actually wants the final buffer state to be
  72. 'foo.zoobar|' (the cursor at the end), but that would be much more difficult
  73. to implement and is probably not worth doing.
  74. """
  75. def NewCandidateInsertionText( to_insert, text_after_cursor ):
  76. overlap_len = OverlapLength( to_insert, text_after_cursor )
  77. if overlap_len:
  78. return to_insert[ :-overlap_len ]
  79. return to_insert
  80. text_after_cursor = vimsupport.TextAfterCursor()
  81. if not text_after_cursor:
  82. return candidates
  83. new_candidates = []
  84. for candidate in candidates:
  85. new_candidate = candidate.copy()
  86. if not new_candidate.get( 'abbr' ):
  87. new_candidate[ 'abbr' ] = new_candidate[ 'word' ]
  88. new_candidate[ 'word' ] = NewCandidateInsertionText(
  89. new_candidate[ 'word' ],
  90. text_after_cursor )
  91. new_candidates.append( new_candidate )
  92. return new_candidates
  93. def OverlapLength( left_string, right_string ):
  94. """Returns the length of the overlap between two strings.
  95. Example: "foo baro" and "baro zoo" -> 4
  96. """
  97. left_string_length = len( left_string )
  98. right_string_length = len( right_string )
  99. if not left_string_length or not right_string_length:
  100. return 0
  101. # Truncate the longer string.
  102. if left_string_length > right_string_length:
  103. left_string = left_string[ -right_string_length: ]
  104. elif left_string_length < right_string_length:
  105. right_string = right_string[ :left_string_length ]
  106. if left_string == right_string:
  107. return min( left_string_length, right_string_length )
  108. # Start by looking for a single character match
  109. # and increase length until no match is found.
  110. best = 0
  111. length = 1
  112. while True:
  113. pattern = left_string[ -length: ]
  114. found = right_string.find( pattern )
  115. if found < 0:
  116. return best
  117. length += found
  118. if left_string[ -length: ] == right_string[ :length ]:
  119. best = length
  120. length += 1