X Tutup
Skip to content

Commit 6cf08ed

Browse files
bpython-urwid appears to be working
can't make it crash deliberately anyway - I don't usually use it so I'll have to spend deliberate time trying to break it
1 parent 64771cd commit 6cf08ed

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

bpython/urwid.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,14 @@ def maybe_redraw(loop, self):
687687
self.main_loop.draw_screen()
688688
self._redraw_time = now
689689

690-
def current_line(self):
691-
"""Return the current line (the one the cursor is in)."""
690+
def _get_current_line(self):
692691
if self.edit is None:
693692
return ''
694693
return self.edit.get_edit_text()
694+
def _set_current_line(self, line):
695+
self.edit.set_edit_text(line)
696+
current_line = property(_get_current_line, _set_current_line, None,
697+
"Return the current line (the one the cursor is in).")
695698

696699
def cw(self):
697700
"""Return the current word (incomplete word left of cursor)."""
@@ -722,9 +725,16 @@ def cw(self):
722725
@property
723726
def cpos(self):
724727
if self.edit is not None:
725-
return len(self.current_line()) - self.edit.edit_pos
728+
return len(self.current_line) - self.edit.edit_pos
726729
return 0
727730

731+
def _get_cursor_offset(self):
732+
return self.edit.edit_pos
733+
def _set_cursor_offset(self, offset):
734+
self.edit.edit_pos = offset
735+
cursor_offset = property(_get_cursor_offset, _set_cursor_offset, None,
736+
"The cursor offset from the beginning of the line")
737+
728738
def _populate_completion(self):
729739
widget_list = self.tooltip.body
730740
while widget_list:
@@ -803,12 +813,12 @@ def _populate_completion(self):
803813
markup.append(('token', '**' + varkw))
804814
markup.append(('punctuation', ')'))
805815
widget_list.append(urwid.Text(markup))
806-
if self.matches:
816+
if self.matches_iter.matches:
807817
attr_map = {}
808818
focus_map = {'main': 'operator'}
809819
texts = [urwid.AttrMap(urwid.Text(('main', match)),
810820
attr_map, focus_map)
811-
for match in self.matches]
821+
for match in self.matches_iter.matches]
812822
width = max(text.original_widget.pack()[0] for text in texts)
813823
gridflow = urwid.GridFlow(texts, width, 1, 0, 'left')
814824
widget_list.append(gridflow)
@@ -1000,7 +1010,7 @@ def on_edit_pos_changed(self, edit, position):
10001010
"""Gets called when the cursor position inside the edit changed.
10011011
Rehighlight the current line because there might be a paren under
10021012
the cursor now."""
1003-
tokens = self.tokenize(self.current_line(), False)
1013+
tokens = self.tokenize(self.current_line, False)
10041014
edit.set_edit_markup(list(format_tokens(tokens)))
10051015

10061016
def handle_input(self, event):
@@ -1077,29 +1087,21 @@ def tab(self, back=False):
10771087
else:
10781088
cw = self.matches_iter.current_word
10791089

1080-
b = os.path.commonprefix(self.matches)
1081-
if b:
1082-
insert = b[len(cw):]
1083-
self.edit.insert_text(insert)
1084-
expanded = bool(insert)
1085-
if expanded:
1086-
self.matches_iter.update(b, self.matches)
1087-
else:
1088-
expanded = False
1089-
1090-
if not expanded and self.matches:
1091-
if self.matches_iter:
1092-
self.edit.set_edit_text(
1093-
text[:-len(self.matches_iter.current())] + cw)
1090+
if self.matches_iter.is_cseq():
1091+
cursor, text = self.matches_iter.substitute_cseq()
1092+
self.edit.set_edit_text(text)
1093+
self.edit.edit_pos = cursor
1094+
elif self.matches_iter.matches:
10941095
if back:
1095-
current_match = self.matches_iter.previous()
1096+
self.matches_iter.previous()
10961097
else:
1097-
current_match = self.matches_iter.next()
1098-
if current_match:
1099-
self.overlay.tooltip_focus = True
1100-
if self.tooltip.grid:
1101-
self.tooltip.grid.set_focus(self.matches_iter.index)
1102-
self.edit.insert_text(current_match[len(cw):])
1098+
self.matches_iter.next()
1099+
cursor, text = self.matches_iter.cur_line()
1100+
self.edit.set_edit_text(text)
1101+
self.edit.edit_pos = cursor
1102+
self.overlay.tooltip_focus = True
1103+
if self.tooltip.grid:
1104+
self.tooltip.grid.set_focus(self.matches_iter.index)
11031105
return True
11041106
finally:
11051107
self._completion_update_suppressed = False

0 commit comments

Comments
 (0)
X Tutup