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 | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 3 | Call Tips are floating windows which display function, class, and method |
| 4 | parameter and docstring information when you type an opening parenthesis, and |
| 5 | which disappear when you type a closing parenthesis. |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 7 | """ |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 8 | import __main__ |
| 9 | import inspect |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10 | import re |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 11 | import sys |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 12 | import textwrap |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | import types |
| 14 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 15 | from idlelib import CallTipWindow |
| 16 | from idlelib.HyperParser import HyperParser |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 17 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | class CallTips: |
| 19 | |
| 20 | menudefs = [ |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 21 | ('edit', [ |
| 22 | ("Show call tip", "<<force-open-calltip>>"), |
| 23 | ]) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 24 | ] |
| 25 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 26 | def __init__(self, editwin=None): |
Raymond Hettinger | c5e378d | 2004-05-04 08:34:56 +0000 | [diff] [blame] | 27 | if editwin is None: # subprocess and test |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 28 | self.editwin = None |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 29 | else: |
| 30 | self.editwin = editwin |
| 31 | self.text = editwin.text |
| 32 | self.active_calltip = None |
| 33 | self._calltip_window = self._make_tk_calltip_window |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | |
| 35 | def close(self): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 36 | self._calltip_window = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 37 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 38 | def _make_tk_calltip_window(self): |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 39 | # See __init__ for usage |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 40 | return CallTipWindow.CallTip(self.text) |
| 41 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 42 | def _remove_calltip_window(self, event=None): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 43 | if self.active_calltip: |
| 44 | self.active_calltip.hidetip() |
| 45 | self.active_calltip = None |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 46 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 47 | def force_open_calltip_event(self, event): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 48 | "The user selected the menu entry or hotkey, open the tip." |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 49 | self.open_calltip(True) |
| 50 | |
| 51 | def try_open_calltip_event(self, event): |
| 52 | """Happens when it would be nice to open a CallTip, but not really |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 53 | necessary, for example after an opening bracket, so function calls |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 54 | won't be made. |
| 55 | """ |
| 56 | self.open_calltip(False) |
| 57 | |
| 58 | def refresh_calltip_event(self, event): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 59 | if self.active_calltip and self.active_calltip.is_active(): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 60 | self.open_calltip(False) |
| 61 | |
| 62 | def open_calltip(self, evalfuncs): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 63 | self._remove_calltip_window() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 64 | |
| 65 | hp = HyperParser(self.editwin, "insert") |
| 66 | sur_paren = hp.get_surrounding_brackets('(') |
| 67 | if not sur_paren: |
| 68 | return |
| 69 | hp.set_index(sur_paren[0]) |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 70 | expression = hp.get_expression() |
| 71 | if not expression: |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 72 | return |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 73 | if not evalfuncs and (expression.find('(') != -1): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 74 | return |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 75 | argspec = self.fetch_tip(expression) |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 76 | if not argspec: |
| 77 | return |
| 78 | self.active_calltip = self._calltip_window() |
| 79 | self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 80 | |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 81 | def fetch_tip(self, expression): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 82 | """Return the argument list and docstring of a function or class. |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 83 | |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 84 | If there is a Python subprocess, get the calltip there. Otherwise, |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 85 | either this fetch_tip() is running in the subprocess or it was |
| 86 | called in an IDLE running without the subprocess. |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 87 | |
| 88 | The subprocess environment is that of the most recently run script. If |
| 89 | two unrelated modules are being edited some calltips in the current |
| 90 | module may be inoperative if the module was not the last to run. |
| 91 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 92 | To find methods, fetch_tip must be fed a fully qualified name. |
| 93 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 94 | """ |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 95 | try: |
| 96 | rpcclt = self.editwin.flist.pyshell.interp.rpcclt |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 97 | except AttributeError: |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 98 | rpcclt = None |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 99 | if rpcclt: |
| 100 | return rpcclt.remotecall("exec", "get_the_calltip", |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 101 | (expression,), {}) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 102 | else: |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 103 | return get_argspec(get_entity(expression)) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 104 | |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 105 | def get_entity(expression): |
| 106 | """Return the object corresponding to expression evaluated |
| 107 | in a namespace spanning sys.modules and __main.dict__. |
| 108 | """ |
| 109 | if expression: |
| 110 | namespace = sys.modules.copy() |
| 111 | namespace.update(__main__.__dict__) |
| 112 | try: |
| 113 | return eval(expression, namespace) |
| 114 | except BaseException: |
| 115 | # An uncaught exception closes idle, and eval can raise any |
| 116 | # exception, especially if user classes are involved. |
| 117 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 119 | # The following are used in get_argspec and some in tests |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 120 | _MAX_COLS = 85 |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 121 | _MAX_LINES = 5 # enough for bytes |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 122 | _INDENT = ' '*4 # for wrapped signatures |
Terry Jan Reedy | 44dea9d | 2012-07-09 00:13:21 -0400 | [diff] [blame] | 123 | _first_param = re.compile('(?<=\()\w*\,?\s*') |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 124 | _default_callable_argspec = "See source or doc" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 125 | |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 126 | |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 127 | def get_argspec(ob): |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 128 | '''Return a string describing the signature of a callable object, or ''. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 129 | |
| 130 | For Python-coded functions and methods, the first line is introspected. |
| 131 | Delete 'self' parameter for classes (.__init__) and bound methods. |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 132 | The next lines are the first lines of the doc string up to the first |
| 133 | empty line or _MAX_LINES. For builtins, this typically includes |
| 134 | the arguments in addition to the return value. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 135 | ''' |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 136 | argspec = "" |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 137 | try: |
| 138 | ob_call = ob.__call__ |
| 139 | except BaseException: |
| 140 | return argspec |
| 141 | if isinstance(ob, type): |
| 142 | fob = ob.__init__ |
| 143 | elif isinstance(ob_call, types.MethodType): |
| 144 | fob = ob_call |
| 145 | else: |
| 146 | fob = ob |
| 147 | if isinstance(fob, (types.FunctionType, types.MethodType)): |
| 148 | argspec = inspect.formatargspec(*inspect.getfullargspec(fob)) |
| 149 | if (isinstance(ob, (type, types.MethodType)) or |
| 150 | isinstance(ob_call, types.MethodType)): |
| 151 | argspec = _first_param.sub("", argspec) |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 152 | |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 153 | lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT) |
| 154 | if len(argspec) > _MAX_COLS else [argspec] if argspec else []) |
| 155 | |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 156 | if isinstance(ob_call, types.MethodType): |
| 157 | doc = ob_call.__doc__ |
| 158 | else: |
| 159 | doc = getattr(ob, "__doc__", "") |
| 160 | if doc: |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 161 | for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]: |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 162 | line = line.strip() |
| 163 | if not line: |
| 164 | break |
| 165 | if len(line) > _MAX_COLS: |
| 166 | line = line[: _MAX_COLS - 3] + '...' |
| 167 | lines.append(line) |
| 168 | argspec = '\n'.join(lines) |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 169 | if not argspec: |
| 170 | argspec = _default_callable_argspec |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 171 | return argspec |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 172 | |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 173 | if __name__ == '__main__': |
Terry Jan Reedy | db4e5c5 | 2013-05-27 21:32:03 -0400 | [diff] [blame] | 174 | from unittest import main |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 175 | main('idlelib.idle_test.test_calltips', verbosity=2) |