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