Skip to content

Commit

Permalink
Fix a number of multi-byte errors and tracebacks
Browse files Browse the repository at this point in the history
- Correct FixIts when there are unicode characters
- Fix errors in CompleteDone handler
- Fix tracebacks when omnifunc returns unicode chars
  • Loading branch information
puremourning committed May 8, 2016
1 parent 73584b2 commit 4d7b386
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 129 deletions.
7 changes: 5 additions & 2 deletions python/ycm/omni_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import vim
from ycm import vimsupport
from ycmd import utils
from ycmd.responses import ServerError
from ycmd.completers.completer import Completer
from ycm.client.base_request import BaseRequest, HandleServerException
Expand Down Expand Up @@ -89,12 +90,14 @@ def ComputeCandidatesInner( self, request_data ):

items = vim.eval( ''.join( omnifunc_call ) )

if 'words' in items:
if hasattr( items, '__getitem__' ) and 'words' in items:
items = items[ 'words' ]

if not hasattr( items, '__iter__' ):
raise TypeError( OMNIFUNC_NOT_LIST )

return list( filter( bool, items ) )
return [ utils.ToUnicode( i ) for i in items if bool( i ) ]

except ( TypeError, ValueError, vim.error ) as error:
vimsupport.PostVimMessage(
OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
Expand Down
85 changes: 64 additions & 21 deletions python/ycm/tests/postcomplete_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
#
# Copyright (C) 2015 YouCompleteMe contributors
#
# This file is part of YouCompleteMe.
Expand Down Expand Up @@ -26,6 +28,8 @@
from ycm.test_utils import MockVimModule
MockVimModule()

from ycmd.utils import ToBytes

import contextlib
from hamcrest import assert_that, empty
from mock import MagicMock, DEFAULT, patch
Expand All @@ -40,11 +44,11 @@ def GetVariableValue_CompleteItemIs( word, abbr = None, menu = None,
def Result( variable ):
if variable == 'v:completed_item':
return {
'word': word,
'abbr': abbr,
'menu': menu,
'info': info,
'kind': kind,
'word': ToBytes( word ),
'abbr': ToBytes( abbr ),
'menu': ToBytes( menu ),
'info': ToBytes( info ),
'kind': ToBytes( kind ),
}
return DEFAULT
return MagicMock( side_effect = Result )
Expand Down Expand Up @@ -115,7 +119,7 @@ def OnCompleteDone_NoActionNoError_test( self, *args ):
@patch( 'ycm.vimsupport.GetVariableValue',
GetVariableValue_CompleteItemIs( 'Test' ) )
def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
eq_( list( result ), completions )

Expand All @@ -125,7 +129,7 @@ def FilterToCompletedCompletions_NewVim_MatchIsReturned_test( self, *args ):
GetVariableValue_CompleteItemIs( 'A' ) )
def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( self,
*args ):
completions = [ BuildCompletion( 'AAA' ) ]
completions = [ BuildCompletion( insertion_text = 'AAA' ) ]
self.ycm._FilterToMatchingCompletions( completions, False )


Expand All @@ -134,7 +138,7 @@ def FilterToCompletedCompletions_NewVim_ShortTextDoesntRaise_test( self,
GetVariableValue_CompleteItemIs( 'Test' ) )
def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( self,
*args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
eq_( list( result ), completions )

Expand All @@ -144,15 +148,24 @@ def FilterToCompletedCompletions_NewVim_ExactMatchIsReturned_test( self,
GetVariableValue_CompleteItemIs( ' Quote' ) )
def FilterToCompletedCompletions_NewVim_NonMatchIsntReturned_test( self,
*args ):
completions = [ BuildCompletion( 'A' ) ]
completions = [ BuildCompletion( insertion_text = 'A' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
assert_that( list( result ), empty() )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
@patch( 'ycm.vimsupport.GetVariableValue',
GetVariableValue_CompleteItemIs( '†es†' ) )
def FilterToCompletedCompletions_NewVim_Unicode_test( self, *args ):
completions = [ BuildCompletion( insertion_text = '†es†' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
eq_( list( result ), completions )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Test' )
def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
eq_( list( result ), completions )

Expand All @@ -161,15 +174,15 @@ def FilterToCompletedCompletions_OldVim_MatchIsReturned_test( self, *args ):
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' )
def FilterToCompletedCompletions_OldVim_ShortTextDoesntRaise_test( self,
*args ):
completions = [ BuildCompletion( 'AAA' ) ]
completions = [ BuildCompletion( insertion_text = 'AAA' ) ]
self.ycm._FilterToMatchingCompletions( completions, False )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' )
def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( self,
*args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
eq_( list( result ), completions )

Expand All @@ -178,7 +191,15 @@ def FilterToCompletedCompletions_OldVim_ExactMatchIsReturned_test( self,
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( self,
*args ):
completions = [ BuildCompletion( 'A' ) ]
completions = [ BuildCompletion( insertion_text = 'A' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
assert_that( list( result ), empty() )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniçø∂¢' )
def FilterToCompletedCompletions_OldVim_Unicode_test( self, *args ):
completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ]
result = self.ycm._FilterToMatchingCompletions( completions, False )
assert_that( list( result ), empty() )

Expand All @@ -187,7 +208,7 @@ def FilterToCompletedCompletions_OldVim_NonMatchIsntReturned_test( self,
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Te' )
def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, True )
Expand All @@ -197,15 +218,15 @@ def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_MatchIsReturned_test(
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'X' )
def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ShortTextDoesntRaise_test( # noqa
self, *args ):
completions = [ BuildCompletion( "AAA" ) ]
completions = [ BuildCompletion( insertion_text = "AAA" ) ]
self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Test' )
def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, False )
Expand All @@ -215,19 +236,29 @@ def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_ExactMatchIsntReturned
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_NonMatchIsntReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'A' ) ]
completions = [ BuildCompletion( insertion_text = 'A' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, False )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = False )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' )
def HasCompletionsThatCouldBeCompletedWithMoreText_OldVim_Unicode_test(
self, *args ):
completions = [ BuildCompletion( insertion_text = 'Uniçø∂¢' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, True )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
@patch( 'ycm.vimsupport.GetVariableValue',
GetVariableValue_CompleteItemIs( 'Te' ) )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, True )
Expand All @@ -239,7 +270,7 @@ def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_MatchIsReturned_test(
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'AAA' ) ]
completions = [ BuildCompletion( insertion_text = 'AAA' ) ]
self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText( completions )


Expand All @@ -249,7 +280,7 @@ def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ShortTextDoesntRaise_t
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( 'Test' ) ]
completions = [ BuildCompletion( insertion_text = 'Test' ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, False )
Expand All @@ -261,12 +292,24 @@ def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_ExactMatchIsntReturned
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = ' Quote' )
def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_NonMatchIsntReturned_test( # noqa
self, *args ):
completions = [ BuildCompletion( "A" ) ]
completions = [ BuildCompletion( insertion_text = "A" ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, False )


@patch( 'ycm.vimsupport.VimVersionAtLeast', return_value = True )
@patch( 'ycm.vimsupport.GetVariableValue',
GetVariableValue_CompleteItemIs( 'Uniç' ) )
@patch( 'ycm.vimsupport.TextBeforeCursor', return_value = 'Uniç' )
def HasCompletionsThatCouldBeCompletedWithMoreText_NewVim_Unicode_test(
self, *args ):
completions = [ BuildCompletion( insertion_text = "Uniçø∂¢" ) ]
result = self.ycm._HasCompletionsThatCouldBeCompletedWithMoreText(
completions )
eq_( result, True )


def GetRequiredNamespaceImport_ReturnNoneForNoExtraData_test( self ):
eq_( None, self.ycm._GetRequiredNamespaceImport( {} ) )

Expand Down
Loading

0 comments on commit 4d7b386

Please sign in to comment.