##// END OF EJS Templates
Support Bytes dict completions, and test it....
Matthias Bussonnier -
Show More
@@ -622,8 +622,27 b' def get__all__entries(obj):'
622 return [cast_unicode_py2(w) for w in words if isinstance(w, str)]
622 return [cast_unicode_py2(w) for w in words if isinstance(w, str)]
623
623
624
624
625 def match_dict_keys(keys, prefix, delims):
625 def match_dict_keys(keys: List[str], prefix: str, delims: str):
626 """Used by dict_key_matches, matching the prefix to a list of keys"""
626 """Used by dict_key_matches, matching the prefix to a list of keys
627
628 Parameters
629 ==========
630 keys:
631 list of keys in dictionary currently being completed.
632 prefix:
633 Part of the text already typed by the user. e.g. `mydict[b'fo`
634 delims:
635 String of delimiters to consider when finding the current key.
636
637 Returns
638 =======
639
640 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
641 ``quote`` being the quote that need to be used to close current string.
642 ``token_start`` the position where the replacement should start occurring,
643 ``matches`` a list of replacement/completion
644
645 """
627 if not prefix:
646 if not prefix:
628 return None, 0, [repr(k) for k in keys
647 return None, 0, [repr(k) for k in keys
629 if isinstance(k, (str, bytes))]
648 if isinstance(k, (str, bytes))]
@@ -639,7 +658,6 b' def match_dict_keys(keys, prefix, delims):'
639 token_start = token_match.start()
658 token_start = token_match.start()
640 token_prefix = token_match.group()
659 token_prefix = token_match.group()
641
660
642 # TODO: support bytes in Py3k
643 matched = []
661 matched = []
644 for key in keys:
662 for key in keys:
645 try:
663 try:
@@ -652,7 +670,7 b' def match_dict_keys(keys, prefix, delims):'
652 # reformat remainder of key to begin with prefix
670 # reformat remainder of key to begin with prefix
653 rem = key[len(prefix_str):]
671 rem = key[len(prefix_str):]
654 # force repr wrapped in '
672 # force repr wrapped in '
655 rem_repr = repr(rem + '"')
673 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
656 if rem_repr.startswith('u') and prefix[0] not in 'uU':
674 if rem_repr.startswith('u') and prefix[0] not in 'uU':
657 # Found key is unicode, but prefix is Py2 string.
675 # Found key is unicode, but prefix is Py2 string.
658 # Therefore attempt to interpret key as string.
676 # Therefore attempt to interpret key as string.
@@ -20,7 +20,7 b' from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory'
20 from IPython.utils.generics import complete_object
20 from IPython.utils.generics import complete_object
21 from IPython.testing import decorators as dec
21 from IPython.testing import decorators as dec
22
22
23 from IPython.core.completer import Completion, provisionalcompleter
23 from IPython.core.completer import Completion, provisionalcompleter, match_dict_keys
24 from nose.tools import assert_in, assert_not_in
24 from nose.tools import assert_in, assert_not_in
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
@@ -528,6 +528,27 b' def test_magic_completion_order():'
528 text, matches = c.complete('timeit')
528 text, matches = c.complete('timeit')
529 nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"])
529 nt.assert_equal(matches, ["timeit", "%timeit", "%%timeit"])
530
530
531 def test_match_dict_keys():
532 """
533 Test that match_dict_keys works on a couple of use case does return what
534 expected, and does not crash
535 """
536 delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
537
538
539 keys = ['foo', b'far']
540 assert match_dict_keys(keys, "b'", delims=delims) == ("'", 2 ,['far'])
541 assert match_dict_keys(keys, "b'f", delims=delims) == ("'", 2 ,['far'])
542 assert match_dict_keys(keys, 'b"', delims=delims) == ('"', 2 ,['far'])
543 assert match_dict_keys(keys, 'b"f', delims=delims) == ('"', 2 ,['far'])
544
545 assert match_dict_keys(keys, "'", delims=delims) == ("'", 1 ,['foo'])
546 assert match_dict_keys(keys, "'f", delims=delims) == ("'", 1 ,['foo'])
547 assert match_dict_keys(keys, '"', delims=delims) == ('"', 1 ,['foo'])
548 assert match_dict_keys(keys, '"f', delims=delims) == ('"', 1 ,['foo'])
549
550 match_dict_keys
551
531
552
532 def test_dict_key_completion_string():
553 def test_dict_key_completion_string():
533 """Test dictionary key completion for string keys"""
554 """Test dictionary key completion for string keys"""
General Comments 0
You need to be logged in to leave comments. Login now