Commits

Anonymous committed ab087f6

codeassist: using offset of the function in get_calltip()

  • Participants
  • Parent commits cf78207

Comments (0)

Files changed (2)

rope/contrib/codeassist.py

 
 
 def get_calltip(project, source_code, offset, resource=None, maxfixes=1):
-    finder = WordRangeFinder(source_code)
-    start = finder.find_parens_start_from_inside(max(0, offset - 1))
-    pyname = _find_pyname_at(project, source_code, start, resource, maxfixes)
+    """Get the calltip of a function
+
+    The format of the returned string is
+    ``module_name.holding_scope_names.function_name(arguments)``.  For
+    classes `__init__()` and for normal objects `__call__()` function
+    is used.
+
+    Note that the offset is on the function itself *not* after the its
+    open parenthesis.  (Actually it used to be the other way but it
+    was easily confused when string literals were involved.  So I
+    decided it is better for it not to try to be too clever when it
+    cannot be clever enough).  You can use a simple search like::
+
+        offset = source_code.rindex('(', 0, offset) - 1
+
+    to handle simple situations.
+    """
+    pyname = _find_pyname_at(project, source_code, offset, resource, maxfixes)
     if pyname is None:
         return None
     pyobject = pyname.get_object()
     scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
     result = rope.base.evaluate.get_pyname_in_scope(scope, expression)
     new_code = pymodule.source_code
-    if result is None or new_code.startswith(source_code[:offset]):
+    if result is None or new_code.startswith(source_code[:offset + 1]):
         if offset < len(new_code):
             return rope.base.evaluate.get_pyname_at(pymodule, offset)
     return result

ropetest/contrib/codeassisttest.py

 
     def test_simple_get_calltips(self):
         src = 'def f():\n    pass\nvar = f()\n'
-        doc = get_calltip(self.project, src, src.rindex(')'))
+        doc = get_calltip(self.project, src, src.rindex('f'))
         self.assertEquals('f()', doc)
 
     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))
+        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, len(src))
+        doc = get_calltip(self.project, src, src.rindex('c'))
         self.assertEquals('C.__call__(self, p)', doc)
 
     def test_get_calltips_and_including_module_name(self):
               'c = C()\nc(1,'
         mod = testutils.create_module(self.project, 'mod')
         mod.write(src)
-        doc = get_calltip(self.project, src, len(src), mod)
+        doc = get_calltip(self.project, src, src.rindex('c'), mod)
         self.assertEquals('mod.C.__call__(self, p)', doc)