Commits

Anonymous committed f385ea4

From package import module
From package import * does not do anything

Comments (0)

Files changed (4)

+- Not proposing variables which are not defined yet : May 23, 2006
+
+
 - Auto-completion should ignore current statement : May 23, 2006
 
 

docs/workingon.txt

 *** Auto-complete from-import imported objects @ 3 ***
 
-- Add Project.find_module(module_name) which should return a list
-- Resource.__hash__
-- 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
+- from package import package
+- from pkg import * should not import anything
 
-* from package import package
 ? Directories should contain __init__.py to be packages
-* from pkg import * should not import anything
-
+? Auto-completion on relative imports
 ? 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
-
-
 ? Better version number selection
 ? Add rope.scopes module
 ? Completions are context dependant

rope/codeassist.py

         self.children = children
 
 
-def _get_global_names_in_module(project, modname):
-    found_modules = project.find_module(modname)
-    if not found_modules:
-        return {}
-    
+def _get_global_names_in_module(module):
     result = {}
     class _GlobalModuleVisitor(object):
         def visitFunction(self, node):
             result[node.name] =  'class'
         def visitAssName(self, node):
             result[node.name] = 'global_variable'
-    ast = compiler.parse(found_modules[0].read())
+    ast = compiler.parse(module.read())
     compiler.walk(ast, _GlobalModuleVisitor())
     return result
-    
+
+def _get_package_children(package):
+    result = {}
+    for resource in package.get_children():
+        if resource.is_folder():
+            result[resource.get_name()] = 'module'
+        elif resource.get_name().endswith('.py'):
+            result[resource.get_name()[:-3]] = 'module'
+    return result
+
 
 class _ScopeVisitor(object):
     def __init__(self, project, starting, start_line):
                 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] == '*':
+        found_modules = self.project.find_module(node.modname)
+        global_names = {}
+        is_module = True
+        if found_modules:
+            module = found_modules[0]
+            if module.is_folder():
+                global_names = _get_package_children(found_modules[0])
+                is_module = False
+            else:
+                global_names = _get_global_names_in_module(found_modules[0])
+        if node.names[0][0] == '*' and is_module:
             for (name, kind) in global_names.iteritems():
                 if name.startswith(self.starting) and not name.startswith('_'):
                     self.scope.var_dict[name] = CompletionProposal(name, kind)
             result.update(current_scope.var_dict)
             new_scope = None
             for scope in current_scope.children:
-                if scope.lineno < lineno:
+                if scope.lineno < lineno + 1:
                     new_scope = scope
                 else:
                     break

ropetest/codeassisttest.py

         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_in_result('my_var', 'global_variable', result)
 
+    def test_reporting_params_when_in_the_first_line_of_a_function(self):
+        code = 'def f(param):\n    para'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('param', 'local_variable', result)
+
 
 class CodeAssistInProjectsTest(unittest.TestCase):
     def setUp(self):
         samplemod.write("class SampleClass(object):\n    def sample_method():\n        pass" + \
                         "\n\ndef sample_func():\n    pass\nsample_var = 10\n" + \
                         "\ndef _underlined_func():\n    pass\n\n" )
+        package = self.project.create_package(self.project.get_root_folder(), 'package')
+        nestedmod = self.project.create_module(package, 'nestedmod')
         self.assist = self.project.get_code_assist()
 
     def assert_proposal_in_result(self, completion, kind, result):
         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_not_in_result('_underlined_func', 'function', result)
 
+    def test_from_package_import_mod(self):
+        code = 'from package import nestedmod\nnest'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('nestedmod', 'module', result)
+
+    def test_from_package_import_star(self):
+        code = 'from package import *\nnest'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_not_in_result('nestedmod', 'module', result)
+
+    def test_unknown_when_module_cannot_be_found(self):
+        code = 'from doesnotexist import nestedmod\nnest'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('nestedmod', 'unknown', result)
+
 
 if __name__ == '__main__':
     unittest.main()