Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 1 | import string |
| 2 | import sys |
| 3 | import types |
| 4 | |
| 5 | class CallTips: |
| 6 | |
| 7 | menudefs = [ |
| 8 | ] |
| 9 | |
| 10 | keydefs = { |
| 11 | '<<paren-open>>': ['<Key-parenleft>'], |
| 12 | '<<paren-close>>': ['<Key-parenright>'], |
Guido van Rossum | 094189f | 1999-06-02 18:18:57 +0000 | [diff] [blame^] | 13 | '<<check-calltip-cancel>>': ['<KeyRelease>'], |
| 14 | '<<calltip-cancel>>': ['<ButtonPress>', '<Key-Escape>'], |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 15 | } |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 17 | windows_keydefs = { |
| 18 | } |
| 19 | |
| 20 | unix_keydefs = { |
| 21 | } |
| 22 | |
| 23 | def __init__(self, editwin): |
| 24 | self.editwin = editwin |
| 25 | self.text = editwin.text |
| 26 | self.calltip = None |
| 27 | if hasattr(self.text, "make_calltip_window"): |
| 28 | self._make_calltip_window = self.text.make_calltip_window |
| 29 | else: |
| 30 | self._make_calltip_window = self._make_tk_calltip_window |
| 31 | |
| 32 | def _make_tk_calltip_window(self): |
| 33 | import CallTipWindow |
| 34 | return CallTipWindow.CallTip(self.text) |
| 35 | |
| 36 | def _remove_calltip_window(self): |
| 37 | if self.calltip: |
| 38 | self.calltip.hidetip() |
| 39 | self.calltip = None |
| 40 | |
| 41 | def paren_open_event(self, event): |
| 42 | self._remove_calltip_window() |
| 43 | arg_text = get_arg_text(self.get_object_at_cursor()) |
| 44 | if arg_text: |
| 45 | self.calltip_start = self.text.index("insert") |
| 46 | self.calltip = self._make_calltip_window() |
| 47 | self.calltip.showtip(arg_text) |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 48 | return "" #so the event is handled normally. |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 49 | |
| 50 | def paren_close_event(self, event): |
| 51 | # Now just hides, but later we should check if other |
| 52 | # paren'd expressions remain open. |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 53 | self._remove_calltip_window() |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 54 | return "" #so the event is handled normally. |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 55 | |
| 56 | def check_calltip_cancel_event(self, event): |
| 57 | # This doesnt quite work correctly as it is processed |
| 58 | # _before_ the key is handled. Thus, when the "Up" key |
| 59 | # is pressed, this test happens before the cursor is moved. |
| 60 | # This will do for now. |
| 61 | if self.calltip: |
| 62 | # If we have moved before the start of the calltip, |
| 63 | # or off the calltip line, then cancel the tip. |
| 64 | # (Later need to be smarter about multi-line, etc) |
| 65 | if self.text.compare("insert", "<=", self.calltip_start) or \ |
| 66 | self.text.compare("insert", ">", self.calltip_start + " lineend"): |
| 67 | self._remove_calltip_window() |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 74 | def get_object_at_cursor(self, |
| 75 | wordchars="._" + string.uppercase + string.lowercase + string.digits): |
| 76 | # XXX - This need to be moved to a better place |
| 77 | # as the "." attribute lookup code can also use it. |
| 78 | text = self.text |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 79 | chars = text.get("insert linestart", "insert") |
| 80 | i = len(chars) |
| 81 | while i and chars[i-1] in wordchars: |
| 82 | i = i-1 |
| 83 | word = chars[i:] |
| 84 | if word: |
| 85 | # How is this for a hack! |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 86 | import sys, __main__ |
| 87 | namespace = sys.modules.copy() |
| 88 | namespace.update(__main__.__dict__) |
| 89 | try: |
| 90 | return eval(word, namespace) |
| 91 | except: |
| 92 | pass |
| 93 | return None # Can't find an object. |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 95 | def get_arg_text(ob): |
| 96 | # Get a string describing the arguments for the given object. |
| 97 | argText = "" |
| 98 | if ob is not None: |
| 99 | argOffset = 0 |
| 100 | # bit of a hack for methods - turn it into a function |
| 101 | # but we drop the "self" param. |
| 102 | if type(ob)==types.MethodType: |
| 103 | ob = ob.im_func |
| 104 | argOffset = 1 |
| 105 | # Try and build one for Python defined functions |
| 106 | if type(ob) in [types.FunctionType, types.LambdaType]: |
| 107 | try: |
| 108 | realArgs = ob.func_code.co_varnames[argOffset:ob.func_code.co_argcount] |
| 109 | defaults = ob.func_defaults or [] |
| 110 | defaults = list(map(lambda name: "=%s" % name, defaults)) |
| 111 | defaults = [""] * (len(realArgs)-len(defaults)) + defaults |
| 112 | argText = string.join( map(lambda arg, dflt: arg+dflt, realArgs, defaults), ", ") |
| 113 | if len(realArgs)+argOffset < (len(ob.func_code.co_varnames) - len(ob.func_code.co_names) ): |
| 114 | if argText: argText = argText + ", " |
| 115 | argText = argText + "..." |
| 116 | argText = "(%s)" % argText |
| 117 | except: |
| 118 | pass |
| 119 | # Can't build an argument list - see if we can use a docstring. |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 120 | if hasattr(ob, "__doc__") and ob.__doc__: |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 121 | pos = string.find(ob.__doc__, "\n") |
| 122 | if pos<0 or pos>70: pos=70 |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 123 | if argText: argText = argText + "\n" |
| 124 | argText = argText + ob.__doc__[:pos] |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 125 | |
| 126 | return argText |
| 127 | |
| 128 | ################################################# |
| 129 | # |
| 130 | # Test code |
| 131 | # |
| 132 | if __name__=='__main__': |
| 133 | |
| 134 | def t1(): "()" |
| 135 | def t2(a, b=None): "(a, b=None)" |
| 136 | def t3(a, *args): "(a, ...)" |
| 137 | def t4(*args): "(...)" |
| 138 | def t5(a, *args): "(a, ...)" |
| 139 | def t6(a, b=None, *args, **kw): "(a, b=None, ...)" |
| 140 | |
| 141 | class TC: |
| 142 | def t1(self): "()" |
| 143 | def t2(self, a, b=None): "(a, b=None)" |
| 144 | def t3(self, a, *args): "(a, ...)" |
| 145 | def t4(self, *args): "(...)" |
| 146 | def t5(self, a, *args): "(a, ...)" |
| 147 | def t6(self, a, b=None, *args, **kw): "(a, b=None, ...)" |
| 148 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 149 | def test( tests ): |
| 150 | failed=[] |
| 151 | for t in tests: |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 152 | if get_arg_text(t) != t.__doc__ + "\n" + t.__doc__: |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 153 | failed.append(t) |
| 154 | print "%s - expected %s, but got %s" % (t, `t.__doc__`, `get_arg_text(t)`) |
| 155 | print "%d of %d tests failed" % (len(failed), len(tests)) |
| 156 | |
Guido van Rossum | 6290dab | 1999-06-02 18:12:55 +0000 | [diff] [blame] | 157 | tc = TC() |
| 158 | tests = t1, t2, t3, t4, t5, t6, \ |
| 159 | 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] | 160 | |
| 161 | test(tests) |