base.py 4.6 KB

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