CallTips.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
  2. # displays parameter information as you open parens.
  3. import string
  4. import sys
  5. import inspect
  6. import traceback
  7. class CallTips:
  8. menudefs = [
  9. ]
  10. keydefs = {
  11. '<<paren-open>>': ['<Key-parenleft>'],
  12. '<<paren-close>>': ['<Key-parenright>'],
  13. '<<check-calltip-cancel>>': ['<KeyRelease>'],
  14. '<<calltip-cancel>>': ['<ButtonPress>', '<Key-Escape>'],
  15. }
  16. windows_keydefs = {
  17. }
  18. unix_keydefs = {
  19. }
  20. def __init__(self, editwin):
  21. self.editwin = editwin
  22. self.text = editwin.text
  23. self.calltip = None
  24. if hasattr(self.text, "make_calltip_window"):
  25. self._make_calltip_window = self.text.make_calltip_window
  26. else:
  27. self._make_calltip_window = self._make_tk_calltip_window
  28. def close(self):
  29. self._make_calltip_window = None
  30. # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
  31. # See __init__ above for how this is used.
  32. def _make_tk_calltip_window(self):
  33. import CallTipWindow
  34. return CallTipWindow.CallTip(self.text)
  35. def _remove_calltip_window(self):
  36. if self.calltip:
  37. self.calltip.hidetip()
  38. self.calltip = None
  39. def paren_open_event(self, event):
  40. self._remove_calltip_window()
  41. arg_text = get_arg_text(self.get_object_at_cursor())
  42. if arg_text:
  43. self.calltip_start = self.text.index("insert")
  44. self.calltip = self._make_calltip_window()
  45. self.calltip.showtip(arg_text)
  46. return "" #so the event is handled normally.
  47. def paren_close_event(self, event):
  48. # Now just hides, but later we should check if other
  49. # paren'd expressions remain open.
  50. self._remove_calltip_window()
  51. return "" #so the event is handled normally.
  52. def check_calltip_cancel_event(self, event):
  53. if self.calltip:
  54. # If we have moved before the start of the calltip,
  55. # or off the calltip line, then cancel the tip.
  56. # (Later need to be smarter about multi-line, etc)
  57. if self.text.compare("insert", "<=", self.calltip_start) or \
  58. self.text.compare("insert", ">", self.calltip_start + " lineend"):
  59. self._remove_calltip_window()
  60. return "" #so the event is handled normally.
  61. def calltip_cancel_event(self, event):
  62. self._remove_calltip_window()
  63. return "" #so the event is handled normally.
  64. def get_object_at_cursor(self,
  65. wordchars="._" + string.ascii_uppercase + string.ascii_lowercase + string.digits):
  66. # XXX - This needs to be moved to a better place
  67. # so the "." attribute lookup code can also use it.
  68. text = self.text
  69. chars = text.get("insert linestart", "insert")
  70. i = len(chars)
  71. while i and chars[i-1] in wordchars:
  72. i = i-1
  73. word = chars[i:]
  74. if word:
  75. # How is this for a hack!
  76. import sys, __main__
  77. namespace = sys.modules.copy()
  78. namespace.update(__main__.__dict__)
  79. try:
  80. return eval(word, namespace)
  81. except:
  82. pass
  83. return None # Can't find an object.
  84. def _find_constructor(class_ob):
  85. # Given a class object, return a function object used for the
  86. # constructor (ie, __init__() ) or None if we can't find one.
  87. try:
  88. return class_ob.__init__
  89. except AttributeError:
  90. for base in class_ob.__bases__:
  91. rc = _find_constructor(base)
  92. if rc is not None: return rc
  93. return None
  94. def get_arg_text(ob):
  95. # Get a string describing the arguments for the given object.
  96. argText = ""
  97. if ob is not None:
  98. argOffset = 0
  99. if inspect.isclass(ob):
  100. # Look for the highest __init__ in the class chain.
  101. fob = _find_constructor(ob)
  102. if fob is None:
  103. fob = lambda: None
  104. else:
  105. fob = ob
  106. if inspect.isfunction(fob) or inspect.ismethod(fob):
  107. try:
  108. # py3k has a 'getfullargspec' which can handle py3k specific things.
  109. arg_getter = getattr(inspect, "getfullargspec", inspect.getargspec)
  110. argText = inspect.formatargspec(*arg_getter(fob))
  111. except:
  112. print("Failed to format the args")
  113. traceback.print_exc()
  114. # See if we can use the docstring
  115. if hasattr(ob, "__doc__"):
  116. doc=ob.__doc__
  117. try:
  118. doc = doc.strip()
  119. pos = doc.find("\n")
  120. except AttributeError:
  121. ## New style classes may have __doc__ slot without actually
  122. ## having a string assigned to it
  123. pass
  124. else:
  125. if pos<0 or pos>70: pos=70
  126. if argText: argText = argText + "\n"
  127. argText = argText + doc[:pos]
  128. return argText
  129. #################################################
  130. #
  131. # Test code
  132. #
  133. if __name__=='__main__':
  134. def t1(): "()"
  135. def t2(a, b=None): "(a, b=None)"
  136. def t3(a, *args): "(a, *args)"
  137. def t4(*args): "(*args)"
  138. def t5(a, *args): "(a, *args)"
  139. def t6(a, b=None, *args, **kw): "(a, b=None, *args, **kw)"
  140. class TC:
  141. "(self, a=None, *b)"
  142. def __init__(self, a=None, *b): "(self, a=None, *b)"
  143. def t1(self): "(self)"
  144. def t2(self, a, b=None): "(self, a, b=None)"
  145. def t3(self, a, *args): "(self, a, *args)"
  146. def t4(self, *args): "(self, *args)"
  147. def t5(self, a, *args): "(self, a, *args)"
  148. def t6(self, a, b=None, *args, **kw): "(self, a, b=None, *args, **kw)"
  149. def test( tests ):
  150. failed=[]
  151. for t in tests:
  152. expected = t.__doc__ + "\n" + t.__doc__
  153. if get_arg_text(t) != expected:
  154. failed.append(t)
  155. print("%s - expected %s, but got %s" % (t, repr(expected), repr(get_arg_text(t))))
  156. print("%d of %d tests failed" % (len(failed), len(tests)))
  157. tc = TC()
  158. tests = t1, t2, t3, t4, t5, t6, \
  159. TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
  160. test(tests)