Anonymous avatar Anonymous committed 5edf5f9

Added PyModule.logical_lines

Comments (0)

Files changed (8)

rope/base/pyobjects.py

         if isinstance(source_code, unicode):
             source_code = source_code.encode('utf-8')
         self.source_code = source_code
-        self._lines = None
         try:
             ast_node = ast.parse(source_code.rstrip(' \t'))
         except SyntaxError, e:
         self.defineds = None
         super(PyModule, self).__init__(pycore, ast_node, resource)
 
-    def _get_lines(self):
-        if self._lines is None:
-            self._lines = rope.base.codeanalyze.\
-                          SourceLinesAdapter(self.source_code)
-        return self._lines
-
-    lines = property(_get_lines, doc="return a `SourceLinesAdapter`")
-
     def _get_defined_objects(self):
         if self.defineds is None:
             self._get_structural_attributes()
     def _create_scope(self):
         return rope.base.pyscopes.GlobalScope(self.pycore, self)
 
+    _lines = None
+    @property
+    def lines(self):
+        """return a `SourceLinesAdapter`"""
+        if self._lines is None:
+            self._lines = rope.base.codeanalyze.\
+                          SourceLinesAdapter(self.source_code)
+        return self._lines
+
+    _logical_lines = None
+    @property
+    def logical_lines(self):
+        """return a `LogicalLinesFinder`"""
+        if self._logical_lines is None:
+            self._logical_lines = rope.base.codeanalyze.LogicalLineFinder(self.lines)
+        return self._logical_lines
+
 
 class PyPackage(_PyModule):
 

rope/base/pyscopes.py

 class _HoldingScopeFinder(object):
 
     def __init__(self, pymodule):
-        self.source_code = pymodule.source_code
-        self.lines = pymodule.lines
+        self.pymodule = pymodule
 
     def get_indents(self, lineno):
         return rope.base.codeanalyze.count_line_indents(
         if not scope.parent:
             return self.lines.length()
         end = scope.pyobject.get_ast().body[-1].lineno
-        finder = rope.base.codeanalyze.LogicalLineFinder(self.lines)
         body_indents = self._get_body_indents(scope)
-        for l in finder.generate_starts(min(end + 1, self.lines.length()),
-                                        self.lines.length() + 1):
+        for l in self.logical_lines.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
                     end = l
         return end
 
+    @property
+    def lines(self):
+        return self.pymodule.lines
+
+    @property
+    def code(self):
+        return self.pymodule.source_code
+
+    @property
+    def logical_lines(self):
+        return self.pymodule.logical_lines
 
 class TemporaryScope(Scope):
     """Currently used for list comprehensions and generator expressions

rope/contrib/generate.py

     def get_insertion_lineno(self):
         lines = self.goal_pymodule.lines
         if self.goal_scope == self.source_scope:
-            line_finder = codeanalyze.LogicalLineFinder(lines)
+            line_finder = self.goal_pymodule.logical_lines
             lineno = lines.get_line_number(self.offset)
             lineno = line_finder.get_logical_line_in(lineno)[0]
             root = suites.ast_suite_tree(self.goal_scope.pyobject.get_ast())

rope/refactor/encapsulate_field.py

 
     def get_changed_module(self):
         result = []
-        line_finder = None
         word_finder = rope.base.codeanalyze.WordRangeFinder(self.source)
         for occurrence in self.occurrences_finder.find_occurrences(self.resource,
                                                                    self.pymodule):
                     var_name = self.source[occurrence.get_primary_range()[0]:
                                            start] + self.getter + '()'
                     result.append(self.setter + '(' + var_name + ' %s ' % assignment_type[:-1])
-                if line_finder is None:
-                    line_finder = rope.base.codeanalyze.LogicalLineFinder(self.lines)
                 current_line = self.lines.get_line_number(start)
-                start_line, end_line = line_finder.get_logical_line_in(current_line)
+                start_line, end_line = self.pymodule.logical_lines.\
+                                       get_logical_line_in(current_line)
                 self.last_set = self.lines.get_line_end(end_line)
                 end = self.source.index('=', end) + 1
                 self.set_index = len(result)
             self.last_set = None
 
     def _is_assigned_in_a_tuple_assignment(self, occurance):
-        line_finder = rope.base.codeanalyze.LogicalLineFinder(self.lines)
         offset = occurance.get_word_range()[0]
         lineno = self.lines.get_line_number(offset)
-        start_line, end_line = line_finder.get_logical_line_in(lineno)
+        start_line, end_line = self.pymodule.logical_lines.\
+                               get_logical_line_in(lineno)
         start_offset = self.lines.get_line_start(start_line)
 
         line = self.source[start_offset:self.lines.get_line_end(end_line)]

rope/refactor/extract.py

         self.make_global = make_global
 
     def _init_parts(self, start, end):
-        self.logical_lines = codeanalyze.LogicalLineFinder(self.lines)
-
         self.region = (self._choose_closest_line_end(start),
                        self._choose_closest_line_end(end, end=True))
 
         self.lines_region = (self.lines.get_line_start(self.region_lines[0]),
                              self.lines.get_line_end(self.region_lines[1]))
 
+    @property
+    def logical_lines(self):
+        return self.pymodule.logical_lines
+
 
     def _init_scope(self):
         start_line = self.region_lines[0]

rope/refactor/inline.py

     def _get_scope_range(self):
         scope = self.pyfunction.get_scope()
         lines = self.pymodule.lines
+        logicals = self.pymodule.logical_lines
         start_line = scope.get_start()
         if self.pyfunction.get_ast().decorators:
             decorators = self.pyfunction.get_ast().decorators
             if hasattr(decorators[0], 'lineno'):
                 start_line = decorators[0].lineno
         start_offset = lines.get_line_start(start_line)
-        end_line = codeanalyze.LogicalLineFinder(lines).\
-                   get_logical_line_in(scope.get_end())[1]
+        end_line = logicals.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)
     def _get_removed_range(self):
         scope = self.pyfunction.get_scope()
         lines = self.pymodule.lines
+        logical = self.pymodule.logical_lines
         start_line = scope.get_start()
         start, end = self._get_scope_range()
-        end_line = codeanalyze.LogicalLineFinder(lines).\
-                   get_logical_line_in(scope.get_end())[1]
+        end_line = logical.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
             return
         end_parens = self._find_end_parens(self.source, end - 1)
         lineno = self.lines.get_line_number(start)
-        start_line, end_line = codeanalyze.LogicalLineFinder(self.lines).\
+        start_line, end_line = self.pymodule.logical_lines.\
                                get_logical_line_in(lineno)
         line_start = self.lines.get_line_start(start_line)
         line_end = self.lines.get_line_end(end_line)
     assignment = pyname.assignments[0]
     definition_line = assignment.ast_node.lineno
     lines = pymodule.lines
-    start, end = codeanalyze.LogicalLineFinder(lines).\
-                 get_logical_line_in(definition_line)
+    logicals = pymodule.logical_lines
+    start, end = logicals.get_logical_line_in(definition_line)
     definition_with_assignment = _join_lines(
         [lines.get_line(n) for n in range(start, end + 1)])
     if assignment.levels:

rope/refactor/introduce_parameter.py

 
     def _get_header_offsets(self):
         lines = self.pymodule.lines
-        line_finder = codeanalyze.LogicalLineFinder(lines)
         start_line = self.pyfunction.get_scope().get_start()
-        end_line = line_finder.get_logical_line_in(start_line)[1]
+        end_line = self.pymodule.logical_lines.\
+                   get_logical_line_in(start_line)[1]
         start = lines.get_line_start(start_line)
         end = lines.get_line_end(end_line)
         start = self.pymodule.source_code.find('def', start) + 4

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]
+    end_line = pymodule.logical_lines.get_logical_line_in(scope.get_end())[1]
     start = lines.get_line_start(start_line)
     end = min(lines.get_line_end(end_line) + 1, len(pymodule.source_code))
     return start, end
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.