Skip to content

Commit 4c820e1

Browse files
committed
Implement reprint_line, thanks to Trundle for explaining how it works.
1 parent d3cb425 commit 4c820e1

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

bpython/urwid.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,21 @@ class BPythonEdit(urwid.Edit):
116116

117117
"""Customized editor *very* tightly interwoven with URWIDRepl."""
118118

119-
def __init__(self, myrepl, *args, **kwargs):
120-
self.repl = myrepl
119+
def __init__(self, *args, **kwargs):
121120
self._bpy_text = ''
122121
self._bpy_attr = []
123122
self._bpy_selectable = True
124123
urwid.Edit.__init__(self, *args, **kwargs)
125-
urwid.connect_signal(self, 'change', self.on_input_change)
126124

127-
def on_input_change(self, edit, text):
128-
tokens = self.repl.tokenize(text, False)
129-
markup = list(format_tokens(tokens))
125+
def set_edit_markup(self, markup):
126+
"""Call this when markup changes but the underlying text does not.
127+
128+
You should arrange for this to be called from the 'change' signal.
129+
"""
130130
self._bpy_text, self._bpy_attr = urwid.decompose_tagmarkup(markup)
131+
# This is redundant when we're called off the 'change' signal.
132+
# I'm assuming this is cheap, making that ok.
133+
self._invalidate()
131134

132135
def get_text(self):
133136
return self._caption + self._bpy_text, self._attrib + self._bpy_attr
@@ -187,6 +190,7 @@ def __init__(self, main_loop, listbox, listwalker, tooltiptext,
187190
self.listbox = listbox
188191
self.listwalker = listwalker
189192
self.tooltiptext = tooltiptext
193+
self.edits = []
190194
self.edit = None
191195
self.statusbar = statusbar
192196
# XXX repl.Repl uses this? What is it?
@@ -251,10 +255,8 @@ def _populate_completion(self, main_loop, user_data):
251255
self.tooltiptext.set_text('NOPE')
252256

253257
def reprint_line(self, lineno, tokens):
254-
# repl calls this.
255-
# Trundle says it is responsible for paren unhighlighting.
256-
# So who cares!
257-
pass
258+
edit = self.edits[-len(self.buffer) + lineno - 1]
259+
edit.set_edit_markup(list(format_tokens(tokens)))
258260

259261
def push(self, s, insert_into_history=True):
260262
# Pretty blindly adapted from bpython.cli
@@ -271,17 +273,20 @@ def start(self):
271273
def prompt(self, more):
272274
# XXX what is s_hist?
273275
if not more:
274-
self.edit = BPythonEdit(self, caption=('prompt', '>>> '))
276+
self.edit = BPythonEdit(caption=('prompt', '>>> '))
275277
self.stdout_hist += '>>> '
276278
else:
277-
self.edit = BPythonEdit(self, caption=('prompt_more', '... '))
279+
self.edit = BPythonEdit(caption=('prompt_more', '... '))
278280
self.stdout_hist += '... '
279281

280282
urwid.connect_signal(self.edit, 'change', self.on_input_change)
283+
self.edits.append(self.edit)
281284
self.listwalker.append(self.edit)
282285
self.listbox.set_focus(len(self.listwalker) - 1)
283286

284287
def on_input_change(self, edit, text):
288+
tokens = self.tokenize(text, False)
289+
edit.set_edit_markup(list(format_tokens(tokens)))
285290
# If we call this synchronously the get_edit_text() in repl.cw
286291
# still returns the old text...
287292
self.main_loop.set_alarm_in(0, self._populate_completion)

0 commit comments

Comments
 (0)