Anonymous avatar Anonymous committed d93df29

Proposing templates in code-assist proposals
Not asking for variables if there is none

Comments (0)

Files changed (4)

docs/workingon.txt

-*** Proposing templates in auto-completion proposals @ 1 ***
+*** Proposing templates in code-assist proposals @ 1 ***
 
 - Show the type of the completion in the codeassist dialog
+- Showing templates in completion dialog
+- main, test
+- Not asking for variables if there is none
 
-* Showing templates in completion dialog
-? Defaults for templates
-* Builtin templates
-
+? Builtin templates
 ?? Ignoring string contents while indenting
 ?? Next/prev word should stop consider underline and capitals as spaces
 ?? Better change editor

rope/codeassist.py

 
     def __init__(self, name, template):
         super(TemplateProposal, self).__init__(name)
+        self.kind = 'template'
         self.template = template
 
 
         import keyword
         self.keywords = keyword.kwlist
         self.templates = []
+        self.templates.extend(self._get_default_templates())
+
+    def _get_default_templates(self):
+        result = []
+        result.append(TemplateProposal('main', Template("if __name__ == '__main__':\n    ${cursor}\n")))
+        test_case_template = "import unittest\n\nclass ${class}Test(unittest.TestCase):\n\n" + \
+                             "    def setUp(self):\n        super(${class}Test, self).setUp()\n\n" + \
+                             "    def tearDown(self):\n        super(${class}Test, self).tearDown()\n\n" + \
+                             "    def test_${aspect1}(self):\n        pass${cursor}\n\n\n" + \
+                             "if __name__ == '__main__':\n    unittest.main()\n"
+        result.append(TemplateProposal('test_case', Template(test_case_template)))
+        return result
 
     def _find_starting_offset(self, source_code, offset):
         current_offset = offset - 1
         toplevel = Toplevel()
         toplevel.title('Code Assist Proposals')
         def open_selected(selected):
+            if selected.kind != 'template':
                 self.text.delete('0.0 +%dc' % result.start_offset,
                                  '0.0 +%dc' % result.end_offset)
                 self.text.insert('0.0 +%dc' % result.start_offset,
                                  selected.name)
-                toplevel.destroy()
+            else:
+                self._get_template_information(result, selected)
+            toplevel.destroy()
         def cancel():
             toplevel.destroy()
         def entry_to_string(proposal):
         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)
+        for proposal in result.templates:
+            enhanced_list.add_entry(proposal)
         #        self.text.see('insert')
         #        local_x, local_y, cx, cy = self.text.bbox("insert")
         #        x = local_x + self.text.winfo_rootx() + 2
         enhanced_list.list.focus_set()
         toplevel.grab_set()
 
+    def _get_template_information(self, result, proposal):
+        template = proposal.template
+        def apply_template(mapping):
+            string = template.substitute(mapping)
+            self.text.delete('0.0 +%dc' % result.start_offset,
+                             '0.0 +%dc' % result.end_offset)
+            self.text.insert('0.0 +%dc' % result.start_offset,
+                             string)
+            offset = template.get_cursor_location(mapping)
+            self.text.mark_set(INSERT, '0.0 +%dc' % (result.start_offset + offset))
+
+        if not template.variables():
+            apply_template({})
+            return
+        toplevel = Toplevel()
+        toplevel.title(proposal.name)
+        frame = Frame(toplevel)
+        label = Label(frame, text=('Variables in template %s' % proposal.name))
+        label.grid(row=0, column=0, columnspan=2)
+        entries = {}
+        def ok(event=None):
+            mapping = {}
+            for var, entry in entries.iteritems():
+                mapping[var] = entry.get()
+            apply_template(mapping)
+            toplevel.destroy()
+        def cancel(event=None):
+            toplevel.destroy()
+
+        for (index, var) in enumerate(template.variables()):
+            label = Label(frame, text=var, width=20)
+            label.grid(row=index+1, column=0)
+            entry = Entry(frame, width=25)
+            entry.insert(INSERT, var)
+            entry.grid(row=index+1, column=1)
+            entries[var] = entry
+        ok_button = Button(frame, text='Done', command=ok)
+        cancel_button = Button(frame, text='Cancel', command=cancel)
+        ok_button.grid(row=len(template.variables()) + 1, column=0)
+        cancel_button.grid(row=len(template.variables()) + 1, column=1)
+        frame.grid()
+
     def get_text(self):
         return self.text.get('1.0', 'end-1c')
 

rope/uihelpers.py

     def add_entry(self, entry):
         self.entries.append(entry)
         self.list.insert(END, self.entry_to_string(entry))
+        if len(self.entries) == 1:
+            self.list.selection_set(0)
  
     def clear(self):
         self.entries = []
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.