Anonymous avatar Anonymous committed c258b18

Having one SourceLinesAdapter for each PyModule

Comments (0)

Files changed (11)

docs/workingon.txt

 Refactoring
 ===========
 
-- Function decorators and the type of the first parameter
-
-  - normal functions
-  - @classmethod
-  - @staticmethod
-  - otherwise if its name is self
-
-* Creating `SourceLinesAdapter` s are expensive; Having one in `PyModule`
+- Creating `SourceLinesAdapter` s are expensive; Having one in `PyModule`
 
 
 Remaining Stories
   thrown from public classes like `Project`, `PyCore`, `Refactoring`
 * Eliminating `Editor.refactoring`
 * Calling `sys.settrace` for all threads in `rope.runmod` module
-* Keyboard shortcuts for new features
 * Goto definition for ``"# comment.\na_var"``
-* Slow `GraphicalEditor._get_offset`
-* GUI testing redux; make a functests directory?; rename ropetest to unittests?

rope/importutils.py

 import rope.pynames
 import rope.exceptions
 import rope.refactor.rename
-from rope.codeanalyze import SourceLinesAdapter
 
 
 class ImportTools(object):
             self.pymodule = pymodule
         self.pycore = pycore
         self.imports = []
-        self.lines = SourceLinesAdapter(self.pymodule.source_code)
+        self.lines = self.pymodule.lines
     
     def visit_import(self, node, end_line):
         start_line = node.lineno
 import rope.objectinfer
 import rope.refactor
 import rope.dynamicoi
-import rope.codeanalyze
 from rope.exceptions import ModuleNotFoundException
 from rope.pyobjects import *
 

rope/pyobjects.py

 
     def __init__(self, pycore, source_code, resource=None):
         self.source_code = source_code
+        self._lines = None
         ast_node = compiler.parse(source_code.rstrip(' \t'))
         self.star_imports = []
         super(PyModule, self).__init__(pycore, ast_node, resource)
     
+    def _get_lines(self):
+        if self._lines is None:
+            self._lines = rope.codeanalyze.SourceLinesAdapter(self.source_code)
+        return self._lines
+    
+    lines = property(_get_lines, doc="return `SourceLinesAdapter`")
+    
     def _create_concluded_attributes(self):
         result = {}
         for star_import in self.star_imports:
 import compiler
 
-import rope.codeanalyze
 import rope.pynames
 import rope.pyobjects
 import rope.exceptions
     
     def _get_scope_finder(self):
         if self.scope_finder is None:
-            self.scope_finder = _HoldingScopeFinder(self.pyobject.source_code)
+            self.scope_finder = _HoldingScopeFinder(self.pyobject)
         return self.scope_finder
 
 
 
 class _HoldingScopeFinder(object):
 
-    def __init__(self, source_code):
-        self.source_code = source_code
-        self.lines = rope.codeanalyze.SourceLinesAdapter(source_code)
+    def __init__(self, pymodule):
+        self.source_code = pymodule.source_code
+        self.lines = pymodule.lines
     
     def get_indents(self, lineno):
         indents = 0

rope/refactor/extract.py

     def __init__(self, pycore, resource, start_offset, end_offset):
         self.source_code = source_code = resource.read()
         
-        self.lines = rope.codeanalyze.SourceLinesAdapter(source_code)
+        pymodule = pycore.resource_to_pyobject(resource)
+        self.lines = pymodule.lines
         self.line_finder = rope.codeanalyze.LogicalLineFinder(self.lines)
         
         self.region = (self._choose_closest_line_end(start_offset),
         self.region_lines = (self.lines.get_line_start(self.region_linenos[0]),
                              self.lines.get_line_end(self.region_linenos[1]))
         
-        scope = pycore.resource_to_pyobject(resource).get_scope()
-        holding_scope = self._find_holding_scope(scope, self.region_linenos[0])
+        holding_scope = self._find_holding_scope(pymodule.get_scope(), self.region_linenos[0])
         self.scope = (self.lines.get_line_start(holding_scope.get_start()),
                       self.lines.get_line_end(holding_scope.get_end()) + 1)
         
         self.extract_variable = extract_variable
         self.info = info
         
-        self.lines = rope.codeanalyze.SourceLinesAdapter(source_code)
+        self.lines = info.lines
         
         self.first_line_indents = self._get_indents(self.info.region_linenos[0])
         self.scope = pycore.get_string_scope(source_code, resource)

rope/refactor/inline.py

     
     def inline(self):
         definition_line = self.pyname.assigned_asts[0].lineno
-        lines = rope.codeanalyze.SourceLinesAdapter(self.pymodule.source_code)
+        lines = self.pymodule.lines
         start, end = rope.codeanalyze.LogicalLineFinder(lines).\
                      get_logical_line_in(definition_line)
         definition_lines = []

rope/refactor/introduce_factory.py

         source_code = rename_in_module.get_changed_module(pymodule=self.pymodule)
         if source_code is None:
             source_code = self.pymodule.source_code
-        lines = rope.codeanalyze.SourceLinesAdapter(source_code)
+        lines = self.pymodule.lines
         start = self._get_insertion_offset(class_scope, lines)
         result = source_code[:start]
         result += self._get_factory_method(lines, class_scope)

rope/refactor/move.py

         changes.add_change(ChangeFileContents(self.source, source))
 
     def _get_moved_moving_source(self, source):
-        lines = rope.codeanalyze.SourceLinesAdapter(source)
+        lines = self.pycore.resource_to_pyobject(self.source).lines
         scope = self.old_pyname.get_object().get_scope()
         start = lines.get_line_start(scope.get_start())
         end = lines.get_line_end(scope.get_end())
         module_with_imports = self.import_tools.get_module_with_imports(pymodule)
         source = pymodule.source_code
         if module_with_imports.get_import_statements():
-            lines = rope.codeanalyze.SourceLinesAdapter(source)
-            start = lines.get_line_end(module_with_imports.
-                                       get_import_statements()[-1].end_line - 1)
+            start = pymodule.lines.get_line_end(
+                module_with_imports.get_import_statements()[-1].end_line - 1)
             result = source[:start + 1] + '\n\n'
         else:
             result = ''
         return self.import_tools.get_module_with_imports(pymodule)
 
     def _get_moving_element(self):
-        lines = rope.codeanalyze.SourceLinesAdapter(self.source.read())
+        lines = self.pycore.resource_to_pyobject(self.source).lines
         scope = self.old_pyname.get_object().get_scope()
         start = lines.get_line_start(scope.get_start())
         end = lines.get_line_end(scope.get_end())

rope/refactor/sourcetools.py

-import rope.codeanalyze
 
 
 def get_indents(lines, lineno):

ropetest/refactor/movetest.py

         self.assertEquals('pkg2/pkg/mod4.py', self.mod4.get_path())
         self.assertEquals('pkg2/pkg/mod5.py', self.mod5.get_path())
         self.assertEquals('import pkg2.pkg.mod4\nprint pkg2.pkg.mod4', self.mod1.read())
+
+
+if __name__ == '__main__':
+    unittest.main()
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.