def _handle_object_info_reply(self, rep): """ Reimplement call tips to only show signatures, using the same style from our Editor and External Console too Note: For IPython 2- """ self.log.debug("oinfo: %s", rep.get('content', '')) cursor = self._get_cursor() info = self._request_info.get('call_tip') if info and info.id == rep['parent_header']['msg_id'] and \ info.pos == cursor.position(): content = rep['content'] if content.get('ismagic', False): call_info, doc = None, None else: call_info, doc = call_tip(content, format_call=True) if call_info is None and doc is not None: name = content['name'].split('.')[-1] argspec = getargspecfromtext(doc) if argspec: # This covers cases like np.abs, whose docstring is # the same as np.absolute and because of that a proper # signature can't be obtained correctly call_info = name + argspec else: call_info = getsignaturefromtext(doc, name) if call_info: self._control.show_calltip(_("Arguments"), call_info, signature=True, color='#2D62FF')
def get_signature(self, content): """Get signature from inspect reply content""" data = content.get('data', {}) text = data.get('text/plain', '') if text: text = ANSI_OR_SPECIAL_PATTERN.sub('', text) self._control.current_prompt_pos = self._prompt_pos line = self._control.get_current_line_to_cursor() name = line[:-1].split('(')[-1] # Take last token after a ( name = name.split('.')[-1] # Then take last token after a . # Clean name from invalid chars try: name = self.clean_invalid_var_chars(name).split('_')[-1] except: pass argspec = getargspecfromtext(text) if argspec: # This covers cases like np.abs, whose docstring is # the same as np.absolute and because of that a proper # signature can't be obtained correctly signature = name + argspec else: signature = getsignaturefromtext(text, name) return signature else: return ''
def get_info(self, info): """ Find the calltip and docs Returns a dict like the following: {'note': 'Function of numpy.core.numeric...', 'argspec': "(shape, dtype=None, order='C')' 'docstring': 'Return an array of given...' 'name': 'ones', 'calltip': 'ones(shape, dtype=None, order='C')'} """ call_def = self.get_jedi_object('goto_definitions', info) for cd in call_def: if cd.doc and not cd.doc.rstrip().endswith(')'): call_def = cd break else: call_def = call_def[0] name = call_def.name if name is None: return if call_def.module_path: mod_name = get_parent_until(call_def.module_path) else: mod_name = None if not mod_name: mod_name = call_def.module_name if call_def.doc.startswith(name + '('): calltip = getsignaturefromtext(call_def.doc, name) argspec = calltip[calltip.find('('):] docstring = call_def.doc[call_def.doc.find(')') + 3:] elif '(' in call_def.doc.splitlines()[0]: calltip = call_def.doc.splitlines()[0] name = call_def.doc.split('(')[0] docstring = call_def.doc[call_def.doc.find(')') + 3:] argspec = calltip[calltip.find('('):] else: calltip = name + '(...)' argspec = '()' docstring = call_def.doc if call_def.type == 'module': note = 'Module %s' % mod_name argspec = '' calltip = name elif call_def.type == 'class': note = 'Class in %s module' % mod_name elif call_def.doc.startswith('%s(self' % name): class_name = call_def.full_name.split('.')[-2] note = 'Method of %s class in %s module' % ( class_name.capitalize(), mod_name) else: note = '%s in %s module' % (call_def.type.capitalize(), mod_name) argspec = argspec.replace(' = ', '=') calltip = calltip.replace(' = ', '=') debug_print(call_def.name) doc_info = dict(name=name, argspec=argspec, note=note, docstring=docstring, calltip=calltip) return doc_info
def show_object_info(self, text, call=False, force=False): """Show signature calltip and/or docstring in the Help plugin""" text = to_text_string(text) # Show docstring help_enabled = self.help_enabled or force if force and self.help is not None: self.help.dockwidget.setVisible(True) self.help.dockwidget.raise_() if help_enabled and (self.help is not None) and \ (self.help.dockwidget.isVisible()): # Help widget exists and is visible if hasattr(self, 'get_doc'): self.help.set_shell(self) else: self.help.set_shell(self.parent()) self.help.set_object_text(text, ignore_unknown=False) self.setFocus() # if help was not at top level, raising it to # top will automatically give it focus because of # the visibility_changed signal, so we must give # focus back to shell # Show calltip if call and self.calltips: # Display argument list if this is a function call iscallable = self.iscallable(text) if iscallable is not None: if iscallable: arglist = self.get_arglist(text) name = text.split('.')[-1] argspec = signature = '' if isinstance(arglist, bool): arglist = [] if arglist: argspec = '(' + ''.join(arglist) + ')' else: doc = self.get__doc__(text) if doc is not None: # This covers cases like np.abs, whose docstring is # the same as np.absolute and because of that a # proper signature can't be obtained correctly argspec = getargspecfromtext(doc) if not argspec: signature = getsignaturefromtext(doc, name) if argspec or signature: if argspec: tiptext = name + argspec else: tiptext = signature self.show_calltip(_("Arguments"), tiptext, signature=True, color='#2D62FF')
def handle_info(self, cts, doc_text, source_code, offset): obj_fullname = '' calltip = '' argspec = '' note = '' if cts: cts = cts.replace('.__init__', '') parpos = cts.find('(') if parpos: obj_fullname = cts[:parpos] obj_name = obj_fullname.split('.')[-1] cts = cts.replace(obj_fullname, obj_name) calltip = cts if ('()' in cts) or ('(...)' in cts): # Either inspected object has no argument, or it's # a builtin or an extension -- in this last case # the following attempt may succeed: calltip = getsignaturefromtext(doc_text, obj_name) if not obj_fullname: obj_fullname = sourcecode.get_primary_at(source_code, offset) if obj_fullname and not obj_fullname.startswith('self.'): # doc_text was generated by utils.dochelpers.getdoc if type(doc_text) is dict: obj_fullname = doc_text['name'] or obj_fullname argspec = doc_text['argspec'] note = doc_text['note'] doc_text = doc_text['docstring'] elif calltip: argspec_st = calltip.find('(') argspec = calltip[argspec_st:] module_end = obj_fullname.rfind('.') module = obj_fullname[:module_end] note = 'Present in %s module' % module if not doc_text and not calltip: return return dict(name=obj_fullname, argspec=argspec, note=note, docstring=doc_text, calltip=calltip)
def handle_info(self, cts, doc_text, source_code, offset): obj_fullname = '' calltip = '' argspec = '' note = '' if cts: cts = cts.replace('.__init__', '') parpos = cts.find('(') if parpos: obj_fullname = cts[:parpos] obj_name = obj_fullname.split('.')[-1] cts = cts.replace(obj_fullname, obj_name) calltip = cts if ('()' in cts) or ('(...)' in cts): # Either inspected object has no argument, or it's # a builtin or an extension -- in this last case # the following attempt may succeed: calltip = getsignaturefromtext(doc_text, obj_name) if not obj_fullname: obj_fullname = sourcecode.get_primary_at(source_code, offset) if obj_fullname and not obj_fullname.startswith('self.'): # doc_text was generated by utils.dochelpers.getdoc if type(doc_text) is dict: obj_fullname = doc_text['name'] or obj_fullname argspec = doc_text['argspec'] note = doc_text['note'] doc_text = doc_text['docstring'] elif calltip: argspec_st = calltip.find('(') argspec = calltip[argspec_st:] module_end = obj_fullname.rfind('.') module = obj_fullname[:module_end] note = 'Present in %s module' % module if not doc_text and not calltip: return default_info_response() return dict(name=obj_fullname, argspec=argspec, note=note, docstring=doc_text, calltip=calltip)
def get_info(self, info): """ Find the calltip and docs Returns a dict like the following: {'note': 'Function of numpy.core.numeric...', 'argspec': "(shape, dtype=None, order='C')' 'docstring': 'Return an array of given...' 'name': 'ones', 'calltip': 'ones(shape, dtype=None, order='C')'} """ call_def = self.get_jedi_object('goto_definitions', info) for cd in call_def: # For compatibility with Jedi 0.11 try: cd.doc = cd.docstring() except AttributeError: pass if cd.doc and not cd.doc.rstrip().endswith(')'): call_def = cd break else: call_def = call_def[0] name = call_def.name if name is None: return if call_def.module_path: mod_name = get_parent_until(call_def.module_path) else: mod_name = None if not mod_name: mod_name = call_def.module_name if call_def.doc.startswith(name + '('): calltip = getsignaturefromtext(call_def.doc, name) argspec = calltip[calltip.find('('):] docstring = call_def.doc[call_def.doc.find(')') + 3:] elif '(' in call_def.doc.splitlines()[0]: calltip = call_def.doc.splitlines()[0] name = call_def.doc.split('(')[0] docstring = call_def.doc[call_def.doc.find(')') + 3:] argspec = calltip[calltip.find('('):] else: calltip = name + '(...)' argspec = '()' docstring = call_def.doc if call_def.type == 'module': note = 'Module %s' % mod_name argspec = '' calltip = name elif call_def.type == 'class': note = 'Class in %s module' % mod_name elif call_def.doc.startswith('%s(self' % name): class_name = call_def.full_name.split('.')[-2] note = 'Method of %s class in %s module' % ( class_name.capitalize(), mod_name) else: note = '%s in %s module' % (call_def.type.capitalize(), mod_name) argspec = argspec.replace(' = ', '=') calltip = calltip.replace(' = ', '=') debug_print(call_def.name) doc_info = dict(name=name, argspec=argspec, note=note, docstring=docstring, calltip=calltip) return doc_info