Skip to content

Commit 3ddde51

Browse files
committed
Issue bpython#264: tab complition bug fixed
1 parent aa2d90c commit 3ddde51

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -628,26 +628,30 @@ def current_word(self):
628628
so must match its definition of current word - changing how it behaves
629629
has many repercussions.
630630
"""
631-
words = re.split(r'([\w_][\w0-9._]*[(]?)', self._current_line)
632-
chars = 0
633-
cw = None
634-
for word in words:
635-
chars += len(word)
636-
if chars == self.cursor_offset_in_line and word and word.count(' ') == 0:
637-
cw = word
638-
if cw and re.match(r'^[\w_][\w0-9._]*[(]?$', cw):
639-
return cw
631+
632+
start, end, word = self._get_current_word()
633+
return word
634+
635+
def _get_current_word(self):
636+
pos = self.cursor_offset_in_line
637+
638+
matches = list(re.finditer(r'[\w_][\w0-9._]*[(]?', self._current_line))
639+
start = pos
640+
end = pos
641+
word = None
642+
for m in matches:
643+
if m.start() < pos and m.end() >= pos:
644+
start = m.start()
645+
end = m.end()
646+
word = m.group()
647+
return (start, end, word)
640648

641649
@current_word.setter
642650
def current_word(self, value):
643-
# current word means word cursor is at the end of, so delete from cursor back to [ ."']
644-
pos = self.cursor_offset_in_line - 1
645-
if pos > -1 and self._current_line[pos] not in tuple(' :)'):
646-
pos -= 1
647-
while pos > -1 and self._current_line[pos] not in tuple(' :()\'"'):
648-
pos -= 1
649-
start = pos + 1; del pos
650-
self._current_line = self._current_line[:start] + value + self._current_line[self.cursor_offset_in_line:]
651+
# current word means word cursor is at the end of
652+
start, end, word = self._get_current_word()
653+
654+
self._current_line = self._current_line[:start] + value + self._current_line[end:]
651655
self.cursor_offset_in_line = start + len(value)
652656

653657
@property

0 commit comments

Comments
 (0)