Commits

Anonymous committed 5494649

Completing after parens

  • Participants
  • Parent commits b7596d2

Comments (0)

Files changed (6)

File docs/workingon.txt

 Refactoring
 ===========
 
-- PyNames with None objects
-
 * Names in strings or comments
 * Using ScopeNameFinder in _CodeCompletionCollector
 * Changing HoldingScopeFinder to get source_code instead of lines

File rope/codeanalyze.py

+
 class WordRangeFinder(object):
 
     def __init__(self, source_code):
             current_offset = self._find_last_non_space_char(current_offset)
             if current_offset >= 0:
                 word_start = self._find_statement_start(current_offset)
+                word_start = self.find_word_start(current_offset)
                 result.append(self.source_code[word_start:current_offset + 1])
                 current_offset = word_start - 1
             current_offset = self._find_last_non_space_char(current_offset)

File rope/codeassist.py

 
 from rope.exceptions import RopeException
 from rope.codeanalyze import (StatementRangeFinder, ArrayLinesAdapter,
-                              HoldingScopeFinder, WordRangeFinder, ScopeNameFinder)
+                              HoldingScopeFinder, WordRangeFinder, ScopeNameFinder,
+                              SourceLinesAdapter)
 
 class RopeSyntaxError(RopeException):
     pass
         self.pycore = self.project.get_pycore()
         self.lines = source_code.split('\n')
         self.source_code = source_code
-        scope_finder = HoldingScopeFinder(self.lines)
-        self.lineno = scope_finder.get_location(offset)[0]
-        self.current_indents = scope_finder.get_indents(self.lineno)
+        source_lines = SourceLinesAdapter(source_code)
+        self.lineno = source_lines.get_line_number(offset)
+        self.current_indents = self._get_line_indents(source_lines.get_line(self.lineno))
         self._comment_current_statement()
+        self.source_code = '\n'.join(self.lines)
 
-    def _get_line_indents(self, line_number):
+    def _get_line_indents(self, line):
         indents = 0
-        for char in self.lines[line_number]:
+        for char in line:
             if char == ' ':
                 indents += 1
             else:
         range_finder.analyze()
         start = range_finder.get_statement_start() - 1
         end = range_finder.get_scope_end() - 1
-        last_indents = self._get_line_indents(start)
+        last_indents = self._get_line_indents(self.lines[start])
         self.lines[start] = last_indents * ' ' + 'pass'
         for line in range(start + 1, end + 1):
             self.lines[line] = '#' # + lines[line]
         scope_finder = HoldingScopeFinder(self.lines)
         return scope_finder.get_holding_scope(base_scope, self.lineno, self.current_indents)
 
-    def _get_dotted_completions(self, scope):
+    def _get_dotted_completions(self, module_scope, holding_scope):
         result = {}
-        if len(self.starting) > 1:
-            element = scope.lookup(self.starting[0])
-            if element is not None:
-                consistent = True
-                for token in self.starting[1:-1]:
-                    if token in element.get_attributes():
-                        element = element.get_attributes()[token]
-                    else:
-                        consistent = False
-                        break
-                if consistent:
-                    for name, pyname in element.get_attributes().iteritems():
-                        if name.startswith(self.starting[-1]) or self.starting[-1] == '':
-                            result[name] = CompletionProposal(name, 'attribute')
+        pyname_finder = ScopeNameFinder(self.source_code, module_scope)
+        element = pyname_finder.get_pyname_in_scope(holding_scope, self.starting[:-1])
+        if element is not None:
+            for name, pyname in element.get_attributes().iteritems():
+                if name.startswith(self.starting[-1]) or self.starting[-1] == '':
+                    result[name] = CompletionProposal(name, 'attribute')
         return result
 
     def _get_undotted_completions(self, scope, result):
 
     def get_code_completions(self):
         try:
-            module_scope = self.pycore.get_string_scope('\n'.join(self.lines))
+            module_scope = self.pycore.get_string_scope(self.source_code)
         except SyntaxError, e:
             raise RopeSyntaxError(e)
         current_scope = module_scope
         result = {}
         inner_scope = self._find_inner_holding_scope(module_scope)
         if len(self.starting) > 1:
-            result.update(self._get_dotted_completions(inner_scope))
+            result.update(self._get_dotted_completions(module_scope, inner_scope))
         else:
             self._get_undotted_completions(inner_scope, result)
         return result

File ropetest/codeanalyzetest.py

 import unittest
 
 from rope.codeanalyze import (StatementRangeFinder, ArrayLinesAdapter,
-                              SourceLinesAdapter)
+                              SourceLinesAdapter, WordRangeFinder)
 
 class StatementRangeFinderTest(unittest.TestCase):
 
         self.assertEquals(1, to_lines.get_line_number(0))
         self.assertEquals(2, to_lines.get_line_number(7))
 
+        
+class WordRangeFinderTest(unittest.TestCase):
+
+    def setUp(self):
+        super(WordRangeFinderTest, self).setUp()
+
+    def tearDown(self):
+        super(WordRangeFinderTest, self).tearDown()
+
+    def test_inside_parans(self):
+        word_finder = WordRangeFinder('a_func(a_var)')
+        self.assertEquals(['a_v'], word_finder.get_name_list_before(10))
+
+
+def suite():
+    result = unittest.TestSuite()
+    result.addTests(unittest.makeSuite(StatementRangeFinderTest))
+    result.addTests(unittest.makeSuite(WordRangeFinderTest))
+    return result
 
 if __name__ == '__main__':
     unittest.main()
-    
+
+

File ropetest/codeassisttest.py

         result = self.assist.get_definition_location(code, len(code) - 3)
         self.assertEquals((None, 3), result)
         
-        
+    def test_code_assists_in_parens(self):
+        code = 'def a_func(a_var):\n    pass\na_var = 10\na_func(a_'
+        result = self.assist.assist(code, len(code))
+        self.assert_completion_in_result('a_var', 'global', result)
+
+
 class CodeAssistInProjectsTest(unittest.TestCase):
 
     def setUp(self):
     result.addTests(unittest.makeSuite(ropetest.indentertest.PythonCodeIndenterTest))
     result.addTests(ropetest.codeassisttest.suite())
     result.addTests(unittest.makeSuite(ropetest.statusbartest.StatusBarTest))
-    result.addTests(unittest.makeSuite(ropetest.codeanalyzetest.StatementRangeFinderTest))
+    result.addTests(ropetest.codeanalyzetest.suite())
     result.addTests(ropetest.pycoretest.suite())
     result.addTests(unittest.makeSuite(ropetest.uihelperstest.UIHelpersTest))
     result.addTests(unittest.makeSuite(ropetest.outlinetest.OutlineTest))