Commits

Anonymous committed 39363e7

support for lambdas in codeassist and get_calltip. original patch by Darren Hoo

Comments (0)

Files changed (4)

rope/base/builtins.py

         else:
             return pyobjects.get_unknown()
 
-    def get_pattributes(self):
+    def get_module(self):
+        return self.parent.get_module()
+
+    def get_scope(self):
+        return self.scope
+
+    def get_kind(self):
+        return 'lambda'
+
+    def get_ast(self):
+        return self.node
+
+    def get_attributes(self):
         return {}
 
     def get_name(self):
         return  'lambda'
 
-    def get_param_names(self):
+    def get_param_names(self, special_args=True):
         result = [node.id for node in self.arguments.args
                   if isinstance(node, ast.Name)]
         if self.arguments.vararg:

rope/base/worder.py

     def get_function_and_args_in_header(self, offset):
         return self.code_finder.get_function_and_args_in_header(offset)
 
+    def get_lambda_and_args(self, offset):
+        return self.code_finder.get_lambda_and_args(offset)
+
     def find_function_offset(self, offset):
         return self.code_finder.find_function_offset(offset)
 
         end = self._find_word_end(offset) + 1
         return (start, end)
 
-    def get_word_parens_range(self, offset):
+    def get_word_parens_range(self, offset, opening='(', closing=')'):
         end = self._find_word_end(offset)
-        start_parens = self.code.index('(', end)
+        start_parens = self.code.index(opening, end)
         index = start_parens
         open_count = 0
         while index < len(self.code):
-            if self.code[index] == '(':
+            if self.code[index] == opening:
                 open_count += 1
-            if self.code[index] == ')':
+            if self.code[index] == closing:
                 open_count -= 1
             if open_count == 0:
                 return (start_parens, index + 1)
         lparens, rparens = self.get_word_parens_range(offset)
         return self.raw[offset:rparens + 1]
 
-    def find_function_offset(self, offset):
+    def find_function_offset(self, offset, definition='def '):
         while True:
-            offset = self.code.index('def ', offset)
+            offset = self.code.index(definition, offset)
             if offset == 0 or not self._is_id_char(offset - 1):
                 break
             offset += 1
         def_ = offset + 4
         return self._find_first_non_space_char(def_)
+
+    def get_lambda_and_args(self, offset):
+        offset = self.find_function_offset(offset, definition = 'lambda ')
+        lparens, rparens = self.get_word_parens_range(offset, opening=' ', closing=':')
+        return self.raw[offset:rparens + 1]
+

rope/refactor/functionutils.py

 import rope.base.exceptions
 import rope.base.pyobjects
+from rope.base.builtins import Lambda
 from rope.base import worder
 
 
         scope = pyfunction.get_scope()
         parent = scope.parent
         parameter_names = pyfunction.get_param_names()
-        is_method = pyfunction.get_kind() == 'method'
-        info = _FunctionParser(code, is_method)
+        kind = pyfunction.get_kind()
+        is_method = kind == 'method'
+        is_lambda = kind == 'lambda'
+        info = _FunctionParser(code, is_method, is_lambda)
         args, keywords = info.get_parameters()
         args_arg = None
         keywords_arg = None
         word_finder = worder.Worder(pymodule.source_code)
         lineno = pyfunction.get_ast().lineno
         start = pymodule.lines.get_line_start(lineno)
-        call = word_finder.get_function_and_args_in_header(start)
+        if isinstance(pyfunction, Lambda):
+            call = word_finder.get_lambda_and_args(start)
+        else:
+            call = word_finder.get_function_and_args_in_header(start)
         return DefinitionInfo._read(pyfunction, call)
 
 
 
 class _FunctionParser(object):
 
-    def __init__(self, call, implicit_arg):
+    def __init__(self, call, implicit_arg, is_lambda=False):
         self.call = call
         self.implicit_arg = implicit_arg
         self.word_finder = worder.Worder(self.call)
-        self.last_parens = self.call.rindex(')')
+        if is_lambda:
+            self.last_parens = self.call.rindex(':')
+        else:
+            self.last_parens = self.call.rindex(')')
         self.first_parens = self.word_finder._find_parens_start(self.last_parens)
 
     def get_parameters(self):

ropetest/contrib/codeassisttest.py

         doc = get_calltip(self.project, src, src.rindex('f'), remove_self=True)
         self.assertEquals('C.f(p1)', 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)')
+
     # TESTING PROPOSAL'S KINDS AND TYPES.
     # SEE RELATION MATRIX IN `CompletionProposal`'s DOCSTRING
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.