Skip to content

Commit f2e931a

Browse files
committed
Add show source feature to gtk.
1 parent 2b0da9a commit f2e931a

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

bpython/cli.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,19 +825,14 @@ def p_key(self, key):
825825
return ''
826826

827827
elif key in key_dispatch[config.show_source_key]:
828-
try:
829-
obj = self.current_func
830-
if obj is None and inspection.is_eval_safe_name(self.s):
831-
obj = self.get_object(self.s)
832-
source = inspect.getsource(obj)
833-
except (AttributeError, IOError, NameError, TypeError):
834-
self.statusbar.message("Cannot show source.")
835-
return ''
836-
else:
828+
source = self.get_source_of_current_name()
829+
if source is not None:
837830
if config.highlight_show_source:
838831
source = format(PythonLexer().get_tokens(source),
839832
TerminalFormatter())
840833
page(source)
834+
else:
835+
self.statusbar.message('Cannot show source.')
841836
return ''
842837

843838
elif key == '\n':

bpython/gtk_.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@
5050
_COLORS = dict(b='blue', c='cyan', g='green', m='magenta', r='red',
5151
w='white', y='yellow', k='black', d='black')
5252

53+
def add_tags_to_buffer(color_scheme, text_buffer):
54+
tags = dict()
55+
for (name, value) in color_scheme.iteritems():
56+
tag = tags[name] = text_buffer.create_tag(name)
57+
for (char, prop) in zip(value, ['foreground', 'background']):
58+
if char.lower() == 'd':
59+
continue
60+
tag.set_property(prop, _COLORS[char.lower()])
61+
if char.isupper():
62+
tag.set_property('weight', pango.WEIGHT_BOLD)
63+
return tags
64+
5365
class ArgspecFormatter(object):
5466
"""
5567
Format an argspec using Pango markup language.
@@ -335,15 +347,7 @@ def __init__(self, interpreter, config):
335347

336348
self.text_buffer = self.get_buffer()
337349
self.interact = GTKInteraction(self.config, Statusbar())
338-
tags = dict()
339-
for (name, value) in self.config.color_gtk_scheme.iteritems():
340-
tag = tags[name] = self.text_buffer.create_tag(name)
341-
for (char, prop) in zip(value, ['foreground', 'background']):
342-
if char.lower() == 'd':
343-
continue
344-
tag.set_property(prop, _COLORS[char.lower()])
345-
if char.isupper():
346-
tag.set_property('weight', pango.WEIGHT_BOLD)
350+
tags = add_tags_to_buffer(self.config.color_gtk_scheme, self.text_buffer)
347351
tags['prompt'].set_property('editable', False)
348352

349353
self.text_buffer.connect('delete-range', self.on_buf_delete_range)
@@ -467,7 +471,29 @@ def do_key_press_event(self, event):
467471
gtk.gdk.MOD4_MASK |
468472
gtk.gdk.SHIFT_MASK)
469473
if not state:
470-
if event.keyval == gtk.keysyms.Return:
474+
if event.keyval == gtk.keysyms.F2:
475+
source = self.get_source_of_current_name()
476+
if source is not None:
477+
win = gtk.Window()
478+
sw = gtk.ScrolledWindow()
479+
view = gtk.TextView()
480+
buffer = view.get_buffer()
481+
add_tags_to_buffer(self.config.color_gtk_scheme, buffer)
482+
from pygments.lexers import PythonLexer
483+
tokens = PythonLexer().get_tokens(source)
484+
for (token, value) in tokens:
485+
while token not in theme_map:
486+
token = token.parent
487+
iter_ = buffer.get_end_iter()
488+
buffer.insert_with_tags_by_name(iter_, value,
489+
theme_map[token])
490+
sw.add(view)
491+
win.add(sw)
492+
win.show_all()
493+
else:
494+
# XXX Error message
495+
pass
496+
elif event.keyval == gtk.keysyms.Return:
471497
if self.list_win_visible:
472498
self.list_win_visible = False
473499
self.list_win.hide()

bpython/repl.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,22 @@ def get_args(self):
488488
return True
489489
return False
490490

491+
def get_source_of_current_name(self):
492+
"""Return the source code of the object which is bound to the
493+
current name in the current input line. Return `None` if the
494+
source cannot be found."""
495+
try:
496+
obj = self.current_func
497+
if obj is None:
498+
line = self.current_line()
499+
if inspection.is_eval_safe_name(line):
500+
obj = self.get_object(line)
501+
source = inspect.getsource(obj)
502+
except (AttributeError, IOError, NameError, TypeError):
503+
return None
504+
else:
505+
return source
506+
491507
def complete(self, tab=False):
492508
"""Construct a full list of possible completions and construct and
493509
display them in a window. Also check if there's an available argspec

0 commit comments

Comments
 (0)