Skip to content

Commit a2468e8

Browse files
committed
Highlight current argument in argument list.
1 parent aa51cc3 commit a2468e8

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

bpython/cli.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,19 +481,22 @@ def getargspec(func):
481481
# Same deal with the exceptions :(
482482
return None
483483

484+
is_bound_method = inspect.ismethod(f) and f.im_self is not None
484485
try:
485486
if inspect.isclass(f):
486487
argspec = inspect.getargspec(f.__init__)
488+
is_bound_method = True
487489
else:
488490
argspec = inspect.getargspec(f)
489-
self.argspec = [func, argspec]
491+
self.argspec = [func, argspec, is_bound_method]
490492
return True
491493

492494
except (NameError, TypeError, KeyError):
493495
t = getpydocspec(f, func)
494496
if t is None:
495497
return None
496498
self.argspec = t
499+
self.argspec.append(is_bound_method)
497500
return True
498501
except AttributeError:
499502
# This happens if no __init__ is found
@@ -502,24 +505,35 @@ def getargspec(func):
502505
if not OPTS.arg_spec:
503506
return False
504507

505-
stack = ['']
508+
stack = [['', 0]]
506509
try:
507510
for (token, value) in PythonLexer().get_tokens(self.s):
508511
if token is Token.Punctuation:
509512
if value == '(':
510-
stack.append('')
513+
stack.append(['', 0])
511514
elif value == ')':
512515
stack.pop()
516+
elif value == ',':
517+
try:
518+
stack[-1][1] += 1
519+
except TypeError:
520+
stack[-1][1] = ''
513521
elif (token is Token.Name or token in Token.Name.subtypes or
514522
token is Token.Operator and value == '.'):
515-
stack[-1] += value
523+
stack[-1][0] += value
524+
elif token is Token.Operator and value == '=':
525+
stack[-1][1] = stack[-1][0]
516526
else:
517-
stack[-1] = ''
518-
func = stack.pop() or stack.pop()
527+
stack[-1][0] = ''
528+
_, arg_number = stack.pop()
529+
func, _ = stack.pop()
519530
except IndexError:
520531
return False
521532

522-
return getargspec(func)
533+
if getargspec(func):
534+
self.argspec.append(arg_number)
535+
return True
536+
return False
523537

524538
def check(self):
525539
"""Check if paste mode should still be active and, if not, deactivate
@@ -738,6 +752,8 @@ def mkargspec(self, topline, down):
738752
kwargs = topline[1][3]
739753
_args = topline[1][1]
740754
_kwargs = topline[1][2]
755+
is_bound_method = topline[2]
756+
in_arg = topline[3]
741757
max_w = int(self.scr.getmaxyx()[1] * 0.6)
742758
self.list_win.erase()
743759
self.list_win.resize(3, max_w)
@@ -749,6 +765,9 @@ def mkargspec(self, topline, down):
749765
self.list_win.addstr(': (', get_colpair('name'))
750766
maxh = self.scr.getmaxyx()[0]
751767

768+
if is_bound_method:
769+
in_arg += 1
770+
752771
for k, i in enumerate(args):
753772
y, x = self.list_win.getyx()
754773
ln = len(str(i))
@@ -776,7 +795,10 @@ def mkargspec(self, topline, down):
776795
else:
777796
color = get_colpair('token')
778797

779-
self.list_win.addstr(str(i), color | curses.A_BOLD)
798+
if k == in_arg or i == in_arg:
799+
color |= curses.A_BOLD
800+
801+
self.list_win.addstr(str(i), color)
780802
if kw:
781803
self.list_win.addstr('=', get_colpair('punctuation'))
782804
self.list_win.addstr(kw, get_colpair('token'))

0 commit comments

Comments
 (0)