Anonymous avatar Anonymous committed 30f0db5

Added PyScope.logical_line

PyScope.get_end() returns raw line end.

Comments (0)

Files changed (4)

rope/base/codeanalyze.py

     def _init_logicals(self):
         self._starts = [False] * (self.lines.length() + 1)
         self._ends = [False] * (self.lines.length() + 1)
-        for start, end in self.logical_lines.logical_lines():
+        for start, end in self.logical_lines.generate_regions():
             self._starts[start] = True
             self._ends[end] = True
 
         start = line_number
         while start > 0 and not self.starts[start]:
             start -= 1
+        if start == 0:
+            try:
+                start = self.starts.index(True, line_number)
+            except ValueError:
+                return (line_number, line_number)
         return (start, self.ends.index(True, start))
 
     def generate_starts(self, start_line=1, end_line=None):
                 lineno = e.lineno + block_start - 1
                 indents = count_line_indents(self.lines.get_line(lineno))
 
-    def get_logical_line_in(self, line_number):
-        warnings.warn('Use `LogicalLineFinder.logical_line_in()` instead',
-                      DeprecationWarning, stacklevel=2)
-        return self.logical_line_in(line_number)
-
     def generate_starts(self, start_line=1, end_line=None):
-        for start, end in self.logical_lines(start_line, end_line):
+        for start, end in self.generate_regions(start_line, end_line):
             yield start
 
-    def logical_lines(self, start_line=1, end_line=None):
+    def generate_regions(self, start_line=1, end_line=None):
         # XXX: `block_start` should be at a better position!
         block_start = 1
         readline = LinesToReadline(self.lines, block_start)
             if real_start >= start_line:
                 yield (real_start, real_end)
 
+    def get_logical_line_in(self, line_number):
+        warnings.warn('Use `LogicalLineFinder.logical_line_in()` instead',
+                      DeprecationWarning, stacklevel=2)
+        return self.logical_line_in(line_number)
+
     def _block_logical_line(self, block_start, line_number):
         readline = LinesToReadline(self.lines, block_start)
         shifted = line_number - block_start + 1
 
     def _logical_lines(self, readline):
         last_end = 1
-        for current in self._logical_ends(readline):
-            yield (last_end, current)
-            last_end = current + 1
-
-    def _logical_ends(self, readline):
-        for current in tokenize.generate_tokens(readline):
-            current_lineno = current[2][0]
-            if current[0] == token.NEWLINE:
-                yield current_lineno
+        for current_token in tokenize.generate_tokens(readline):
+            current = current_token[2][0]
+            if current_token[0] == token.NEWLINE:
+                yield (last_end, current)
+                last_end = current + 1
 
     def _first_non_blank(self, line_number):
         current = line_number

rope/base/pyscopes.py

     _end = None
 
     def get_end(self):
+        pymodule = self._get_global_scope().pyobject
+        return pymodule.logical_lines.logical_line_in(self.logical_end)[1]
+
+    def get_logical_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
 
+    start = property(get_start)
+    end = property(get_end)
+    logical_end = property(get_logical_end)
+
     def get_kind(self):
         pass
 

rope/refactor/inline.py

             if hasattr(decorators[0], 'lineno'):
                 start_line = decorators[0].lineno
         start_offset = lines.get_line_start(start_line)
-        end_line = logicals.logical_line_in(scope.get_end())[1]
-        end_offset = min(lines.get_line_end(end_line) + 1,
+        end_offset = min(lines.get_line_end(scope.end) + 1,
                          len(self.pymodule.source_code))
         return (start_offset, end_offset)
 
         logical = self.pymodule.logical_lines
         start_line = scope.get_start()
         start, end = self._get_scope_range()
-        end_line = logical.logical_line_in(scope.get_end())[1]
+        end_line = scope.get_end()
         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 = pymodule.logical_lines.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))
+    end = min(lines.get_line_end(scope.end) + 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.