Skip to content

Commit

Permalink
Merge pull request #10743 from takluyver/limit-no-completions
Browse files Browse the repository at this point in the history
Limit number of completions returned
  • Loading branch information
minrk committed Aug 18, 2017
2 parents 8cbb79f + ed0c88f commit 03bb4e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@ _build
docs/man/*.gz
docs/source/api/generated
docs/source/config/options
docs/source/config/shortcuts/*.csv
docs/source/interactive/magics-generated.txt
docs/source/config/shortcuts/*.csv
docs/gh-pages
Expand Down
16 changes: 10 additions & 6 deletions IPython/core/completer.py
Expand Up @@ -161,6 +161,9 @@
else:
PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'

# Protect against returning an enormous number of completions which the frontend
# may have trouble processing.
MATCHES_LIMIT = 500

_deprecation_readline_sentinel = object()

Expand Down Expand Up @@ -1943,7 +1946,8 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
name_text, name_matches = meth(base_text)
if name_text:
return name_text, name_matches, [meth.__qualname__]*len(name_matches), ()
return name_text, name_matches[:MATCHES_LIMIT], \
[meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()


# If no line buffer is given, assume the input text is all there was
Expand All @@ -1955,11 +1959,10 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,

# Do magic arg matches
for matcher in self.magic_arg_matchers:
matches = [(m, matcher.__qualname__) for m in matcher(line_buffer)]
matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
if matches:
matches2 = [m[0] for m in matches]
origins = [m[1] for m in matches]
return text, matches2, origins, ()
origins = [matcher.__qualname__] * len(matches)
return text, matches, origins, ()

# Start with a clean slate of completions
matches = []
Expand Down Expand Up @@ -2006,7 +2009,8 @@ def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
seen.add(t)

_filtered_matches = sorted(
set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))
set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
[:MATCHES_LIMIT]

_matches = [m[0] for m in _filtered_matches]
origins = [m[1] for m in _filtered_matches]
Expand Down

0 comments on commit 03bb4e4

Please sign in to comment.