summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Slenders <jonathan@slenders.be>2016-07-23 00:00:11 +0200
committerJonathan Slenders <jonathan@slenders.be>2016-07-23 00:00:11 +0200
commit4f653f32f1516610689c481f301f54b78d3d242a (patch)
treee2a36ad1965e53376b6fc1e5c4e76c0e73250013
parent41e1152367ba45e293006f20aa8656a670549ce3 (diff)
Improved autocompletion behaviour. See IPython issue #9658
-rw-r--r--prompt_toolkit/completion.py16
-rw-r--r--prompt_toolkit/interface.py17
-rw-r--r--prompt_toolkit/key_binding/bindings/completion.py16
3 files changed, 32 insertions, 17 deletions
diff --git a/prompt_toolkit/completion.py b/prompt_toolkit/completion.py
index 056274a1..7b26ef39 100644
--- a/prompt_toolkit/completion.py
+++ b/prompt_toolkit/completion.py
@@ -66,6 +66,22 @@ class Completion(object):
else:
return ''
+ def new_completion_from_position(self, position):
+ """
+ (Only for internal use!)
+ Get a new completion by splitting this one. Used by
+ `CommandLineInterface` when it needs to have a list of new completions
+ after inserting the common prefix.
+ """
+ assert isinstance(position, int)
+
+ return Completion(
+ text=self.text[position + self.start_position:],
+ display=self.display,
+ display_meta=self._display_meta,
+ get_display_meta=self._get_display_meta)
+
+
class CompleteEvent(object):
"""
diff --git a/prompt_toolkit/interface.py b/prompt_toolkit/interface.py
index 33f4f712..c91f3833 100644
--- a/prompt_toolkit/interface.py
+++ b/prompt_toolkit/interface.py
@@ -857,21 +857,28 @@ class CommandLineInterface(object):
set_completions = True
select_first_anyway = False
- # When the commond part has to be inserted, and there
+ # When the common part has to be inserted, and there
# is a common part.
if insert_common_part:
common_part = get_common_complete_suffix(document, completions)
if common_part:
- # Insert + run completer again.
+ # Insert the common part, update completions.
buffer.insert_text(common_part)
- async_completer()
- set_completions = False
+ if len(completions) > 1:
+ # (Don't call `async_completer` again, but
+ # recalculate completions. See:
+ # https://github.com/ipython/ipython/issues/9658)
+ completions[:] = [
+ c.new_completion_from_position(len(common_part))
+ for c in completions]
+ else:
+ set_completions = False
else:
# When we were asked to insert the "common"
# prefix, but there was no common suffix but
# still exactly one match, then select the
# first. (It could be that we have a completion
- # which does * expension, like '*.py', with
+ # which does * expansion, like '*.py', with
# exactly one match.)
if len(completions) == 1:
select_first_anyway = True
diff --git a/prompt_toolkit/key_binding/bindings/completion.py b/prompt_toolkit/key_binding/bindings/completion.py
index 6c7a9891..4903900b 100644
--- a/prompt_toolkit/key_binding/bindings/completion.py
+++ b/prompt_toolkit/key_binding/bindings/completion.py
@@ -21,19 +21,11 @@ def generate_completions(event):
"""
b = event.current_buffer
- def second_tab():
- if b.complete_state:
- b.complete_next()
- else:
- event.cli.start_completion(select_first=True)
-
- # On the second tab-press, or when already navigating through
- # completions.
- if event.is_repeat or b.complete_state:
- second_tab()
+ # When already navigating through completions, select the next one.
+ if b.complete_state:
+ b.complete_next()
else:
- event.cli.start_completion(insert_common_part=True,
- select_first=False)
+ event.cli.start_completion(insert_common_part=True, select_first=False)
def display_completions_like_readline(event):