Anonymous avatar Anonymous committed d33ebde

Added rope-create-(module|package|file|directory)

Comments (0)

Files changed (7)

 C-x p u         rope-undo-refactoring
 C-x p r         rope-redo-refactoring
 C-x p c         rope-project-config
+C-x p n [mpfd]  rope-create-(module|package|file|directory)
 
 C-c r r         rope-rename
 C-c r l         rope-extract-variable
 C-c i o         rope-organize-imports
 C-c f           rope-find-occurrences
 M-?             rope-lucky-assist
-
-C-c n v         rope-generate-variable
-C-c n f         rope-generate-function
-C-c n c         rope-generate-class
-C-c n m         rope-generate-module
-C-c n p         rope-generate-package
+C-c n [vfcmp]   rope-generate-(variable|function|class|module|package)
 ==============  ============================
 
 
  Done
 ======
 
+- new file/directory/module/package; ``C-x p n [fdmp]`` : November 30, 2007
 - lucky-assist; ``M-?`` : November 29, 2007
 - find occurrences; ``C-c f`` : November 29, 2007
 - edit project config; ``C-x p c`` : November 28, 2007
 
 > Public Release 0.3
 
-* new file/directory/module/package; C-x p n [fdmp]
 * showing proposal type in code-assist

ropemacs/__init__.py

 from rope.base import project, libutils
 from rope.contrib import codeassist
 
-from ropemacs import refactor, lisputils
+from ropemacs import refactor, lisputils, dialog
 from ropemacs.lisputils import lispfunction, interactive, prefixed, rawprefixed
 
 
             ('C-x p u', lisp.rope_undo_refactoring),
             ('C-x p r', lisp.rope_redo_refactoring),
             ('C-x p f', lisp.rope_find_file),
-            ('C-x p c', lisp.rope_project_config)]
+            ('C-x p c', lisp.rope_project_config),
+            ('C-x p n m', lisp.rope_create_module),
+            ('C-x p n p', lisp.rope_create_package),
+            ('C-x p n f', lisp.rope_create_file),
+            ('C-x p n d', lisp.rope_create_directory)]
 
         self.local_keys = [
             ('M-/', lisp.rope_code_assist),
                         refactoring(self).show()
                     setattr(self, attr.name, do_refactor)
                     name = 'rope-' + attr.name.replace('_', '-')
-                    self.local_keys.append((attr.key, lisp[name]))
+                    if attr.key.startswith('C-x'):
+                        self.global_keys.append((attr.key, lisp[name]))
+                    else:
+                        self.local_keys.append((attr.key, lisp[name]))
 
     def _key_sequence(self, sequence):
         result = []
 
     @interactive
     def open_project(self):
-        root = lisp.read_directory_name('Rope project root folder: ')
+        root = lisputils.ask_directory('Rope project root folder: ')
         if self.project is not None:
             self.close_project()
         self.project = project.Project(root)
         else:
             lisputils.message('No rope project folder found')
 
+
+    @interactive
+    def create_module(self):
+        def callback(sourcefolder, name):
+            return self.project.pycore.create_module(sourcefolder, name)
+        self._create('module', callback)
+
+    @interactive
+    def create_package(self):
+        def callback(sourcefolder, name):
+            folder = self.project.pycore.create_package(sourcefolder, name)
+            return folder.get_child('__init__.py')
+        self._create('package', callback)
+
+    @interactive
+    def create_file(self):
+        def callback(parent, name):
+            return parent.create_file(name)
+        self._create('file', callback, 'parent')
+
+    @interactive
+    def create_directory(self):
+        def callback(parent, name):
+            parent.create_folder(name)
+        self._create('directory', callback, 'parent')
+
+    def _create(self, name, callback, parentname='source'):
+        self._check_project()
+        confs = {'name': dialog.Data(name.title() + ' name: ')}
+        parentname = parentname + 'folder'
+        optionals = {parentname: dialog.Data(
+                parentname.title() + ' Folder: ',
+                default=self.project.address, kind='directory')}
+        action, values = dialog.show_dialog(
+            lisputils.askdata, ['perform', 'cancel'], confs, optionals)
+        if action == 'perform':
+            parent = libutils.path_to_resource(
+                self.project, values.get(parentname, self.project.address))
+            resource = callback(parent, values['name'])
+            if resource:
+                lisp.find_file(resource.real_path)
+
     def _goto_location(self, location, readonly=False):
         if location[0]:
             resource = location[0]

ropemacs/dialog.py

 class Data(object):
 
     def __init__(self, prompt=None, default=None,
-                 values=None, starting=None):
+                 values=None, starting=None, kind=None):
         self.prompt = prompt
         self.default = default
         self.values = values
         self.starting = starting
+        self.kind = kind
 
 
 def show_dialog(askdata, actions, confs={}, optionals={}):

ropemacs/lisputils.py

 
 def askdata(data):
     """`data` is a `ropemacs.dialog.Data` object"""
+    ask_func = ask
+    ask_args = {'prompt': data.prompt, 'starting': data.starting,
+                'default': data.default}
     if data.values:
-        return ask_values(data.prompt, data.values, default=data.default,
-                          starting=data.starting)
-    else:
-        return ask(data.prompt, default=data.default, starting=data.starting)
+        ask_func = ask_values
+        ask_args['values'] = data.values
+    elif data.kind == 'directory':
+        ask_func = ask_directory
+    return ask_func(**ask_args)
 
 
 def ask_values(prompt, values, default=None, starting=None, exact=True):
 def ask(prompt, default=None, starting=None):
     if default is not None:
         prompt = prompt + ('[%s] ' % default)
-    result =  lisp.read_from_minibuffer(prompt, starting, None, None,
+    result = lisp.read_from_minibuffer(prompt, starting, None, None,
                                         None, default, None)
     if result == '' and default is not None:
         return default
     return result
 
+def ask_directory(prompt, default=None, starting=None):
+    if default is not None:
+        prompt = prompt + ('[%s] ' % default)
+    result = lisp.read_directory_name(prompt, starting, default)
+    if result == '' and default is not None:
+        return default
+    return result
+
 
 def lispfunction(func):
     func.lisp = None

ropemacs/refactor.py

     confs = {'pattern': dialog.Data('Restructuring pattern: '),
              'goal': dialog.Data('Restructuring goal: ')}
     optionals = {'checks': dialog.Data('Checks: '),
-                     'imports': dialog.Data('Imports: ')}
+                 'imports': dialog.Data('Imports: ')}
 
     def _calculate_changes(self, values, task_handle):
         restructuring = rope.refactor.restructure.Restructure(
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.