2013-05-19 23:06:14 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2014-01-13 14:08:43 -05:00
|
|
|
# Copyright (C) 2011, 2012 Google Inc.
|
2013-05-19 23:06:14 -04:00
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from ycm import vimsupport
|
2014-05-13 16:09:19 -04:00
|
|
|
from ycmd import user_options_store
|
2014-05-27 17:29:19 -04:00
|
|
|
from ycmd import request_wrap
|
2014-08-28 14:36:46 -04:00
|
|
|
from ycmd import identifier_utils
|
2013-05-19 23:06:14 -04:00
|
|
|
|
2013-09-02 17:45:53 -04:00
|
|
|
YCM_VAR_PREFIX = 'ycm_'
|
|
|
|
|
2013-05-19 23:06:14 -04:00
|
|
|
|
2013-09-02 17:45:53 -04:00
|
|
|
def BuildServerConf():
|
|
|
|
"""Builds a dictionary mapping YCM Vim user options to values. Option names
|
|
|
|
don't have the 'ycm_' prefix."""
|
|
|
|
|
2013-10-06 21:26:59 -04:00
|
|
|
vim_globals = vimsupport.GetReadOnlyVimGlobals( force_python_objects = True )
|
2013-09-02 17:45:53 -04:00
|
|
|
server_conf = {}
|
|
|
|
for key, value in vim_globals.items():
|
|
|
|
if not key.startswith( YCM_VAR_PREFIX ):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
new_value = int( value )
|
|
|
|
except:
|
|
|
|
new_value = value
|
|
|
|
new_key = key[ len( YCM_VAR_PREFIX ): ]
|
|
|
|
server_conf[ new_key ] = new_value
|
|
|
|
|
|
|
|
return server_conf
|
|
|
|
|
|
|
|
|
2013-10-04 15:23:33 -04:00
|
|
|
def LoadJsonDefaultsIntoVim():
|
|
|
|
defaults = user_options_store.DefaultOptions()
|
|
|
|
vim_defaults = {}
|
|
|
|
for key, value in defaults.iteritems():
|
|
|
|
vim_defaults[ 'ycm_' + key ] = value
|
|
|
|
|
|
|
|
vimsupport.LoadDictIntoVimGlobals( vim_defaults, overwrite = False )
|
|
|
|
|
|
|
|
|
2013-05-19 23:06:14 -04:00
|
|
|
def CompletionStartColumn():
|
2014-05-27 17:29:19 -04:00
|
|
|
return ( request_wrap.CompletionStartColumn(
|
2014-08-28 14:36:46 -04:00
|
|
|
vimsupport.CurrentLineContents(),
|
|
|
|
vimsupport.CurrentColumn() + 1,
|
|
|
|
vimsupport.CurrentFiletypes()[ 0 ] ) - 1 )
|
2013-05-19 23:06:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def CurrentIdentifierFinished():
|
|
|
|
current_column = vimsupport.CurrentColumn()
|
|
|
|
previous_char_index = current_column - 1
|
|
|
|
if previous_char_index < 0:
|
|
|
|
return True
|
2014-08-28 14:36:46 -04:00
|
|
|
line = vimsupport.CurrentLineContents()
|
|
|
|
filetype = vimsupport.CurrentFiletypes()[ 0 ]
|
|
|
|
regex = identifier_utils.IdentifierRegexForFiletype( filetype )
|
2013-05-19 23:06:14 -04:00
|
|
|
|
2014-08-28 14:36:46 -04:00
|
|
|
for match in regex.finditer( line ):
|
|
|
|
if match.end() == previous_char_index:
|
|
|
|
return True
|
|
|
|
# If the whole line is whitespace, that means the user probably finished an
|
|
|
|
# identifier on the previous line.
|
|
|
|
return line[ : current_column ].isspace()
|
2013-05-19 23:06:14 -04:00
|
|
|
|
|
|
|
|
2013-12-03 17:38:23 -05:00
|
|
|
def LastEnteredCharIsIdentifierChar():
|
|
|
|
current_column = vimsupport.CurrentColumn()
|
2014-08-28 14:36:46 -04:00
|
|
|
if current_column - 1 < 0:
|
2013-12-03 17:38:23 -05:00
|
|
|
return False
|
2014-08-28 14:36:46 -04:00
|
|
|
line = vimsupport.CurrentLineContents()
|
|
|
|
filetype = vimsupport.CurrentFiletypes()[ 0 ]
|
|
|
|
return (
|
|
|
|
identifier_utils.StartOfLongestIdentifierEndingAtIndex(
|
|
|
|
line, current_column, filetype ) != current_column )
|
2013-12-03 17:38:23 -05:00
|
|
|
|
|
|
|
|
2013-06-09 22:00:49 -04:00
|
|
|
def AdjustCandidateInsertionText( candidates ):
|
|
|
|
"""This function adjusts the candidate insertion text to take into account the
|
|
|
|
text that's currently in front of the cursor.
|
|
|
|
|
|
|
|
For instance ('|' represents the cursor):
|
|
|
|
1. Buffer state: 'foo.|bar'
|
|
|
|
2. A completion candidate of 'zoobar' is shown and the user selects it.
|
|
|
|
3. Buffer state: 'foo.zoobar|bar' instead of 'foo.zoo|bar' which is what the
|
|
|
|
user wanted.
|
|
|
|
|
|
|
|
This function changes candidates to resolve that issue.
|
|
|
|
|
|
|
|
It could be argued that the user actually wants the final buffer state to be
|
|
|
|
'foo.zoobar|' (the cursor at the end), but that would be much more difficult
|
|
|
|
to implement and is probably not worth doing.
|
|
|
|
"""
|
|
|
|
|
2014-01-05 14:48:05 -05:00
|
|
|
def NewCandidateInsertionText( to_insert, text_after_cursor ):
|
|
|
|
overlap_len = OverlapLength( to_insert, text_after_cursor )
|
|
|
|
if overlap_len:
|
|
|
|
return to_insert[ :-overlap_len ]
|
2013-06-09 22:00:49 -04:00
|
|
|
return to_insert
|
|
|
|
|
2014-01-05 14:48:05 -05:00
|
|
|
text_after_cursor = vimsupport.TextAfterCursor()
|
2014-01-05 15:16:53 -05:00
|
|
|
if not text_after_cursor:
|
|
|
|
return candidates
|
2014-01-05 15:18:46 -05:00
|
|
|
|
|
|
|
new_candidates = []
|
2013-06-09 22:00:49 -04:00
|
|
|
for candidate in candidates:
|
|
|
|
if type( candidate ) is dict:
|
|
|
|
new_candidate = candidate.copy()
|
|
|
|
|
|
|
|
if not 'abbr' in new_candidate:
|
|
|
|
new_candidate[ 'abbr' ] = new_candidate[ 'word' ]
|
|
|
|
|
|
|
|
new_candidate[ 'word' ] = NewCandidateInsertionText(
|
|
|
|
new_candidate[ 'word' ],
|
2014-01-05 14:48:05 -05:00
|
|
|
text_after_cursor )
|
2013-06-09 22:00:49 -04:00
|
|
|
|
|
|
|
new_candidates.append( new_candidate )
|
|
|
|
|
|
|
|
elif type( candidate ) is str:
|
|
|
|
new_candidates.append(
|
|
|
|
{ 'abbr': candidate,
|
2014-01-05 14:48:05 -05:00
|
|
|
'word': NewCandidateInsertionText( candidate, text_after_cursor ) } )
|
2013-06-09 22:00:49 -04:00
|
|
|
return new_candidates
|
|
|
|
|
|
|
|
|
2014-01-05 14:48:05 -05:00
|
|
|
def OverlapLength( left_string, right_string ):
|
|
|
|
"""Returns the length of the overlap between two strings.
|
|
|
|
Example: "foo baro" and "baro zoo" -> 4
|
|
|
|
"""
|
|
|
|
left_string_length = len( left_string )
|
|
|
|
right_string_length = len( right_string )
|
|
|
|
|
|
|
|
if not left_string_length or not right_string_length:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# Truncate the longer string.
|
|
|
|
if left_string_length > right_string_length:
|
|
|
|
left_string = left_string[ -right_string_length: ]
|
|
|
|
elif left_string_length < right_string_length:
|
|
|
|
right_string = right_string[ :left_string_length ]
|
|
|
|
|
|
|
|
if left_string == right_string:
|
|
|
|
return min( left_string_length, right_string_length )
|
|
|
|
|
|
|
|
# Start by looking for a single character match
|
|
|
|
# and increase length until no match is found.
|
|
|
|
best = 0
|
|
|
|
length = 1
|
|
|
|
while True:
|
|
|
|
pattern = left_string[ -length: ]
|
|
|
|
found = right_string.find( pattern )
|
|
|
|
if found < 0:
|
|
|
|
return best
|
|
|
|
length += found
|
|
|
|
if left_string[ -length: ] == right_string[ :length ]:
|
|
|
|
best = length
|
|
|
|
length += 1
|