def _get_completions(body, offset, cursor_position, ipyc): """ Private equivalent of get_completions() use only for unit_testing. """ debug = getattr(ipyc, 'debug', False) completions = _deduplicate_completions( body, ipyc.completions(body, offset)) for c in completions: if not c.text: # Guard against completion machinery giving us an empty string. continue text = unicodedata.normalize('NFC', c.text) # When the first character of the completion has a zero length, # then it's probably a decomposed unicode character. E.g. caused by # the "\dot" completion. Try to compose again with the previous # character. if wcwidth(text[0]) == 0: if cursor_position + c.start > 0: char_before = body[c.start - 1] fixed_text = unicodedata.normalize( 'NFC', char_before + text) # Yield the modified completion instead, if this worked. if wcwidth(text[0:1]) == 1: yield Completion(fixed_text, start_position=c.start - offset - 1) continue # TODO: Use Jedi to determine meta_text # (Jedi currently has a bug that results in incorrect information.) # meta_text = '' # yield Completion(m, start_position=start_pos, # display_meta=meta_text) yield Completion(c.text, start_position=c.start - offset, display_meta=c.type)
def _get_completions(body, offset, cursor_position, ipyc): """ Private equivalent of get_completions() use only for unit_testing. """ debug = getattr(ipyc, "debug", False) completions = _deduplicate_completions(body, ipyc.completions(body, offset)) for c in completions: if not c.text: # Guard against completion machinery giving us an empty string. continue text = unicodedata.normalize("NFC", c.text) # When the first character of the completion has a zero length, # then it's probably a decomposed unicode character. E.g. caused by # the "\dot" completion. Try to compose again with the previous # character. if wcwidth(text[0]) == 0: if cursor_position + c.start > 0: char_before = body[c.start - 1] fixed_text = unicodedata.normalize("NFC", char_before + text) # Yield the modified completion instead, if this worked. if wcwidth(text[0:1]) == 1: yield Completion(fixed_text, start_position=c.start - offset - 1) continue # TODO: Use Jedi to determine meta_text # (Jedi currently has a bug that results in incorrect information.) # meta_text = '' # yield Completion(m, start_position=start_pos, # display_meta=meta_text) display_text = c.text adjusted_text = _adjust_completion_text_based_on_context( c.text, body, offset) if c.type == "function": yield Completion( adjusted_text, start_position=c.start - offset, display=_elide(display_text + "()"), display_meta=c.type + c.signature, ) else: yield Completion( adjusted_text, start_position=c.start - offset, display=_elide(display_text), display_meta=c.type, )
def test_deduplicate_completions(): """ Test that completions are correctly deduplicated (even if ranges are not the same) """ ip = get_ipython() ip.ex(textwrap.dedent(''' class Z: zoo = 1 ''')) with provisionalcompleter(): l = list(_deduplicate_completions('Z.z', ip.Completer.completions('Z.z', 3))) assert len(l) == 1, 'Completions (Z.z<tab>) correctly deduplicate: %s ' % l assert l[0].text == 'zoo' # and not `it.accumulate`
def test_deduplicate_completions(self): """ Test that completions are correctly deduplicated (even if ranges are not the same) """ ip = get_ipython() ip.ex( textwrap.dedent(""" class Z: zoo = 1 """)) with provisionalcompleter(): ip.Completer.use_jedi = True l = list( _deduplicate_completions("Z.z", ip.Completer.completions("Z.z", 3))) ip.Completer.use_jedi = False assert len( l) == 1, "Completions (Z.z<tab>) correctly deduplicate: %s " % l assert l[0].text == "zoo" # and not `it.accumulate`