Skip to content

Commit 9281dcc

Browse files
committed
Refactor
1 parent f44b8c2 commit 9281dcc

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

bpython/autocomplete.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,10 @@ def matches(
555555
if r is None:
556556
return None
557557

558-
matches = set()
559558
n = len(r.word)
560-
for word in KEYWORDS:
561-
if self.method_match(word, n, r.word):
562-
matches.add(word)
559+
matches = {
560+
word for word in KEYWORDS if self.method_match(word, n, r.word)
561+
}
563562
for nspace in (builtins.__dict__, locals_):
564563
for word, val in nspace.items():
565564
# if identifier isn't ascii, don't complete (syntax error)
@@ -652,27 +651,36 @@ def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
652651

653652
else:
654653

655-
class JediCompletion(BaseCompletionType):
654+
class MultilineJediCompletion(BaseCompletionType): # type: ignore [no-redef]
656655
_orig_start: Optional[int]
657656

658657
def matches(
659658
self,
660659
cursor_offset: int,
661660
line: str,
662661
*,
662+
current_block: Optional[str] = None,
663663
history: Optional[List[str]] = None,
664664
**kwargs: Any,
665665
) -> Optional[Set[str]]:
666-
if history is None:
667-
return None
668-
if not lineparts.current_word(cursor_offset, line):
666+
if (
667+
current_block is None
668+
or history is None
669+
or "\n" not in current_block
670+
or not lineparts.current_word(cursor_offset, line)
671+
):
669672
return None
670673

674+
assert cursor_offset <= len(line), "{!r} {!r}".format(
675+
cursor_offset,
676+
line,
677+
)
678+
671679
combined_history = "\n".join(itertools.chain(history, (line,)))
672680
try:
673681
script = jedi.Script(combined_history, path="fake.py")
674682
completions = script.complete(
675-
len(combined_history.splitlines()), cursor_offset
683+
combined_history.count("\n") + 1, cursor_offset
676684
)
677685
except (jedi.NotFoundError, IndexError, KeyError):
678686
# IndexError for #483
@@ -688,8 +696,6 @@ def matches(
688696
return None
689697
assert isinstance(self._orig_start, int)
690698

691-
first_letter = line[self._orig_start : self._orig_start + 1]
692-
693699
matches = [c.name for c in completions]
694700
if any(
695701
not m.lower().startswith(matches[0][0].lower()) for m in matches
@@ -699,35 +705,15 @@ def matches(
699705
return None
700706
else:
701707
# case-sensitive matches only
708+
first_letter = line[self._orig_start]
702709
return {m for m in matches if m.startswith(first_letter)}
703710

704711
def locate(self, cursor_offset: int, line: str) -> LinePart:
705-
assert isinstance(self._orig_start, int)
712+
assert self._orig_start is not None
706713
start = self._orig_start
707714
end = cursor_offset
708715
return LinePart(start, end, line[start:end])
709716

710-
class MultilineJediCompletion(JediCompletion): # type: ignore [no-redef]
711-
def matches(
712-
self,
713-
cursor_offset: int,
714-
line: str,
715-
*,
716-
current_block: Optional[str] = None,
717-
history: Optional[List[str]] = None,
718-
**kwargs: Any,
719-
) -> Optional[Set[str]]:
720-
if current_block is None or history is None:
721-
return None
722-
if "\n" not in current_block:
723-
return None
724-
725-
assert cursor_offset <= len(line), "{!r} {!r}".format(
726-
cursor_offset,
727-
line,
728-
)
729-
return super().matches(cursor_offset, line, history=history)
730-
731717

732718
def get_completer(
733719
completers: Sequence[BaseCompletionType],

0 commit comments

Comments
 (0)