Anonymous avatar Anonymous committed 17384d1

Using LogicalLinesFinder in Scope.get_end()

Comments (0)

Files changed (6)

rope/base/codeanalyze.py

             for start, end in self._logical_lines(readline):
                 if line_number <= end:
                     return (start, end)
-                last_end = end
-        except tokenize.TokenError:
-            return (last_end, None)
+                last_end = end + 1
+        except tokenize.TokenError, e:
+            current = e.args[1][0]
+            return (last_end, current)
         return (last_end, None)
 
     def _logical_lines(self, readline):

rope/base/pyscopes.py

             return body[0].lineno
         return self.get_start()
 
+    _end = None
+
     def get_end(self):
         global_scope = self._get_global_scope()
         return global_scope._get_scope_finder().find_scope_end(self)
 
+    def xget_end(self):
+        if self._end is None:
+            global_scope = self._get_global_scope()
+            self._end = global_scope._get_scope_finder().find_scope_end(self)
+        return self._end
+
     def get_kind(self):
         pass
 
         return self.get_indents(scope.get_start())
 
     def get_holding_scope(self, module_scope, lineno, line_indents=None):
-        line_indents = line_indents
         if line_indents is None:
             line_indents = self.get_indents(lineno)
         current_scope = module_scope
             new_scope = None
             for scope in current_scope.get_scopes():
                 if scope.get_start() <= lineno:
-                    if self.find_scope_end(scope) >= lineno:
+                    if lineno <= scope.get_end():
                         new_scope = scope
+                        break
                 else:
                     break
         return current_scope
         if not scope.parent:
             return self.lines.length()
         end = scope.pyobject.get_ast().body[-1].lineno
-        # IDEA: Can we use LogicalLineFinder here?
+        finder = rope.base.codeanalyze.LogicalLineFinder(self.lines)
         body_indents = self._get_body_indents(scope)
-        for l in range(end + 1, self.lines.length() + 1):
+        for l in finder.generate_starts(min(end + 1, self.lines.length()),
+                                        self.lines.length() + 1):
             if not self._is_empty_line(l):
                 if self.get_indents(l) < body_indents:
                     return end

rope/refactor/inline.py

             if hasattr(decorators[0], 'lineno'):
                 start_line = decorators[0].lineno
         start_offset = lines.get_line_start(start_line)
-        end_offset = min(lines.get_line_end(scope.get_end()) + 1,
+        end_line = codeanalyze.LogicalLineFinder(lines).\
+                   get_logical_line_in(scope.get_end())[1]
+        end_offset = min(lines.get_line_end(end_line) + 1,
                          len(self.pymodule.source_code))
         return (start_offset, end_offset)
 
         lines = self.pymodule.lines
         start_line = scope.get_start()
         start, end = self._get_scope_range()
-        end_line = scope.get_end()
+        end_line = codeanalyze.LogicalLineFinder(lines).\
+                   get_logical_line_in(scope.get_end())[1]
         for i in range(end_line + 1, lines.length()):
             if lines.get_line(i).strip() == '':
                 end_line = i

rope/refactor/sourceutils.py

     elif len(node.body) > 1:
         start_line = node.body[1].lineno
 
+    end_line = codeanalyze.LogicalLineFinder(lines).\
+               get_logical_line_in(scope.get_end())[1]
     start = lines.get_line_start(start_line)
-    end = min(lines.get_line_end(scope.get_end()) + 1,
-              len(pymodule.source_code))
+    end = min(lines.get_line_end(end_line) + 1, len(pymodule.source_code))
     return start, end
 
 

ropetest/pyscopestest.py

         self.assertEquals(get_base_type('Function'), sample_func.get_type())
 
     def test_simple_function_scope(self):
-        scope = self.pycore.get_string_scope('def sample_func():\n    a = 10\n')
+        scope = self.pycore.get_string_scope(
+            'def sample_func():\n    a = 10\n')
         self.assertEquals(1, len(scope.get_scopes()))
         sample_func_scope = scope.get_scopes()[0]
         self.assertEquals(1, len(sample_func_scope.get_names()))
         self.assertEquals(0, len(sample_func_scope.get_scopes()))
 
     def test_classes_inside_function_scopes(self):
-        scope = self.pycore.get_string_scope('def sample_func():\n' +
-                                             '    class SampleClass(object):\n        pass\n')
+        scope = self.pycore.get_string_scope(
+            'def sample_func():\n'
+            '    class SampleClass(object):\n        pass\n')
         self.assertEquals(1, len(scope.get_scopes()))
         sample_func_scope = scope.get_scopes()[0]
         self.assertEquals(get_base_type('Type'),
                           get_name('SampleClass').get_object().get_type())
 
     def test_simple_class_scope(self):
-        scope = self.pycore.get_string_scope('class SampleClass(object):\n' +
-                                             '    def f(self):\n        var = 10\n')
+        scope = self.pycore.get_string_scope(
+            'class SampleClass(object):\n'
+            '    def f(self):\n        var = 10\n')
         self.assertEquals(1, len(scope.get_scopes()))
         sample_class_scope = scope.get_scopes()[0]
         self.assertEquals(1, len(sample_class_scope.get_names()))
         self.assertTrue('var' in f_in_class.get_names())
 
     def test_get_lineno(self):
-        scope = self.pycore.get_string_scope('\ndef sample_func():\n    a = 10\n')
+        scope = self.pycore.get_string_scope(
+            '\ndef sample_func():\n    a = 10\n')
         self.assertEquals(1, len(scope.get_scopes()))
         sample_func_scope = scope.get_scopes()[0]
         self.assertEquals(1, scope.get_start())
         self.assertEquals(2, sample_func_scope.get_start())
 
     def test_scope_kind(self):
-        scope = self.pycore.get_string_scope('class SampleClass(object):\n    pass\n' +
-                                             'def sample_func():\n    pass\n')
+        scope = self.pycore.get_string_scope(
+            'class SampleClass(object):\n    pass\n'
+            'def sample_func():\n    pass\n')
         sample_class_scope = scope.get_scopes()[0]
         sample_func_scope = scope.get_scopes()[1]
         self.assertEquals('Module', scope.get_kind())
         self.assertEquals('Function', sample_func_scope.get_kind())
 
     def test_function_parameters_in_scope_names(self):
-        scope = self.pycore.get_string_scope('def sample_func(param):\n    a = 10\n')
+        scope = self.pycore.get_string_scope(
+            'def sample_func(param):\n    a = 10\n')
         sample_func_scope = scope.get_scopes()[0]
         self.assertTrue('param' in sample_func_scope.get_names())
 
     def test_get_names_contains_only_names_defined_in_a_scope(self):
-        scope = self.pycore.get_string_scope('var1 = 10\ndef sample_func(param):\n    var2 = 20\n')
+        scope = self.pycore.get_string_scope(
+            'var1 = 10\ndef sample_func(param):\n    var2 = 20\n')
         sample_func_scope = scope.get_scopes()[0]
         self.assertTrue('var1' not in sample_func_scope.get_names())
 
     def test_scope_lookup(self):
-        scope = self.pycore.get_string_scope('var1 = 10\ndef sample_func(param):\n    var2 = 20\n')
+        scope = self.pycore.get_string_scope(
+            'var1 = 10\ndef sample_func(param):\n    var2 = 20\n')
         self.assertTrue(scope.lookup('var2') is None)
         self.assertEquals(get_base_type('Function'),
                           scope.lookup('sample_func').get_object().get_type())
         self.assertTrue('var' in func_scope.get_names())
 
     def test_function_scopes_classes(self):
-        scope = self.pycore.get_string_scope('def func():\n    class Sample(object):\n        pass\n')
+        scope = self.pycore.get_string_scope(
+            'def func():\n    class Sample(object):\n        pass\n')
         func_scope = scope.get_scopes()[0]
         self.assertTrue('Sample' in func_scope.get_names())
 
         self.assertTrue('var' in func_scope.get_names())
 
     def test_scopes_in_function_scopes(self):
-        scope = self.pycore.get_string_scope('def func():\n    def inner():\n        var = 10\n')
+        scope = self.pycore.get_string_scope(
+            'def func():\n    def inner():\n        var = 10\n')
         func_scope = scope.get_scopes()[0]
         inner_scope = func_scope.get_scopes()[0]
         self.assertTrue('var' in inner_scope.get_names())
 
     def test_for_variables_in_scopes(self):
-        scope = self.pycore.get_string_scope('for a_var in range(10):\n    pass\n')
+        scope = self.pycore.get_string_scope(
+            'for a_var in range(10):\n    pass\n')
         self.assertTrue('a_var' in scope.get_names())
 
     def test_assists_inside_fors(self):
-        scope = self.pycore.get_string_scope('for i in range(10):\n    a_var = i\n')
+        scope = self.pycore.get_string_scope(
+            'for i in range(10):\n    a_var = i\n')
         self.assertTrue('a_var' in scope.get_names())
 
     def test_first_parameter_of_a_method(self):
-        code = 'class AClass(object):\n    def a_func(self, param):\n        pass\n'
-        a_class = self.pycore.get_string_module(code).get_attribute('AClass'). get_object()
-        function_scope = a_class.get_attribute('a_func').get_object().get_scope()
-        self.assertEquals(a_class, function_scope.get_name('self').get_object().get_type())
-        self.assertNotEquals(a_class, function_scope.get_name('param').get_object().get_type())
+        code = 'class AClass(object):\n' \
+               '    def a_func(self, param):\n        pass\n'
+        a_class = self.pycore.get_string_module(code).\
+                  get_attribute('AClass'). get_object()
+        function_scope = a_class.get_attribute('a_func').\
+                         get_object().get_scope()
+        self.assertEquals(a_class, function_scope.
+                          get_name('self').get_object().get_type())
+        self.assertNotEquals(a_class, function_scope.get_name('param').
+                             get_object().get_type())
 
     def test_first_parameter_of_static_methods(self):
-        code = 'class AClass(object):\n    @staticmethod\n    def a_func(param):\n        pass\n'
-        a_class = self.pycore.get_string_module(code).get_attribute('AClass'). get_object()
-        function_scope = a_class.get_attribute('a_func').get_object().get_scope()
-        self.assertNotEquals(a_class, function_scope.get_name('param').get_object().get_type())
+        code = 'class AClass(object):\n' \
+               '    @staticmethod\n    def a_func(param):\n        pass\n'
+        a_class = self.pycore.get_string_module(code).\
+                  get_attribute('AClass'). get_object()
+        function_scope = a_class.get_attribute('a_func').\
+                         get_object().get_scope()
+        self.assertNotEquals(a_class, function_scope.
+                             get_name('param').get_object().get_type())
 
     def test_first_parameter_of_class_methods(self):
-        code = 'class AClass(object):\n    @classmethod\n    def a_func(cls):\n        pass\n'
-        a_class = self.pycore.get_string_module(code).get_attribute('AClass'). get_object()
-        function_scope = a_class.get_attribute('a_func').get_object().get_scope()
+        code = 'class AClass(object):\n' \
+            '    @classmethod\n    def a_func(cls):\n        pass\n'
+        a_class = self.pycore.get_string_module(code).\
+                  get_attribute('AClass'). get_object()
+        function_scope = a_class.get_attribute('a_func').\
+                         get_object().get_scope()
         self.assertEquals(a_class, function_scope.get_name('cls').get_object())
 
     def test_first_parameter_with_self_as_name_and_unknown_decorator(self):
         code = 'def my_decorator(func):\n    return func\n'\
-               'class AClass(object):\n    @my_decorator\n    def a_func(self):\n        pass\n'
-        a_class = self.pycore.get_string_module(code).get_attribute('AClass'). get_object()
-        function_scope = a_class.get_attribute('a_func').get_object().get_scope()
-        self.assertEquals(a_class, function_scope.get_name('self').get_object().get_type())
+               'class AClass(object):\n' \
+               '    @my_decorator\n    def a_func(self):\n        pass\n'
+        a_class = self.pycore.get_string_module(code).\
+                  get_attribute('AClass'). get_object()
+        function_scope = a_class.get_attribute('a_func').\
+                         get_object().get_scope()
+        self.assertEquals(a_class, function_scope.get_name('self').
+                          get_object().get_type())
 
     def test_inside_class_scope_attribute_lookup(self):
         scope = self.pycore.get_string_scope(
         range = mod.get_scope().lookup('range')
         self.assertEquals((mod, 1), range.get_definition_location())
 
+    def test_get_inner_scope_and_logical_lines(self):
+        scope = self.pycore.get_string_scope(
+            'class C(object):\n'
+            '    def f():\n        s = """\n1\n2\n"""\n        a = 1\n')
+        c_scope = scope.get_scopes()[0]
+        f_in_c = c_scope.get_scopes()[0]
+        self.assertEquals(f_in_c, scope.get_inner_scope_for_line(7))
+
 
 def suite():
     result = unittest.TestSuite()

ropetest/refactor/inlinetest.py

                        "a = A()\nname = a.get_name()\n")
         self._inline2(self.mod, self.mod.read().rindex('get_name') + 1)
         self.assertEquals("class A(object):\n    name = 'hey'\n"
-                          "a = A()\nname = a.name\n",
-                          self.mod.read())
+                          "a = A()\nname = a.name\n", self.mod.read())
 
     def test_simple_returns_with_backslashes(self):
         self.mod.write('def a_func():\n    return 1\\\n        + 2\na = a_func()\n')
         self._inline2(self.mod, self.mod.read().index('a_func') + 1)
-        self.assertEquals('a = 1 + 2\n',
-                          self.mod.read())
+        self.assertEquals('a = 1 + 2\n', self.mod.read())
 
     def test_a_function_with_pass_body(self):
         self.mod.write('def a_func():\n    print(1)\na = a_func()\n')
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.