Commits

Anonymous committed bc54594

Sorting code assist proposals

Comments (0)

Files changed (6)

 ===========
 
 
+- Sorting code assist proposals : July 22, 2006
+
+
 - Not renaming names in strings and comments in refactorings : July 22, 2006
 
 
   * Showing modified file status
 
 
-* Sorting code assist proposals @ 5h
-
-
 Remaining Stories
 -----------------
 * Change program goals and description; principles.txt

docs/workingon.txt

-Sorting code assist proposals @ 5h
-==================================
-
-
 Pre-Release Refactorings
 ========================
 

rope/codeassist.py

         else:
             return (None, None)
 
+
+class ProposalSorter(object):
+
+    def __init__(self, code_assist_proposals):
+        self.proposals = code_assist_proposals
+    
+    def get_sorted_proposal_list(self):
+        local_proposals = []
+        global_proposals = []
+        others = []
+        for proposal in self.proposals.completions:
+            if proposal.kind == 'global':
+                global_proposals.append(proposal)
+            elif proposal.kind == 'local':
+                local_proposals.append(proposal)
+            else:
+                others.append(proposal)
+        template_proposals = self.proposals.templates
+        result = []
+        result.extend(local_proposals)
+        result.extend(global_proposals)
+        result.extend(template_proposals)
+        result.extend(others)
+        return result
+
         toplevel.title('Code Assist Proposals')
         enhanced_list = EnhancedList(toplevel, _CompletionListHandle(self, toplevel, result),
                                      title='Code Assist Proposals')
-        for proposal in result.completions:
-            enhanced_list.add_entry(proposal)
-        for proposal in result.templates:
+        proposals = rope.codeassist.ProposalSorter(result).get_sorted_proposal_list()
+        for proposal in proposals:
             enhanced_list.add_entry(proposal)
         start_index = self.text.index('0.0 +%dc' % result.start_offset)
         initial_cursor_position = str(self.text.index(INSERT))
             if len(event.char) == 1 and (event.char.isalnum() or
                                          event.char in string.punctuation):
                 self.text.insert(INSERT, event.char)
-            if event.keysym == 'space':
+            elif event.keysym == 'space':
                 self.text.insert(INSERT, ' ')
-            if event.keysym == 'BackSpace':
+            elif event.keysym == 'BackSpace':
                 self.text.delete(INSERT + '-1c')
-            if self.text.compare(initial_cursor_position, '>', INSERT):
+            elif self.text.compare(initial_cursor_position, '>', INSERT):
                 toplevel.destroy()
                 return
+            else:
+                return
             new_name = self.text.get(start_index, INSERT)
             enhanced_list.clear()
-            for proposal in result.completions:
-                if proposal.name.startswith(new_name):
-                    enhanced_list.add_entry(proposal)
-            for proposal in result.templates:
+            for proposal in proposals:
                 if proposal.name.startswith(new_name):
                     enhanced_list.add_entry(proposal)
         enhanced_list.list.focus_set()

ropetest/codeassisttest.py

 import os
 import unittest
 
-from rope.codeassist import PythonCodeAssist, RopeSyntaxError, Template
+from rope.codeassist import PythonCodeAssist, RopeSyntaxError, Template, ProposalSorter
 from rope.project import Project
 from ropetest import testutils
 
                'Sample("hey").a_'
         result = self.assist.assist(code, len(code))
         self.assert_completion_in_result('a_method', 'attribute', result)
-    
+
+    def test_proposals_sorter(self):
+        self.assist.add_template('my_sample_template', '')
+        code = 'def my_sample_function(self):\n' + \
+               '    my_sample_var = 20\n' + \
+               '    my_sample_'
+        result = self.assist.assist(code, len(code))
+        sorted_proposals = ProposalSorter(result).get_sorted_proposal_list()
+        self.assertEquals('my_sample_var', sorted_proposals[0].name)
+        self.assertEquals('my_sample_function', sorted_proposals[1].name)
+        self.assertEquals('my_sample_template', sorted_proposals[2].name)
+
 
 class CodeAssistInProjectsTest(unittest.TestCase):