4545import pydoc
4646import types
4747import unicodedata
48+ import textwrap
4849from cStringIO import StringIO
4950from locale import LC_ALL , getpreferredencoding , setlocale
5051from optparse import OptionParser
@@ -460,6 +461,8 @@ def get_args(self):
460461 argspec() for it. On success, update self.argspec and return True,
461462 otherwise set self.argspec to None and return False"""
462463
464+ self .current_func = None
465+
463466 def getpydocspec (f , func ):
464467 try :
465468 argspec = pydoc .getdoc (f )
@@ -483,21 +486,25 @@ def getargspec(func):
483486 f = self .interp .locals [func ]
484487 except TypeError :
485488 return None
486-
487489 else :
488490 try :
489491 f = eval (func , self .interp .locals )
490492 except Exception :
491493# Same deal with the exceptions :(
492494 return None
495+ else :
496+ self .current_func = f
493497
494498 is_bound_method = inspect .ismethod (f ) and f .im_self is not None
495499 try :
496500 if inspect .isclass (f ):
501+ self .current_func = f
497502 argspec = inspect .getargspec (f .__init__ )
503+ self .current_func = f .__init__
498504 is_bound_method = True
499505 else :
500506 argspec = inspect .getargspec (f )
507+ self .current_func = f
501508 self .argspec = [func , argspec , is_bound_method ]
502509 return True
503510
@@ -615,11 +622,15 @@ def _complete(self, unused_tab=False):
615622 else :
616623 matches = self .completer .matches
617624
625+ self .docstring = None
626+
618627 if e or not matches :
619628 self .matches = []
620629 if not self .argspec :
621630 self .scr .redrawwin ()
622631 return False
632+ if self .current_func is not None :
633+ self .docstring = getattr (self .current_func , '__doc__' , None )
623634
624635 if not e and matches :
625636# remove duplicates and restore order
@@ -646,7 +657,6 @@ def show_list(self, items, topline=None):
646657 else :
647658 max_h = y + 1
648659 max_w = int (w * 0.8 )
649-
650660 self .list_win .erase ()
651661 if items and '.' in items [0 ]:
652662 items = [x .rsplit ('.' )[- 1 ] for x in items ]
@@ -713,7 +723,17 @@ def lsize():
713723 if height_offset and display_rows + 5 >= max_h :
714724 del v_items [- (cols * (height_offset )):]
715725
716- self .list_win .resize (rows + 2 , w )
726+ if self .docstring is None :
727+ self .list_win .resize (rows + 2 , w )
728+ else :
729+ self .list_win .resize (max_h , max_w )
730+ docstring = []
731+ for paragraph in self .docstring .split ('\n ' ):
732+ for line in textwrap .wrap (paragraph , max_w - 2 ):
733+ docstring .append ('\n %s' % (line ,))
734+ docstring = docstring [:max_h ]
735+ docstring_string = '' .join (docstring )
736+ rows = len (docstring ) + 1
717737
718738 if down :
719739 self .list_win .mvwin (y + 1 , 0 )
@@ -731,6 +751,8 @@ def lsize():
731751 and ix + 1 < len (v_items )):
732752 self .list_win .addstr ('\n ' )
733753
754+ if self .docstring is not None :
755+ self .list_win .addstr (docstring_string , get_colpair ('comment' ))
734756# XXX: After all the trouble I had with sizing the list box (I'm not very good
735757# at that type of thing) I decided to do this bit of tidying up here just to
736758# make sure there's no unnececessary blank lines, it makes things look nicer.
0 commit comments