Anonymous avatar Anonymous committed 5c35505

Added rope.uihelpers

Comments (0)

Files changed (6)

   Completions for for-loop, except, lambda and with variables
 
 
-* Auto completion type @ 3
+* Auto completion type @ 2
   * Inserting or overwriting
   * Inserting common prefixes
 
   * Sorting proposals
 
 
-* Auto completing and comments and strings @ 1
+* Auto completing and comments and strings @ 2
 
 
 > Public release 0.2pre2 : June 4, 2006

docs/workingon.txt

 *** Proposing templates in auto-completion proposals @ 1 ***
 
+- Show the type of the completion in the codeassist dialog
+
 * Showing templates in completion dialog
 ? Defaults for templates
 * Builtin templates
 ?? Next/prev word should stop consider underline and capitals as spaces
 ?? Better change editor
 ?? GUI testing redux
-? Show the type of the completion in the codeassist dialog
 ? From-import might cache module global variables
 ? Directories should contain __init__.py to be packages in codeassist
 ? Auto-completion on relative imports

rope/codeassist.py

         
 
 class ICodeAssist(object):
-    def complete_code(self, source, offset):
+    def assist(self, source, offset):
         pass
 
 
 class NoAssist(ICodeAssist):
-    def complete_code(self, source_code, offset):
+    def assist(self, source_code, offset):
         return Proposals()
 
 
         while current_pos + len(lines[lineno]) < offset:
             current_pos += len(lines[lineno]) + 1
             lineno += 1
+        current_indents = self._get_line_indents(lines, lineno)
         self._comment_current_statement(lines, lineno)
         source_code = '\n'.join(lines)
         try:
         compiler.walk(code_ast, visitor)
         result = {}
         current_scope = visitor.scope
-        current_indents = self._get_line_indents(lines, lineno)
         while current_scope is not None and \
               (current_scope == visitor.scope or
                self._get_line_indents(lines, current_scope.lineno - 1) < current_indents):
 import rope.searching
 import rope.indenter
 import rope.codeassist
+from rope.uihelpers import EnhancedList
 
 
 class TextEditor(object):
     def _show_completion_window(self):
         result = self.code_assist.assist(self.get_text(), self.get_current_offset())
         toplevel = Toplevel()
-        toplevel.title('Completion Proposals')
-        frame = Frame(toplevel)
-        label = Label(frame, text='Code Assist Proposals')
-        proposals = Listbox(frame, selectmode=SINGLE, width=23, height=7)
-        scrollbar = Scrollbar(frame, orient=VERTICAL)
-        scrollbar['command'] = proposals.yview
-        proposals.config(yscrollcommand=scrollbar.set)
-        for proposal in result.completions:
-            proposal_info = proposal.kind[0].upper() + '  ' + proposal.name
-            proposals.insert(END, proposal_info)
-        if result:
-            proposals.selection_set(0)
-        self.text.see('insert')
-        local_x, local_y, cx, cy = self.text.bbox("insert")
-        x = local_x + self.text.winfo_rootx() + 2
-        y = local_y + cy + self.text.winfo_rooty()
-        #        toplevel.wm_geometry('+%d+%d' % (x, y))
-        #        toplevel.wm_overrideredirect(1)
-        def open_selected():
-            selection = proposals.curselection()
-            if selection:
-                selected = int(selection[0])
+        toplevel.title('Code Assist Proposals')
+        def open_selected(selected):
                 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.completions[selected].name)
+                                 selected.name)
                 toplevel.destroy()
         def cancel():
             toplevel.destroy()
-        proposals.bind('<Return>', lambda event: open_selected())
-        proposals.bind('<Escape>', lambda event: cancel())
-        proposals.bind('<FocusOut>', lambda event: cancel())
-        def select_prev(event):
-            selection = proposals.curselection()
-            if selection:
-                active = int(selection[0])
-                if active - 1 >= 0:
-                    proposals.select_clear(0, END)
-                    proposals.selection_set(active - 1)
-                    proposals.see(active - 1)
-                    proposals.activate(active - 1)
-                    proposals.see(active - 1)
-        proposals.bind('<Control-p>', select_prev)
-        def select_next(event):
-            selection = proposals.curselection()
-            if selection:
-                active = int(selection[0])
-                if active + 1 < proposals.size():
-                    proposals.select_clear(0, END)
-                    proposals.selection_set(active + 1)
-                    proposals.see(active + 1)
-                    proposals.activate(active + 1)
-                    proposals.see(active + 1)
-        proposals.bind('<Control-n>', select_next)
-        label.grid(row=0, column=0, columnspan=2)
-        proposals.grid(row=1, column=0, sticky=N+E+W+S)
-        scrollbar.grid(row=1, column=1, sticky=N+E+W+S)
-        frame.grid(sticky=N+E+W+S)
-        proposals.focus_set()
+        def entry_to_string(proposal):
+            return proposal.kind[0].upper() + '  ' + proposal.name
+        enhanced_list = EnhancedList(toplevel, entry_to_string, open_selected, cancel, cancel)
+        for proposal in result.completions:
+            enhanced_list.add_entry(proposal)
+        if result:
+            enhanced_list.list.selection_set(0)
+        #        self.text.see('insert')
+        #        local_x, local_y, cx, cy = self.text.bbox("insert")
+        #        x = local_x + self.text.winfo_rootx() + 2
+        #        y = local_y + cy + self.text.winfo_rooty()
+        #        toplevel.wm_geometry('+%d+%d' % (x, y))
+        #        toplevel.wm_overrideredirect(1)
+        enhanced_list.list.focus_set()
         toplevel.grab_set()
 
     def get_text(self):

rope/uihelpers.py

+from Tkinter import *
+
+
+def do_nothing(*args, **kws):
+    pass
+
+class EnhancedList(object):
+
+    def __init__(self, parent, entry_to_string=str, selected=do_nothing,
+                 cancel=do_nothing, focus_out=do_nothing):
+        self.cancel = cancel
+        self.selected = selected
+        self.focus_out = focus_out
+        self.entry_to_string = entry_to_string
+        self.entries = []
+        self.frame = Frame(parent)
+        label = Label(self.frame, text='Code Assist Proposals')
+        self.list = Listbox(self.frame, selectmode=SINGLE)
+        scrollbar = Scrollbar(self.frame, orient=VERTICAL)
+        scrollbar['command'] = self.list.yview
+        self.list.config(yscrollcommand=scrollbar.set)
+        self.list.bind('<Return>', self._open_selected)
+        self.list.bind('<Escape>', self._cancel)
+        self.list.bind('<FocusOut>', self._focus_out)
+        self.list.bind('<Control-p>', self._select_prev)
+        self.list.bind('<Control-n>', self._select_next)
+        label.grid(row=0, column=0, columnspan=2)
+        self.list.grid(row=1, column=0, sticky=N+E+W+S)
+        scrollbar.grid(row=1, column=1, sticky=N+E+W+S)
+        self.frame.grid(sticky=N+E+W+S)
+        
+
+    def _focus_out(self, event):
+        self.focus_out()
+
+    def _open_selected(self, event):
+        selection = self.list.curselection()
+        if selection:
+            selected = int(selection[0])
+            self.selected(self.entries[selected])
+
+    def _cancel(self, event):
+        self.cancel()
+
+    def _select_prev(self, event):
+        selection = self.list.curselection()
+        if selection:
+            active = int(selection[0])
+            if active - 1 >= 0:
+                self.list.select_clear(0, END)
+                self.list.selection_set(active - 1)
+                self.list.see(active - 1)
+                self.list.activate(active - 1)
+                self.list.see(active - 1)
+
+    def _select_next(self, event):
+        selection = self.list.curselection()
+        if selection:
+            active = int(selection[0])
+            if active + 1 < self.list.size():
+                self.list.select_clear(0, END)
+                self.list.selection_set(active + 1)
+                self.list.see(active + 1)
+                self.list.activate(active + 1)
+                self.list.see(active + 1)
+
+    def add_entry(self, entry):
+        self.entries.append(entry)
+        self.list.insert(END, self.entry_to_string(entry))
+ 
+    def clear(self):
+        self.entries = []
+        self.list.delete(0, END)
+

ropetest/codeassisttest.py

         result = self.assist.assist(code, len(code))
         self.assert_completion_in_result('param1', 'local_variable', result)
 
+    def test_code_assist_with_function_with_two_line_return(self):
+        code = 'def f(param1, param2):\n    return(param1,\n           para'
+        result = self.assist.assist(code, len(code))
+        self.assert_completion_in_result('param2', 'local_variable', result)
+
 
 class CodeAssistInProjectsTest(unittest.TestCase):
 
         self.assert_completion_in_result('nestedmod', 'unknown', result)
 
 
-class TemplateHelperTest(unittest.TestCase):
+class TemplateTest(unittest.TestCase):
 
     def test_template_get_variables(self):
         template = Template('Name = ${name}')
     result = unittest.TestSuite()
     result.addTests(unittest.makeSuite(CodeAssistTest))
     result.addTests(unittest.makeSuite(CodeAssistInProjectsTest))
-    result.addTests(unittest.makeSuite(TemplateHelperTest))
+    result.addTests(unittest.makeSuite(TemplateTest))
     return result
 
 if __name__ == '__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.