Anonymous avatar Anonymous committed f42896c

Refactoring visitors in codeassist module
When from mod import * not importing names starting with an underscore
Changing Project.find_module to return packages too

Comments (0)

Files changed (6)

 * Static type inference @ 6
 
 
-* Changing next/prev word to stop on underline and capital letters @ 1
-
-
 * Commenting and uncommenting lines @ 1
 
 
 * Rename local variable @ 2
 
 
+* Changing next/prev word to stop on underline and capital letters @ 1
+
+
 * Rename module refactoring @ 2
 
 
 * Auto completing and comments and strings @ 1
 
 
+> Public release 0.2 : June 4, 2006
+
+
 * Auto completion after "."s @ 3
   * Auto-completing "self."s
 
 
-> Public release 0.2 : June 4, 2006
-
-
 * Proposing templates in auto-completion proposals @ 2
 
 

docs/workingon.txt

 - imports inside functions
 - from module import *
 - Add new TestCase to runtests.py
+- Reduce duplication in visitors in codeassist.py
+- from mod import * should not import names starting with an underscore
+- Project.find_module might return package
 
-* Star in functions
-* from mod import * should not import names starting with an underscore
 * from package import package
+? Directories should contain __init__.py to be packages
+* from pkg import * should not import anything
+
+? Python C extensions
 ? from name import *: loads everything without starting underscore or __all__
 ? Packages: First look in the __init__.py module for that name and if
   it did not exist, look for submodules or packages
-? Reduce the duplication in visitors in codeassist.py
+
 
 ? Better version number selection
 ? Add rope.scopes module

rope/codeassist.py

         self.children = children
 
 
-class _FunctionScopeVisitor(object):
+def _get_global_names_in_module(project, modname):
+    found_modules = project.find_module(modname)
+    if not found_modules:
+        return {}
+    
+    result = {}
+    class _GlobalModuleVisitor(object):
+        def visitFunction(self, node):
+            result[node.name] = 'function'
+        def visitClass(self, node):
+            result[node.name] =  'class'
+        def visitAssName(self, node):
+            result[node.name] = 'global_variable'
+    ast = compiler.parse(found_modules[0].read())
+    compiler.walk(ast, _GlobalModuleVisitor())
+    return result
+    
+
+class _ScopeVisitor(object):
     def __init__(self, project, starting, start_line):
         self.project = project
         self.starting = starting
         global_names = _get_global_names_in_module(self.project, node.modname)
         if node.names[0][0] == '*':
             for (name, kind) in global_names.iteritems():
-                if name.startswith(self.starting):
+                if name.startswith(self.starting) and not name.startswith('_'):
                     self.scope.var_dict[name] = CompletionProposal(name, kind)
             return
         for (name, alias) in node.names:
                     self.scope.var_dict[imported] = CompletionProposal(imported, 'unknown')
 
     def visitAssName(self, node):
-        if node.name.startswith(self.starting):
-            self.scope.var_dict[node.name] = CompletionProposal(node.name, 'local_variable')
-        
+        pass
 
     def visitFunction(self, node):
         if node.name.startswith(self.starting):
 
     def visitClass(self, node):
         if node.name.startswith(self.starting):
-            self.result[node.name] = CompletionProposal(node.name, 'class')
+            self.scope.var_dict[node.name] = CompletionProposal(node.name, 'class')
         new_visitor = _ClassScopeVisitor.walk_class(self.project, self.starting, node)
         self.scope.children.append(new_visitor.scope)
 
+
+class _FunctionScopeVisitor(_ScopeVisitor):
+    def __init__(self, project, starting, start_line):
+        super(_FunctionScopeVisitor, self).__init__(project, starting, start_line)
+
+    def visitAssName(self, node):
+        if node.name.startswith(self.starting):
+            self.scope.var_dict[node.name] = CompletionProposal(node.name, 'local_variable')
+        
+
     @staticmethod
     def walk_function(project, starting, function_node):
         new_visitor = _FunctionScopeVisitor(project, starting, function_node.lineno)
         return new_visitor
 
 
-class _ClassScopeVisitor(object):
+class _ClassScopeVisitor(_ScopeVisitor):
     def __init__(self, project, starting, start_line):
-        self.project = project
-        self.starting = starting
-        self.scope = _Scope(start_line, {}, [])
+        super(_ClassScopeVisitor, self).__init__(project, starting, start_line)
 
     def visitImport(self, node):
         pass
         return new_visitor
 
 
-def _get_global_names_in_module(project, modname):
-    found_modules = project.find_module(modname)
-    if not found_modules:
-        return {}
-    
-    result = {}
-    class _GlobalModuleVisitor(object):
-        def visitFunction(self, node):
-            result[node.name] = 'function'
-        def visitClass(self, node):
-            result[node.name] =  'class'
-        def visitAssName(self, node):
-            result[node.name] = 'global_variable'
-    ast = compiler.parse(found_modules[0].read())
-    compiler.walk(ast, _GlobalModuleVisitor())
-    return result
-    
-
-class _GlobalScopeVisitor(object):
+class _GlobalScopeVisitor(_ScopeVisitor):
     
     def __init__(self, project, starting):
-        self.project = project
-        self.starting = starting
-        self.scope = _Scope(0, {}, [])
-
-    def visitImport(self, node):
-        for import_pair in node.names:
-            name, alias = import_pair
-            imported = name
-            if alias is not None:
-                imported = alias
-            if imported.startswith(self.starting):
-                self.scope.var_dict[imported] = CompletionProposal(imported, 'module')
-
-    def visitFrom(self, node):
-        global_names = _get_global_names_in_module(self.project, node.modname)
-        if node.names[0][0] == '*':
-            for (name, kind) in global_names.iteritems():
-                if name.startswith(self.starting):
-                    self.scope.var_dict[name] = CompletionProposal(name, kind)
-            return
-        for (name, alias) in node.names:
-            imported = name
-            if alias is not None:
-                imported = alias
-            if imported.startswith(self.starting):
-                if global_names.has_key(name):
-                    self.scope.var_dict[imported] = CompletionProposal(imported, global_names[name])
-                else:
-                    self.scope.var_dict[imported] = CompletionProposal(imported, 'unknown')
+        super(_GlobalScopeVisitor, self).__init__(project, starting, 1)
 
     def visitAssName(self, node):
         if node.name.startswith(self.starting):
             self.scope.var_dict[node.name] = CompletionProposal(node.name, 'global_variable')
         
 
-    def visitFunction(self, node):
-        if node.name.startswith(self.starting):
-            self.scope.var_dict[node.name] = CompletionProposal(node.name, 'function')
-        new_visitor = _FunctionScopeVisitor.walk_function(self.project, self.starting, node)
-        self.scope.children.append(new_visitor.scope)
-
-    def visitClass(self, node):
-        if node.name.startswith(self.starting):
-            self.scope.var_dict[node.name] = CompletionProposal(node.name, 'class')
-        new_visitor = _ClassScopeVisitor.walk_class(self.project, self.starting, node)
-        self.scope.children.append(new_visitor.scope)
-
-
 class ICodeAssist(object):
     def complete_code(self, source, offset):
         pass
         packages = module_name.split('.')
         result = []
         for src in source_folders:
-            current_resource = src
+            module = src
             found = True
             for pkg in packages[:-1]:
-                try:
-                    if not current_resource.is_folder():
-                        found = False
-                        break
-                    current_resource = current_resource.get_child(pkg)
-                except RopeException, e:
+                if  module.is_folder() and module.has_child(pkg):
+                    module = module.get_child(pkg)
+                else:
                     found = False
                     break
-            try:
-                if current_resource.is_folder():
-                    current_resource = current_resource.get_child(packages[-1] + '.py')
-                else:
-                    found = False
-            except RopeException, e:
+
+            if module.is_folder() and module.has_child(packages[-1]) and\
+               module.get_child(packages[-1]).is_folder():
+                module = module.get_child(packages[-1])
+            elif module.is_folder() and module.has_child(packages[-1] + '.py') and\
+               not module.get_child(packages[-1] + '.py').is_folder():
+                module = module.get_child(packages[-1] + '.py')
+            else:
                 found = False
             if found:
-                result.append(current_resource)
+                result.append(module)
         return result
 
 
             child_path = name
         return self.project.get_resource(child_path)
     
+    def has_child(self, name):
+        try:
+            self.get_child(name)
+            return True
+        except RopeException:
+            return False
+
     def get_files(self):
         result = []
         for resource in self.get_children():

ropetest/codeassisttest.py

         self.project = Project(self.project_root)
         samplemod = self.project.create_module(self.project.get_root_folder(), 'samplemod')
         samplemod.write("class SampleClass(object):\n    def sample_method():\n        pass" + \
-                        "\n\ndef sample_func():\n    pass\nsample_var = 10\n")
+                        "\n\ndef sample_func():\n    pass\nsample_var = 10\n" + \
+                        "\ndef _underlined_func():\n    pass\n\n" )
         self.assist = self.project.get_code_assist()
 
     def assert_proposal_in_result(self, completion, kind, result):
         code = 'from samplemod import *\nSample'
         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_in_result('SampleClass', 'class', result)
+
+    def test_from_import_star2(self):
         code = 'from samplemod import *\nsample'
         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_in_result('sample_func', 'function', result)
         self.assert_proposal_in_result('sample_var', 'global_variable', result)
 
+    def test_from_import_star_not_imporing_underlined(self):
+        code = 'from samplemod import *\n_under'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_not_in_result('_underlined_func', 'function', result)
+
 
 if __name__ == '__main__':
     unittest.main()

ropetest/projecttest.py

         self.assertTrue(samplemod1 in found_modules and samplemod2 in found_modules and \
                         samplemod3 in found_modules)
 
+    def test_find_module_packages(self):
+        src = self.project.get_root_folder()
+        samplepkg = self.project.create_package(src, 'samplepkg')
+        found_modules = self.project.find_module('samplepkg')
+        self.assertEquals(1, len(found_modules))
+        self.assertEquals(samplepkg, found_modules[0])
+
+    def test_find_module_when_module_and_package_with_the_same_name(self):
+        src = self.project.get_root_folder()
+        samplemod = self.project.create_module(src, 'sample')
+        samplepkg = self.project.create_package(src, 'sample')
+        found_modules = self.project.find_module('sample')
+        self.assertEquals(1, len(found_modules))
+        self.assertEquals(samplepkg, found_modules[0])
+
 
 class FileFinderTest(unittest.TestCase):
     def setUp(self):
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.