Anonymous avatar Anonymous committed 0632e9a

Applying the completion in GraphicalEditor

Comments (0)

Files changed (4)

docs/workingon.txt

 *** Auto-complete imports names @ 3 ***
 
+- Applying the completion
+
 * What if the statement in current line is spread in more than one line
 * Extract listbox; duplicates in editor and core
 * Complete as you type (updating proposal list while typing)
-* Applying the completion
 ? The connection between ASTs and Type Hierarchies
 
 * Better font selection on windows

rope/codeassist.py

 
 
 class CompletionProposal(object):
-    global_variable = 'global_variable'
-
+    """A completion proposal.
+    
+    The kind instance variable shows the type of the completion and
+    can be global_variable, function, class
+    
+    """
     def __init__(self, completion, kind):
         self.completion = completion
         self.kind = kind
 
 
+class CompletionResult(object):
+    def __init__(self, proposals=[], start_offset=0, end_offset=0):
+        self.proposals = proposals
+        self.start_offset = start_offset
+        self.end_offset = end_offset
+
+
 class _GlobalVisitor(object):
     
     def __init__(self, starting):
     def visitFunction(self, node):
         if node.name.startswith(self.starting):
             self.result[node.name] = CompletionProposal(node.name, 'function')
-    
+
     def visitClass(self, node):
         if node.name.startswith(self.starting):
             self.result[node.name] = CompletionProposal(node.name, 'class')
 
 class NoAssist(ICodeAssist):
     def complete_code(self, source_code, offset):
-        return []
+        return CompletionResult()
 
 
 class CodeAssist(ICodeAssist):
-    def _find_starting(self, source_code, offset):
-        starting = ''
+    def _find_starting_offset(self, source_code, offset):
         current_offset = offset - 1
         while current_offset >= 0 and (source_code[current_offset].isalnum() or
                                        source_code[current_offset] == '_'):
-            starting = source_code[current_offset] + starting
             current_offset -= 1;
-        return starting
+        return current_offset + 1
 
     def _comment_current_line(self, source_code, offset):
         line_beginning = offset - 1
     def complete_code(self, source_code, offset):
         if offset > len(source_code):
             return []
-        starting = self._find_starting(source_code, offset)
+        starting_offset = self._find_starting_offset(source_code, offset)
+        starting = source_code[starting_offset:offset]
         commented_source_code = self._comment_current_line(source_code, offset)
         result = {}
         try:
         visitor = _GlobalVisitor(starting)
         compiler.walk(code_ast, visitor)
         result.update(visitor.result)
-        return result.values()
+        return CompletionResult(result.values(), starting_offset, offset)
 
         scrollbar['command'] = proposals.yview
         proposals.config(yscrollcommand=scrollbar.set)
         result = self.code_assist.complete_code(self.get_text(), self.get_current_offset())
-        for proposal in result:
+        for proposal in result.proposals:
             proposals.insert(END, proposal.completion)
         if result:
             proposals.selection_set(0)
         def open_selected():
             selection = proposals.curselection()
             if selection:
-                selected = proposals.get(selection[0])
-                # TODO: insert the completion
+                selected = int(selection[0])
+                self.text.delete('0.0 +%dc' % result.start_offset,
+                                 '0.0 +%dc' % result.end_offset)
+                self.text.insert('0.0 +%dc' % result.start_offset,
+                                 result.proposals[selected].completion)
                 toplevel.destroy()
         def cancel():
             toplevel.destroy()

ropetest/codeassisttest.py

         self.assist.complete_code('', 0)
 
     def assert_proposal_in_result(self, completion, kind, result):
-        for proposal in result:
+        for proposal in result.proposals:
             if proposal.completion == completion and proposal.kind == kind:
                 return
         self.fail('Completion %s not proposed' % completion)
 
     def assert_proposal_not_in_result(self, completion, kind, result):
-        for proposal in result:
+        for proposal in result.proposals:
             if proposal.completion == completion and proposal.kind == kind:
                 self.fail('Completion %s was proposed' % completion)
 
     def test_proposing_each_name_at_most_once(self):
         code = 'variable = 10\nvariable = 20\nt = vari'
         result = self.assist.complete_code(code, len(code))
-        count = len([x for x in result if x.completion == 'variable' and x.kind == 'global_variable'])
+        count = len([x for x in result.proposals
+                     if x.completion == 'variable' and x.kind == 'global_variable'])
         self.assertEquals(1, count)
 
     def test_throwing_exception_in_case_of_syntax_errors(self):
         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_not_in_result('my_', 'global_variable', result)
 
+    def test_completion_result(self):
+        code = 'my_global = 10\nt = my'
+        result = self.assist.complete_code(code, len(code))
+        self.assertEquals(len(code) - 2, result.start_offset)
+        self.assertEquals(len(code), result.end_offset)
+
 
 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.