base.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 user_options_store
  19. from ycmd import request_wrap
  20. from ycmd import identifier_utils
  21. YCM_VAR_PREFIX = 'ycm_'
  22. def BuildServerConf():
  23. """Builds a dictionary mapping YCM Vim user options to values. Option names
  24. don't have the 'ycm_' prefix."""
  25. vim_globals = vimsupport.GetReadOnlyVimGlobals( force_python_objects = True )
  26. server_conf = {}
  27. for key, value in vim_globals.items():
  28. if not key.startswith( YCM_VAR_PREFIX ):
  29. continue
  30. try:
  31. new_value = int( value )
  32. except:
  33. new_value = value
  34. new_key = key[ len( YCM_VAR_PREFIX ): ]
  35. server_conf[ new_key ] = new_value
  36. return server_conf
  37. def LoadJsonDefaultsIntoVim():
  38. defaults = user_options_store.DefaultOptions()
  39. vim_defaults = {}
  40. for key, value in defaults.iteritems():
  41. vim_defaults[ 'ycm_' + key ] = value
  42. vimsupport.LoadDictIntoVimGlobals( vim_defaults, overwrite = False )
  43. def CompletionStartColumn():
  44. return ( request_wrap.CompletionStartColumn(
  45. vimsupport.CurrentLineContents(),
  46. vimsupport.CurrentColumn() + 1,
  47. vimsupport.CurrentFiletypes()[ 0 ] ) - 1 )
  48. def CurrentIdentifierFinished():
  49. current_column = vimsupport.CurrentColumn()
  50. previous_char_index = current_column - 1
  51. if previous_char_index < 0:
  52. return True
  53. line = vimsupport.CurrentLineContents()
  54. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  55. regex = identifier_utils.IdentifierRegexForFiletype( filetype )
  56. for match in regex.finditer( line ):
  57. if match.end() == previous_char_index:
  58. return True
  59. # If the whole line is whitespace, that means the user probably finished an
  60. # identifier on the previous line.
  61. return line[ : current_column ].isspace()
  62. def LastEnteredCharIsIdentifierChar():
  63. current_column = vimsupport.CurrentColumn()
  64. if current_column - 1 < 0:
  65. return False
  66. line = vimsupport.CurrentLineContents()
  67. filetype = vimsupport.CurrentFiletypes()[ 0 ]
  68. return (
  69. identifier_utils.StartOfLongestIdentifierEndingAtIndex(
  70. line, current_column, filetype ) != current_column )
  71. def AdjustCandidateInsertionText( candidates ):
  72. """This function adjusts the candidate insertion text to take into account the
  73. text that's currently in front of the cursor.
  74. For instance ('|' represents the cursor):
  75. 1. Buffer state: 'foo.|bar'
  76. 2. A completion candidate of 'zoobar' is shown and the user selects it.
  77. 3. Buffer state: 'foo.zoobar|bar' instead of 'foo.zoo|bar' which is what the
  78. user wanted.
  79. This function changes candidates to resolve that issue.
  80. It could be argued that the user actually wants the final buffer state to be
  81. 'foo.zoobar|' (the cursor at the end), but that would be much more difficult
  82. to implement and is probably not worth doing.
  83. """
  84. def NewCandidateInsertionText( to_insert, text_after_cursor ):
  85. overlap_len = OverlapLength( to_insert, text_after_cursor )
  86. if overlap_len:
  87. return to_insert[ :-overlap_len ]
  88. return to_insert
  89. text_after_cursor = vimsupport.TextAfterCursor()
  90. if not text_after_cursor:
  91. return candidates
  92. new_candidates = []
  93. for candidate in candidates:
  94. if type( candidate ) is dict:
  95. new_candidate = candidate.copy()
  96. if not 'abbr' in new_candidate:
  97. new_candidate[ 'abbr' ] = new_candidate[ 'word' ]
  98. new_candidate[ 'word' ] = NewCandidateInsertionText(
  99. new_candidate[ 'word' ],
  100. text_after_cursor )
  101. new_candidates.append( new_candidate )
  102. elif type( candidate ) is str:
  103. new_candidates.append(
  104. { 'abbr': candidate,
  105. 'word': NewCandidateInsertionText( candidate, text_after_cursor ) } )
  106. return new_candidates
  107. def OverlapLength( left_string, right_string ):
  108. """Returns the length of the overlap between two strings.
  109. Example: "foo baro" and "baro zoo" -> 4
  110. """
  111. left_string_length = len( left_string )
  112. right_string_length = len( right_string )
  113. if not left_string_length or not right_string_length:
  114. return 0
  115. # Truncate the longer string.
  116. if left_string_length > right_string_length:
  117. left_string = left_string[ -right_string_length: ]
  118. elif left_string_length < right_string_length:
  119. right_string = right_string[ :left_string_length ]
  120. if left_string == right_string:
  121. return min( left_string_length, right_string_length )
  122. # Start by looking for a single character match
  123. # and increase length until no match is found.
  124. best = 0
  125. length = 1
  126. while True:
  127. pattern = left_string[ -length: ]
  128. found = right_string.find( pattern )
  129. if found < 0:
  130. return best
  131. length += found
  132. if left_string[ -length: ] == right_string[ :length ]:
  133. best = length
  134. length += 1