Simplify AdjustCandidateInsertionText function

Candidates are always dictionaries containing the 'word' key.
This commit is contained in:
micbou 2018-08-19 20:40:46 +02:00
parent 73fea03d78
commit 926e32d6e5
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
2 changed files with 23 additions and 30 deletions

View File

@ -111,22 +111,16 @@ def AdjustCandidateInsertionText( candidates ):
new_candidates = [] new_candidates = []
for candidate in candidates: for candidate in candidates:
if isinstance( candidate, dict ): new_candidate = candidate.copy()
new_candidate = candidate.copy()
if 'abbr' not in new_candidate: if 'abbr' not in new_candidate:
new_candidate[ 'abbr' ] = new_candidate[ 'word' ] new_candidate[ 'abbr' ] = new_candidate[ 'word' ]
new_candidate[ 'word' ] = NewCandidateInsertionText( new_candidate[ 'word' ] = NewCandidateInsertionText(
new_candidate[ 'word' ], new_candidate[ 'word' ],
text_after_cursor ) text_after_cursor )
new_candidates.append( new_candidate ) new_candidates.append( new_candidate )
elif isinstance( candidate, str ) or isinstance( candidate, bytes ):
new_candidates.append(
{ 'abbr': candidate,
'word': NewCandidateInsertionText( candidate, text_after_cursor ) } )
return new_candidates return new_candidates

View File

@ -57,49 +57,49 @@ def MockTextAfterCursor( text ):
def AdjustCandidateInsertionText_Basic_test(): def AdjustCandidateInsertionText_Basic_test():
with MockTextAfterCursor( 'bar' ): with MockTextAfterCursor( 'bar' ):
eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar' } ] ) )
def AdjustCandidateInsertionText_ParenInTextAfterCursor_test(): def AdjustCandidateInsertionText_ParenInTextAfterCursor_test():
with MockTextAfterCursor( 'bar(zoo' ): with MockTextAfterCursor( 'bar(zoo' ):
eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar' } ] ) )
def AdjustCandidateInsertionText_PlusInTextAfterCursor_test(): def AdjustCandidateInsertionText_PlusInTextAfterCursor_test():
with MockTextAfterCursor( 'bar+zoo' ): with MockTextAfterCursor( 'bar+zoo' ):
eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar' } ] ) )
def AdjustCandidateInsertionText_WhitespaceInTextAfterCursor_test(): def AdjustCandidateInsertionText_WhitespaceInTextAfterCursor_test():
with MockTextAfterCursor( 'bar zoo' ): with MockTextAfterCursor( 'bar zoo' ):
eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar' } ] ) )
def AdjustCandidateInsertionText_MoreThanWordMatchingAfterCursor_test(): def AdjustCandidateInsertionText_MoreThanWordMatchingAfterCursor_test():
with MockTextAfterCursor( 'bar.h' ): with MockTextAfterCursor( 'bar.h' ):
eq_( [ { 'abbr': 'foobar.h', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar.h', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar.h' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar.h' } ] ) )
with MockTextAfterCursor( 'bar(zoo' ): with MockTextAfterCursor( 'bar(zoo' ):
eq_( [ { 'abbr': 'foobar(zoo', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar(zoo', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( [ 'foobar(zoo' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foobar(zoo' } ] ) )
def AdjustCandidateInsertionText_NotSuffix_test(): def AdjustCandidateInsertionText_NotSuffix_test():
with MockTextAfterCursor( 'bar' ): with MockTextAfterCursor( 'bar' ):
eq_( [ { 'abbr': 'foofoo', 'word': 'foofoo' } ], eq_( [ { 'abbr': 'foofoo', 'word': 'foofoo' } ],
base.AdjustCandidateInsertionText( [ 'foofoo' ] ) ) base.AdjustCandidateInsertionText( [ { 'word': 'foofoo' } ] ) )
def AdjustCandidateInsertionText_NothingAfterCursor_test(): def AdjustCandidateInsertionText_NothingAfterCursor_test():
with MockTextAfterCursor( '' ): with MockTextAfterCursor( '' ):
eq_( [ 'foofoo', eq_( [ { 'word': 'foofoo' },
'zobar' ], { 'word': 'zobar' } ],
base.AdjustCandidateInsertionText( [ 'foofoo', base.AdjustCandidateInsertionText( [ { 'word': 'foofoo' },
'zobar' ] ) ) { 'word': 'zobar' } ] ) )
def AdjustCandidateInsertionText_MultipleStrings_test(): def AdjustCandidateInsertionText_MultipleStrings_test():
@ -108,17 +108,16 @@ def AdjustCandidateInsertionText_MultipleStrings_test():
{ 'abbr': 'zobar', 'word': 'zo' }, { 'abbr': 'zobar', 'word': 'zo' },
{ 'abbr': 'qbar', 'word': 'q' }, { 'abbr': 'qbar', 'word': 'q' },
{ 'abbr': 'bar', 'word': '' }, ], { 'abbr': 'bar', 'word': '' }, ],
base.AdjustCandidateInsertionText( [ 'foobar', base.AdjustCandidateInsertionText( [ { 'word': 'foobar' },
'zobar', { 'word': 'zobar' },
'qbar', { 'word': 'qbar' },
'bar' ] ) ) { 'word': 'bar' } ] ) )
def AdjustCandidateInsertionText_DictInput_test(): def AdjustCandidateInsertionText_DictInput_test():
with MockTextAfterCursor( 'bar' ): with MockTextAfterCursor( 'bar' ):
eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ], eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
base.AdjustCandidateInsertionText( base.AdjustCandidateInsertionText( [ { 'word': 'foobar' } ] ) )
[ { 'word': 'foobar' } ] ) )
def AdjustCandidateInsertionText_DontTouchAbbr_test(): def AdjustCandidateInsertionText_DontTouchAbbr_test():