Skip to content

Commit

Permalink
Minor improvements to completion
Browse files Browse the repository at this point in the history
If object being completed is a function, append `()` to match the
convention used in cpython doc. This include methods. Append `()` only
in the _displayed_ menu, does not insert the ().

If completion is relatively to long (mostly if is does not come from
jedi), and use a fully qualified name with more than 3 parts, ellides
the middles one to `…`. (This closes #9835).
  • Loading branch information
Carreau committed Mar 3, 2017
1 parent 3c15425 commit ca19ba9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions IPython/terminal/ptutils.py
Expand Up @@ -11,7 +11,7 @@
from wcwidth import wcwidth

from IPython.core.completer import (
IPCompleter, provisionalcompleter, rectify_completions, cursor_to_position,
provisionalcompleter, cursor_to_position,
_deduplicate_completions)
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.layout.lexers import Lexer
Expand All @@ -21,6 +21,23 @@

_completion_sentinel = object()

def _elide(string, *, min_elide=30):
"""
If a string is long enough, and has at least 2 dots,
replace the middle part with ellipses.
For example:
"""
if len(string) < min_elide:
return string

parts = string.split('.')

if len(parts) <= 3:
return string

return '{}.{}\N{HORIZONTAL ELLIPSIS}{}.{}'.format(parts[0], parts[1][0], parts[-2][-1], parts[-1])




Expand Down Expand Up @@ -90,7 +107,12 @@ def _get_completions(body, offset, cursor_position, ipyc):
# 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)
display_text = c.text

if c.type == 'function':
display_text = display_text + '()'

yield Completion(c.text, start_position=c.start - offset, display=_elide(display_text), display_meta=c.type)

class IPythonPTLexer(Lexer):
"""
Expand Down

0 comments on commit ca19ba9

Please sign in to comment.