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