wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 1 | """Pop up a reminder of how to call a function. |
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. |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 6 | """ |
Terry Jan Reedy | 0fe4513 | 2019-03-24 17:12:28 -0400 | [diff] [blame] | 7 | import __main__ |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 8 | import inspect |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9 | import re |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 10 | import sys |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 11 | import textwrap |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 12 | import types |
| 13 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 14 | from idlelib import calltip_w |
| 15 | from idlelib.hyperparser import HyperParser |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 16 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | |
Terry Jan Reedy | 06e2029 | 2018-06-19 23:00:35 -0400 | [diff] [blame] | 18 | class Calltip: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 19 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 20 | def __init__(self, editwin=None): |
Raymond Hettinger | c5e378d | 2004-05-04 08:34:56 +0000 | [diff] [blame] | 21 | if editwin is None: # subprocess and test |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 22 | self.editwin = None |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 23 | else: |
| 24 | self.editwin = editwin |
| 25 | self.text = editwin.text |
| 26 | self.active_calltip = None |
| 27 | self._calltip_window = self._make_tk_calltip_window |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | |
| 29 | def close(self): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 30 | self._calltip_window = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | def _make_tk_calltip_window(self): |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 33 | # See __init__ for usage |
Terry Jan Reedy | 9af1836 | 2018-06-20 02:18:49 -0400 | [diff] [blame] | 34 | return calltip_w.CalltipWindow(self.text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 35 | |
Zackery Spytz | bfdeaa3 | 2020-01-30 18:55:42 -0700 | [diff] [blame] | 36 | def remove_calltip_window(self, event=None): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 37 | if self.active_calltip: |
| 38 | self.active_calltip.hidetip() |
| 39 | self.active_calltip = None |
Kurt B. Kaiser | ae67647 | 2001-07-12 23:10:35 +0000 | [diff] [blame] | 40 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 41 | def force_open_calltip_event(self, event): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 42 | "The user selected the menu entry or hotkey, open the tip." |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 43 | self.open_calltip(True) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 44 | return "break" |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 45 | |
| 46 | def try_open_calltip_event(self, event): |
Terry Jan Reedy | 9af1836 | 2018-06-20 02:18:49 -0400 | [diff] [blame] | 47 | """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] | 48 | necessary, for example after an opening bracket, so function calls |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 49 | won't be made. |
| 50 | """ |
| 51 | self.open_calltip(False) |
| 52 | |
| 53 | def refresh_calltip_event(self, event): |
Tal Einat | 87e59ac | 2018-08-05 09:21:08 +0300 | [diff] [blame] | 54 | if self.active_calltip and self.active_calltip.tipwindow: |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 55 | self.open_calltip(False) |
| 56 | |
| 57 | def open_calltip(self, evalfuncs): |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 58 | """Maybe close an existing calltip and maybe open a new calltip. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 59 | |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 60 | Called from (force_open|try_open|refresh)_calltip_event functions. |
| 61 | """ |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 62 | hp = HyperParser(self.editwin, "insert") |
| 63 | sur_paren = hp.get_surrounding_brackets('(') |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 64 | |
| 65 | # If not inside parentheses, no calltip. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 66 | if not sur_paren: |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 67 | self.remove_calltip_window() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 68 | return |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 69 | |
| 70 | # If a calltip is shown for the current parentheses, do |
| 71 | # nothing. |
| 72 | if self.active_calltip: |
| 73 | opener_line, opener_col = map(int, sur_paren[0].split('.')) |
| 74 | if ( |
| 75 | (opener_line, opener_col) == |
| 76 | (self.active_calltip.parenline, self.active_calltip.parencol) |
| 77 | ): |
| 78 | return |
| 79 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 80 | hp.set_index(sur_paren[0]) |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 81 | try: |
| 82 | expression = hp.get_expression() |
| 83 | except ValueError: |
| 84 | expression = None |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 85 | if not expression: |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 86 | # No expression before the opening parenthesis, e.g. |
| 87 | # because it's in a string or the opener for a tuple: |
| 88 | # Do nothing. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 89 | return |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 90 | |
| 91 | # At this point, the current index is after an opening |
| 92 | # parenthesis, in a section of code, preceded by a valid |
| 93 | # expression. If there is a calltip shown, it's not for the |
| 94 | # same index and should be closed. |
| 95 | self.remove_calltip_window() |
| 96 | |
| 97 | # Simple, fast heuristic: If the preceding expression includes |
| 98 | # an opening parenthesis, it likely includes a function call. |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 99 | if not evalfuncs and (expression.find('(') != -1): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 100 | return |
Tal Einat | da7bb7b | 2020-11-02 05:59:52 +0200 | [diff] [blame] | 101 | |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 102 | argspec = self.fetch_tip(expression) |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 103 | if not argspec: |
| 104 | return |
| 105 | self.active_calltip = self._calltip_window() |
| 106 | self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 107 | |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 108 | def fetch_tip(self, expression): |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 109 | """Return the argument list and docstring of a function or class. |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 110 | |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 111 | If there is a Python subprocess, get the calltip there. Otherwise, |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 112 | either this fetch_tip() is running in the subprocess or it was |
| 113 | called in an IDLE running without the subprocess. |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 114 | |
| 115 | The subprocess environment is that of the most recently run script. If |
| 116 | two unrelated modules are being edited some calltips in the current |
| 117 | module may be inoperative if the module was not the last to run. |
| 118 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 119 | To find methods, fetch_tip must be fed a fully qualified name. |
| 120 | |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 121 | """ |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 122 | try: |
| 123 | rpcclt = self.editwin.flist.pyshell.interp.rpcclt |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 124 | except AttributeError: |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame] | 125 | rpcclt = None |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 126 | if rpcclt: |
| 127 | return rpcclt.remotecall("exec", "get_the_calltip", |
Terry Jan Reedy | e606e23 | 2012-06-03 00:27:54 -0400 | [diff] [blame] | 128 | (expression,), {}) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 129 | else: |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 130 | return get_argspec(get_entity(expression)) |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 131 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 132 | |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 133 | def get_entity(expression): |
| 134 | """Return the object corresponding to expression evaluated |
Terry Jan Reedy | 0fe4513 | 2019-03-24 17:12:28 -0400 | [diff] [blame] | 135 | in a namespace spanning sys.modules and __main.dict__. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 136 | """ |
| 137 | if expression: |
Terry Jan Reedy | 0fe4513 | 2019-03-24 17:12:28 -0400 | [diff] [blame] | 138 | namespace = {**sys.modules, **__main__.__dict__} |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 139 | try: |
Terry Jan Reedy | 2b75155 | 2019-03-23 03:50:15 -0400 | [diff] [blame] | 140 | return eval(expression, namespace) # Only protect user code. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 141 | except BaseException: |
| 142 | # An uncaught exception closes idle, and eval can raise any |
| 143 | # exception, especially if user classes are involved. |
| 144 | return None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 145 | |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 146 | # The following are used in get_argspec and some in tests |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 147 | _MAX_COLS = 85 |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 148 | _MAX_LINES = 5 # enough for bytes |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 149 | _INDENT = ' '*4 # for wrapped signatures |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 150 | _first_param = re.compile(r'(?<=\()\w*\,?\s*') |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 151 | _default_callable_argspec = "See source or doc" |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 152 | _invalid_method = "invalid method signature" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 153 | |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 154 | def get_argspec(ob): |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 155 | '''Return a string describing the signature of a callable object, or ''. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 156 | |
| 157 | For Python-coded functions and methods, the first line is introspected. |
| 158 | Delete 'self' parameter for classes (.__init__) and bound methods. |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 159 | The next lines are the first lines of the doc string up to the first |
| 160 | empty line or _MAX_LINES. For builtins, this typically includes |
| 161 | the arguments in addition to the return value. |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 162 | ''' |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 163 | # Determine function object fob to inspect. |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 164 | try: |
| 165 | ob_call = ob.__call__ |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 166 | except BaseException: # Buggy user object could raise anything. |
| 167 | return '' # No popup for non-callables. |
Terry Jan Reedy | 7ddbaa7 | 2020-11-20 01:59:11 -0500 | [diff] [blame] | 168 | # For Get_argspecTest.test_buggy_getattr_class, CallA() & CallB(). |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 169 | fob = ob_call if isinstance(ob_call, types.MethodType) else ob |
| 170 | |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 171 | # Initialize argspec and wrap it to get lines. |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 172 | try: |
| 173 | argspec = str(inspect.signature(fob)) |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 174 | except Exception as err: |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 175 | msg = str(err) |
| 176 | if msg.startswith(_invalid_method): |
| 177 | return _invalid_method |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 178 | else: |
| 179 | argspec = '' |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 180 | |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 181 | if isinstance(fob, type) and argspec == '()': |
Terry Jan Reedy | 949fe97 | 2019-06-04 21:55:37 -0400 | [diff] [blame] | 182 | # If fob has no argument, use default callable argspec. |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 183 | argspec = _default_callable_argspec |
Terry Jan Reedy | 2a2ce4f | 2012-06-07 19:41:04 -0400 | [diff] [blame] | 184 | |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 185 | lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT) |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 186 | if len(argspec) > _MAX_COLS else [argspec] if argspec else []) |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 187 | |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 188 | # Augment lines from docstring, if any, and join to get argspec. |
Terry Jan Reedy | 7ddbaa7 | 2020-11-20 01:59:11 -0500 | [diff] [blame] | 189 | doc = inspect.getdoc(ob) |
Terry Jan Reedy | 715476d | 2014-01-21 15:36:51 -0500 | [diff] [blame] | 190 | if doc: |
Terry Jan Reedy | a0f1e22 | 2014-01-26 19:55:34 -0500 | [diff] [blame] | 191 | for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]: |
Terry Jan Reedy | d5710f8 | 2014-01-21 20:45:17 -0500 | [diff] [blame] | 192 | line = line.strip() |
| 193 | if not line: |
| 194 | break |
| 195 | if len(line) > _MAX_COLS: |
| 196 | line = line[: _MAX_COLS - 3] + '...' |
| 197 | lines.append(line) |
Emmanuel Arias | ab54b9a | 2019-01-03 04:47:58 -0300 | [diff] [blame] | 198 | argspec = '\n'.join(lines) |
Tal Einat | 52013e5 | 2020-04-04 06:05:58 +0300 | [diff] [blame] | 199 | |
| 200 | return argspec or _default_callable_argspec |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 201 | |
Louie Lu | 3b0f620 | 2017-08-10 08:58:13 +0800 | [diff] [blame] | 202 | |
Kurt B. Kaiser | 152b3c2 | 2007-08-30 06:35:15 +0000 | [diff] [blame] | 203 | if __name__ == '__main__': |
Terry Jan Reedy | db4e5c5 | 2013-05-27 21:32:03 -0400 | [diff] [blame] | 204 | from unittest import main |
Terry Jan Reedy | 06e2029 | 2018-06-19 23:00:35 -0400 | [diff] [blame] | 205 | main('idlelib.idle_test.test_calltip', verbosity=2) |