Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 1 | """CallTips.py - An IDLE Extension to Jog Your Memory |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 3 | Call Tips are floating windows which display function/method parameter |
| 4 | information as you open the parameter parenthesis, and which disappear when you |
| 5 | type the closing parenthesis. Future plans include extending the functionality |
| 6 | to include class attributes. |
| 7 | |
| 8 | """ |
| 9 | import sys |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 10 | import string |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 11 | import types |
| 12 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 13 | import CallTipWindow |
| 14 | |
| 15 | import __main__ |
| 16 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | class CallTips: |
| 18 | |
| 19 | menudefs = [ |
| 20 | ] |
| 21 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 22 | def __init__(self, editwin=None): |
| 23 | if editwin == None: # subprocess and test |
| 24 | self.editwin = None |
| 25 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 26 | self.editwin = editwin |
| 27 | self.text = editwin.text |
| 28 | self.calltip = None |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 29 | self._make_calltip_window = self._make_tk_calltip_window |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 30 | |
| 31 | def close(self): |
| 32 | self._make_calltip_window = None |
| 33 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | def _make_tk_calltip_window(self): |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 35 | # See __init__ for usage |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | return CallTipWindow.CallTip(self.text) |
| 37 | |
| 38 | def _remove_calltip_window(self): |
| 39 | if self.calltip: |
| 40 | self.calltip.hidetip() |
| 41 | self.calltip = None |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 42 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 43 | def paren_open_event(self, event): |
| 44 | self._remove_calltip_window() |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 45 | name = self.get_name_at_cursor() |
| 46 | arg_text = self.fetch_tip(name) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | if arg_text: |
| 48 | self.calltip_start = self.text.index("insert") |
| 49 | self.calltip = self._make_calltip_window() |
| 50 | self.calltip.showtip(arg_text) |
| 51 | return "" #so the event is handled normally. |
| 52 | |
| 53 | def paren_close_event(self, event): |
| 54 | # Now just hides, but later we should check if other |
| 55 | # paren'd expressions remain open. |
| 56 | self._remove_calltip_window() |
| 57 | return "" #so the event is handled normally. |
| 58 | |
| 59 | def check_calltip_cancel_event(self, event): |
| 60 | if self.calltip: |
| 61 | # If we have moved before the start of the calltip, |
| 62 | # or off the calltip line, then cancel the tip. |
| 63 | # (Later need to be smarter about multi-line, etc) |
| 64 | if self.text.compare("insert", "<=", self.calltip_start) or \ |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 65 | self.text.compare("insert", ">", self.calltip_start |
| 66 | + " lineend"): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 67 | self._remove_calltip_window() |
| 68 | return "" #so the event is handled normally. |
| 69 | |
| 70 | def calltip_cancel_event(self, event): |
| 71 | self._remove_calltip_window() |
| 72 | return "" #so the event is handled normally. |
| 73 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 74 | __IDCHARS = "._" + string.ascii_letters + string.digits |
Kurt B. Kaiser | 92cfaf6 | 2002-09-14 00:55:21 +0000 | [diff] [blame] | 75 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 76 | def get_name_at_cursor(self): |
| 77 | idchars = self.__IDCHARS |
| 78 | str = self.text.get("insert linestart", "insert") |
| 79 | i = len(str) |
| 80 | while i and str[i-1] in idchars: |
| 81 | i -= 1 |
| 82 | return str[i:] |
| 83 | |
| 84 | def fetch_tip(self, name): |
| 85 | interp = self.editwin and self.editwin.flist.pyshell.interp |
| 86 | rpcclt = interp and interp.rpcclt |
| 87 | if rpcclt: |
| 88 | return rpcclt.remotecall("exec", "get_the_calltip", |
| 89 | (name,), {}) |
| 90 | else: |
| 91 | entity = self.get_entity(name) |
| 92 | return get_arg_text(entity) |
| 93 | |
| 94 | def get_entity(self, name): |
| 95 | if name: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 96 | namespace = sys.modules.copy() |
| 97 | namespace.update(__main__.__dict__) |
| 98 | try: |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 99 | return eval(name, namespace) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | except: |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 101 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | |
| 103 | def _find_constructor(class_ob): |
| 104 | # Given a class object, return a function object used for the |
| 105 | # constructor (ie, __init__() ) or None if we can't find one. |
| 106 | try: |
| 107 | return class_ob.__init__.im_func |
| 108 | except AttributeError: |
| 109 | for base in class_ob.__bases__: |
| 110 | rc = _find_constructor(base) |
| 111 | if rc is not None: return rc |
| 112 | return None |
| 113 | |
| 114 | def get_arg_text(ob): |
| 115 | # Get a string describing the arguments for the given object. |
| 116 | argText = "" |
| 117 | if ob is not None: |
| 118 | argOffset = 0 |
| 119 | if type(ob)==types.ClassType: |
| 120 | # Look for the highest __init__ in the class chain. |
| 121 | fob = _find_constructor(ob) |
| 122 | if fob is None: |
| 123 | fob = lambda: None |
| 124 | else: |
| 125 | argOffset = 1 |
| 126 | elif type(ob)==types.MethodType: |
| 127 | # bit of a hack for methods - turn it into a function |
| 128 | # but we drop the "self" param. |
| 129 | fob = ob.im_func |
| 130 | argOffset = 1 |
| 131 | else: |
| 132 | fob = ob |
| 133 | # Try and build one for Python defined functions |
| 134 | if type(fob) in [types.FunctionType, types.LambdaType]: |
| 135 | try: |
| 136 | realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount] |
| 137 | defaults = fob.func_defaults or [] |
| 138 | defaults = list(map(lambda name: "=%s" % name, defaults)) |
| 139 | defaults = [""] * (len(realArgs)-len(defaults)) + defaults |
| 140 | items = map(lambda arg, dflt: arg+dflt, realArgs, defaults) |
| 141 | if fob.func_code.co_flags & 0x4: |
| 142 | items.append("...") |
| 143 | if fob.func_code.co_flags & 0x8: |
| 144 | items.append("***") |
Kurt B. Kaiser | 908aece | 2002-09-15 22:02:58 +0000 | [diff] [blame] | 145 | argText = ", ".join(items) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 146 | argText = "(%s)" % argText |
| 147 | except: |
| 148 | pass |
| 149 | # See if we can use the docstring |
Kurt B. Kaiser | 908aece | 2002-09-15 22:02:58 +0000 | [diff] [blame] | 150 | doc = getattr(ob, "__doc__", "") |
| 151 | if doc: |
| 152 | while doc[:1] in " \t\n": |
| 153 | doc = doc[1:] |
| 154 | pos = doc.find("\n") |
| 155 | if pos < 0 or pos > 70: |
| 156 | pos = 70 |
| 157 | if argText: |
| 158 | argText += "\n" |
| 159 | argText += doc[:pos] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 160 | return argText |
| 161 | |
| 162 | ################################################# |
| 163 | # |
| 164 | # Test code |
| 165 | # |
| 166 | if __name__=='__main__': |
| 167 | |
| 168 | def t1(): "()" |
| 169 | def t2(a, b=None): "(a, b=None)" |
| 170 | def t3(a, *args): "(a, ...)" |
| 171 | def t4(*args): "(...)" |
| 172 | def t5(a, *args): "(a, ...)" |
| 173 | def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)" |
| 174 | |
| 175 | class TC: |
| 176 | "(a=None, ...)" |
| 177 | def __init__(self, a=None, *b): "(a=None, ...)" |
| 178 | def t1(self): "()" |
| 179 | def t2(self, a, b=None): "(a, b=None)" |
| 180 | def t3(self, a, *args): "(a, ...)" |
| 181 | def t4(self, *args): "(...)" |
| 182 | def t5(self, a, *args): "(a, ...)" |
| 183 | def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)" |
| 184 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 185 | def test(tests): |
| 186 | ct = CallTips() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 187 | failed=[] |
| 188 | for t in tests: |
| 189 | expected = t.__doc__ + "\n" + t.__doc__ |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 190 | name = t.__name__ |
| 191 | arg_text = ct.fetch_tip(name) |
| 192 | if arg_text != expected: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 193 | failed.append(t) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 194 | print "%s - expected %s, but got %s" % (t, expected, |
| 195 | get_arg_text(entity)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 196 | print "%d of %d tests failed" % (len(failed), len(tests)) |
| 197 | |
| 198 | tc = TC() |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 199 | tests = (t1, t2, t3, t4, t5, t6, |
| 200 | TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 201 | |
| 202 | test(tests) |