def test_removing_self_parameter_and_more_than_one_parameter(self): src = 'class C(object):\n' \ ' def f(self, p1):\n'\ ' pass\n' \ 'C().f()' doc = get_calltip(self.project, src, src.rindex('f'), remove_self=True) self.assertEquals('C.f(p1)', doc)
def my_get_calltip(self, prefix): self._check_project() maxfixes = self.env.get('codeassist_maxfixes') text = self._get_text() offset = self.env.get_offset() docs = codeassist.get_calltip(self.project, text, offset, self.resource, maxfixes, remove_self = True) return docs
def test_get_calltips_and_including_module_name(self): src = 'class C(object):\n' \ ' def __call__(self, p):\n pass\n' \ 'c = C()\nc(1,' mod = testutils.create_module(self.project, 'mod') mod.write(src) doc = get_calltip(self.project, src, src.rindex('c'), mod) self.assertEquals('mod.C.__call__(self, p)', doc)
def getCallTip(self, offset=None): if offset is None: offset = self.getOffset() project = self.getProject() try: calltip = codeassist.get_calltip(project, self.editorTabWidget.getSource(), offset) return calltip except Exception as err: return None
def CompletionRequest(self, request, response): """ Finds completion proposals for the given location in the given source file. """ # Get information out of the request project, resource, source, offset = self._Context(request.context) # If the cursor is immediately after a comma or open paren, we should look # for a calltip first. word_finder = worder.Worder(source) non_space_offset = word_finder.code_finder._find_last_non_space_char(offset) if word_finder.code_finder.code[non_space_offset] in "(,": paren_start = word_finder.find_parens_start_from_inside(offset) # Get a calltip now calltip = codeassist.get_calltip(project, source, paren_start-1, maxfixes=self.MAXFIXES, resource=resource, remove_self=True) if calltip is not None: response.insertion_position = paren_start + 1 response.calltip = calltip return # Do normal completion if a calltip couldn't be found proposals = codeassist.code_assist(project, source, offset, maxfixes=self.MAXFIXES, resource=resource) proposals = codeassist.sorted_proposals(proposals) # Get the position that this completion will start from. starting_offset = codeassist.starting_offset(source, offset) response.insertion_position = starting_offset # Construct the response protobuf for proposal in proposals: proposal_pb = response.proposal.add() proposal_pb.name = proposal.name docstring = proposal.get_doc() if proposal.type in self.PROPOSAL_TYPES: proposal_pb.type = self.PROPOSAL_TYPES[proposal.type] if proposal.scope in self.PROPOSAL_SCOPES: proposal_pb.scope = self.PROPOSAL_SCOPES[proposal.scope] if docstring is not None: proposal_pb.docstring = docstring
def getCalltipAndDoc( fileName, editor, position = None, tryQt = False ): " Provides a calltip and docstring " try: GlobalData().validateRopeProject() ropeProject = GlobalData().getRopeProject( fileName ) if position is None: position = editor.currentPosition() text = editor.text() calltip = None docstring = None resource = None if os.path.isabs( fileName ): resource = path_to_resource( ropeProject, fileName ) calltip = get_calltip( ropeProject, text, position, resource, ignore_unknown = False, remove_self = True, maxfixes = 7 ) if calltip is not None: calltip = calltip.strip() while '..' in calltip: calltip = calltip.replace( '..', '.' ) if '(.)' in calltip: calltip = calltip.replace( '(.)', '(...)' ) calltip = calltip.replace( '.__init__', '' ) try: docstring = get_doc( ropeProject, text, position, resource, maxfixes = 7 ) except: pass if not calltip: calltip = None if tryQt and calltip is not None and docstring is not None: # try to extract signatures from the QT docstring try: if calltip.startswith( 'QtCore.' ) or calltip.startswith( 'QtGui.' ): parenPos = calltip.index( "(" ) dotPos = calltip.rindex( ".", 0, parenPos ) pattern = calltip[ dotPos : parenPos + 1 ] signatures = [] for line in docstring.splitlines(): line = line.strip() if pattern in line and not line.endswith( ':' ): signatures.append( line ) if signatures: calltip = '\n'.join( signatures ) except: pass if calltip: # Sometimes rope makes a mistake and provides a calltip for the # wrong function. Check the name here. line, index = editor.lineIndexFromPosition( position ) word = str( editor.getWord( line, index ) ) if word and not (word.startswith( '__' ) and word.endswith( '__' )): fullName = calltip.split( '(', 1 )[ 0 ].strip() lastPart = fullName.split( '.' )[ -1 ] if lastPart != word: # Wrong calltip # print "Wrong calltip. Asked: '" + word + "' received: '" + lastPart + "'" # print calltip return None, None return calltip, docstring except: return None, None
def test_get_calltips_for_classes(self): src = 'class C(object):\n' \ ' def __init__(self):\n pass\nC(' doc = get_calltip(self.project, src, len(src) - 1) self.assertEquals('C.__init__(self)', doc)
def test_get_calltips_for_objects_with_call(self): src = 'class C(object):\n' \ ' def __call__(self, p):\n pass\n' \ 'c = C()\nc(1,' doc = get_calltip(self.project, src, src.rindex('c')) self.assertEquals('C.__call__(self, p)', doc)
def test_lambda_calltip(self): src = 'foo = lambda x, y=1: None\n' \ 'foo()' doc = get_calltip(self.project, src, src.rindex('f')) self.assertEqual(doc, 'lambda(x, y)')
def test_simple_get_calltips(self): src = 'def f():\n pass\nvar = f()\n' doc = get_calltip(self.project, src, src.rindex('f')) self.assertEquals('f()', doc)
def _get_doc(project, text, offset, *args, **kwds): try: offset = text.rindex('(', 0, offset) - 1 except ValueError: return None return codeassist.get_calltip(project, text, offset, *args, **kwds)
def test_get_calltips_and_including_module_name(self): src = 'range()\n' doc = get_calltip(self.project, src, 1, ignore_unknown=True) self.assertTrue(doc is None)
def getCalltipAndDoc(fileName, editor, position=None, tryQt=False): " Provides a calltip and docstring " try: GlobalData().validateRopeProject() ropeProject = GlobalData().getRopeProject(fileName) if position is None: position = editor.currentPosition() text = editor.text() calltip = None docstring = None resource = None if os.path.isabs(fileName): resource = path_to_resource(ropeProject, fileName) calltip = get_calltip(ropeProject, text, position, resource, ignore_unknown=False, remove_self=True, maxfixes=7) if calltip is not None: calltip = calltip.strip() while '..' in calltip: calltip = calltip.replace('..', '.') if '(.)' in calltip: calltip = calltip.replace('(.)', '(...)') calltip = calltip.replace('.__init__', '') try: docstring = get_doc(ropeProject, text, position, resource, maxfixes=7) except: pass if not calltip: calltip = None if tryQt and calltip is not None and docstring is not None: # try to extract signatures from the QT docstring try: if calltip.startswith('QtCore.') or calltip.startswith( 'QtGui.'): parenPos = calltip.index("(") dotPos = calltip.rindex(".", 0, parenPos) pattern = calltip[dotPos:parenPos + 1] signatures = [] for line in docstring.splitlines(): line = line.strip() if pattern in line and not line.endswith(':'): signatures.append(line) if signatures: calltip = '\n'.join(signatures) except: pass if calltip: # Sometimes rope makes a mistake and provides a calltip for the # wrong function. Check the name here. line, index = editor.lineIndexFromPosition(position) word = str(editor.getWord(line, index)) if word and not (word.startswith('__') and word.endswith('__')): fullName = calltip.split('(', 1)[0].strip() lastPart = fullName.split('.')[-1] if lastPart != word: # Wrong calltip # print "Wrong calltip. Asked: '" + word + "' received: '" + lastPart + "'" # print calltip return None, None return calltip, docstring except: return None, None