David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | # CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that |
| 2 | # displays parameter information as you open parens. |
| 3 | |
| 4 | import string |
| 5 | import sys |
| 6 | import types |
| 7 | |
| 8 | class CallTips: |
| 9 | |
| 10 | menudefs = [ |
| 11 | ] |
| 12 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | def __init__(self, editwin): |
| 14 | self.editwin = editwin |
| 15 | self.text = editwin.text |
| 16 | self.calltip = None |
| 17 | if hasattr(self.text, "make_calltip_window"): |
| 18 | self._make_calltip_window = self.text.make_calltip_window |
| 19 | else: |
| 20 | self._make_calltip_window = self._make_tk_calltip_window |
| 21 | |
| 22 | def close(self): |
| 23 | self._make_calltip_window = None |
| 24 | |
| 25 | # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin. |
| 26 | # See __init__ above for how this is used. |
| 27 | def _make_tk_calltip_window(self): |
| 28 | import CallTipWindow |
| 29 | return CallTipWindow.CallTip(self.text) |
| 30 | |
| 31 | def _remove_calltip_window(self): |
| 32 | if self.calltip: |
| 33 | self.calltip.hidetip() |
| 34 | self.calltip = None |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 35 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | def paren_open_event(self, event): |
| 37 | self._remove_calltip_window() |
| 38 | arg_text = get_arg_text(self.get_object_at_cursor()) |
| 39 | if arg_text: |
| 40 | self.calltip_start = self.text.index("insert") |
| 41 | self.calltip = self._make_calltip_window() |
| 42 | self.calltip.showtip(arg_text) |
| 43 | return "" #so the event is handled normally. |
| 44 | |
| 45 | def paren_close_event(self, event): |
| 46 | # Now just hides, but later we should check if other |
| 47 | # paren'd expressions remain open. |
| 48 | self._remove_calltip_window() |
| 49 | return "" #so the event is handled normally. |
| 50 | |
| 51 | def check_calltip_cancel_event(self, event): |
| 52 | if self.calltip: |
| 53 | # If we have moved before the start of the calltip, |
| 54 | # or off the calltip line, then cancel the tip. |
| 55 | # (Later need to be smarter about multi-line, etc) |
| 56 | if self.text.compare("insert", "<=", self.calltip_start) or \ |
| 57 | self.text.compare("insert", ">", self.calltip_start + " lineend"): |
| 58 | self._remove_calltip_window() |
| 59 | return "" #so the event is handled normally. |
| 60 | |
| 61 | def calltip_cancel_event(self, event): |
| 62 | self._remove_calltip_window() |
| 63 | return "" #so the event is handled normally. |
| 64 | |
| 65 | def get_object_at_cursor(self, |
| 66 | wordchars="._" + string.uppercase + string.lowercase + string.digits): |
| 67 | # XXX - This needs to be moved to a better place |
| 68 | # so the "." attribute lookup code can also use it. |
| 69 | text = self.text |
| 70 | chars = text.get("insert linestart", "insert") |
| 71 | i = len(chars) |
| 72 | while i and chars[i-1] in wordchars: |
| 73 | i = i-1 |
| 74 | word = chars[i:] |
| 75 | if word: |
| 76 | # How is this for a hack! |
| 77 | import sys, __main__ |
| 78 | namespace = sys.modules.copy() |
| 79 | namespace.update(__main__.__dict__) |
| 80 | try: |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 81 | return eval(word, namespace) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 82 | except: |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 83 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 84 | return None # Can't find an object. |
| 85 | |
| 86 | def _find_constructor(class_ob): |
| 87 | # Given a class object, return a function object used for the |
| 88 | # constructor (ie, __init__() ) or None if we can't find one. |
| 89 | try: |
| 90 | return class_ob.__init__.im_func |
| 91 | except AttributeError: |
| 92 | for base in class_ob.__bases__: |
| 93 | rc = _find_constructor(base) |
| 94 | if rc is not None: return rc |
| 95 | return None |
| 96 | |
| 97 | def get_arg_text(ob): |
| 98 | # Get a string describing the arguments for the given object. |
| 99 | argText = "" |
| 100 | if ob is not None: |
| 101 | argOffset = 0 |
| 102 | if type(ob)==types.ClassType: |
| 103 | # Look for the highest __init__ in the class chain. |
| 104 | fob = _find_constructor(ob) |
| 105 | if fob is None: |
| 106 | fob = lambda: None |
| 107 | else: |
| 108 | argOffset = 1 |
| 109 | elif type(ob)==types.MethodType: |
| 110 | # bit of a hack for methods - turn it into a function |
| 111 | # but we drop the "self" param. |
| 112 | fob = ob.im_func |
| 113 | argOffset = 1 |
| 114 | else: |
| 115 | fob = ob |
| 116 | # Try and build one for Python defined functions |
| 117 | if type(fob) in [types.FunctionType, types.LambdaType]: |
| 118 | try: |
| 119 | realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount] |
| 120 | defaults = fob.func_defaults or [] |
| 121 | defaults = list(map(lambda name: "=%s" % name, defaults)) |
| 122 | defaults = [""] * (len(realArgs)-len(defaults)) + defaults |
| 123 | items = map(lambda arg, dflt: arg+dflt, realArgs, defaults) |
| 124 | if fob.func_code.co_flags & 0x4: |
| 125 | items.append("...") |
| 126 | if fob.func_code.co_flags & 0x8: |
| 127 | items.append("***") |
| 128 | argText = string.join(items , ", ") |
| 129 | argText = "(%s)" % argText |
| 130 | except: |
| 131 | pass |
| 132 | # See if we can use the docstring |
| 133 | if hasattr(ob, "__doc__") and ob.__doc__: |
| 134 | pos = string.find(ob.__doc__, "\n") |
| 135 | if pos<0 or pos>70: pos=70 |
| 136 | if argText: argText = argText + "\n" |
| 137 | argText = argText + ob.__doc__[:pos] |
| 138 | |
| 139 | return argText |
| 140 | |
| 141 | ################################################# |
| 142 | # |
| 143 | # Test code |
| 144 | # |
| 145 | if __name__=='__main__': |
| 146 | |
| 147 | def t1(): "()" |
| 148 | def t2(a, b=None): "(a, b=None)" |
| 149 | def t3(a, *args): "(a, ...)" |
| 150 | def t4(*args): "(...)" |
| 151 | def t5(a, *args): "(a, ...)" |
| 152 | def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)" |
| 153 | |
| 154 | class TC: |
| 155 | "(a=None, ...)" |
| 156 | def __init__(self, a=None, *b): "(a=None, ...)" |
| 157 | def t1(self): "()" |
| 158 | def t2(self, a, b=None): "(a, b=None)" |
| 159 | def t3(self, a, *args): "(a, ...)" |
| 160 | def t4(self, *args): "(...)" |
| 161 | def t5(self, a, *args): "(a, ...)" |
| 162 | def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)" |
| 163 | |
| 164 | def test( tests ): |
| 165 | failed=[] |
| 166 | for t in tests: |
| 167 | expected = t.__doc__ + "\n" + t.__doc__ |
| 168 | if get_arg_text(t) != expected: |
| 169 | failed.append(t) |
| 170 | print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`) |
| 171 | print "%d of %d tests failed" % (len(failed), len(tests)) |
| 172 | |
| 173 | tc = TC() |
| 174 | tests = t1, t2, t3, t4, t5, t6, \ |
| 175 | TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6 |
| 176 | |
| 177 | test(tests) |