Anonymous avatar Anonymous committed 92f9696

Added ropemacs.lisputils

Comments (0)

Files changed (5)

   (pymacs-load "ropemacs" "rope-")
   (rope-init)
 
-If you want to only ropemacs only when you really need it, you can use
+If you want to load ropemacs only when you really need it, you can use
 a function like this instead of that::
 
   (defun load-ropemacs ()
                                ":/path/to/extracted/ropemacs/package"))
 
 
-
 Getting Started
 ===============
 
 returned back to the base prompt.  Finally, you can ask rope to
 perform, preview or cancel the refactoring.
 
-
 See keybinding_ section and try the refactorings yourself.
 
 
 project.
 
 
-
 Keybinding
-==========
+----------
 
 Uses almost the same keybinding as ropeide.
 
 ==============  ============================
 C-x p o         rope-open-project
 C-x p k         rope-close-project
+C-x p f         rope-find-file
 C-x p u         rope-undo-refactoring
 C-x p r         rope-redo-refactoring
-C-x p f         rope-find-file
 
 C-c r r         rope-rename
 C-c r l         rope-extract-variable
 ==============  ============================
 
 
-
 Variables
-=========
+---------
 
 * ``rope-confirm-saving``: If non-nil, you have to confirm saving all
   modified python files before refactorings; otherwise they are saved
   automatically. Defaults to ``t``.
 
 
-
 Contributing
 ============
 
 .. _`rope-dev (at) googlegroups.com`: http://groups.google.com/group/rope-dev
 
 
-
 License
 =======
 

ropemacs/__init__.py

-import threading
-
 from Pymacs import lisp
-from rope.base import project, libutils, taskhandle, exceptions
+from rope.base import project, libutils
 from rope.contrib import codeassist
 
-from ropemacs import refactor
-
-
-def interactive(func):
-    func.interaction = ''
-    return func
-
-def lispfunction(func):
-    func.lisp = None
-    return func
+from ropemacs import refactor, lisputils
+from ropemacs.lisputils import lispfunction, interactive
 
 
 class RopeInterface(object):
         resource, offset = self._get_location()
         docs = codeassist.get_doc(
             self.project, lisp.buffer_string(), offset, resource)
-        _make_buffer('*rope-pydoc*', docs, empty_goto=False)
+        lisputils.make_buffer('*rope-pydoc*', docs, empty_goto=False)
 
     @interactive
     def code_assist(self):
         names = [proposal.name for proposal in proposals]
         starting = source[starting_offset:offset]
         prompt = 'Completion for %s: ' % starting
-        result = _ask_values(prompt, names, starting=starting, exact=None)
+        result = lisputils.ask_values(prompt, names,
+                                       starting=starting, exact=None)
         lisp.delete_region(starting_offset + 1, offset + 1)
         lisp.insert(result)
 
         for file in files:
             names.append('<'.join(reversed(file.path.split('/'))))
         source = lisp.buffer_string()
-        result = _ask_values('Rope Find File: ', names, exact=True)
+        result = lisputils.ask_values('Rope Find File: ', names, exact=True)
         path = '/'.join(reversed(result.split('<')))
         file = self.project.get_file(path)
         lisp.find_file(file.real_path)
             globals()[attrname] = attr
 
 
-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,
-                                        None, default, None)
-    if result == '' and default is not None:
-        return default
-    return result
-
-def _ask_values(prompt, values, default=None, starting=None, exact=True):
-    if exact and default is not None:
-        prompt = prompt + ('[%s] ' % default)
-    result = lisp.completing_read(prompt, values, None, exact, starting)
-    if result == '' and exact:
-        return default
-    return result
-
-def _lisp_askdata(data):
-    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)
-
-def _message(message):
-    lisp.message(message)
-
-
-class _RunTask(object):
-
-    def __init__(self, task, name, interrupts=True):
-        self.task = task
-        self.name = name
-        self.interrupts = interrupts
-
-    def __call__(self):
-        handle = taskhandle.TaskHandle(name=self.name)
-        progress = lisp.make_progress_reporter(
-            '%s ... ' % self.name, 0, 100)
-        def update_progress():
-            jobset = handle.current_jobset()
-            if jobset:
-                percent = jobset.get_percent_done()
-                if percent is not None:
-                    lisp.progress_reporter_update(progress, percent)
-        handle.add_observer(update_progress)
-        class Calculate(object):
-
-            def __init__(self, task):
-                self.task = task
-                self.result = None
-                self.exception = None
-
-            def __call__(self):
-                try:
-                    self.result = self.task(handle)
-                except Exception, e:
-                    self.exception = e
-
-        calculate = Calculate(self.task)
-        thread = threading.Thread(target=calculate)
-        try:
-            thread.start()
-            thread.join()
-            lisp.progress_reporter_done(progress)
-            if calculate.exception is not None:
-                description = type(calculate.exception).__name__ + ': ' + \
-                                   str(calculate.exception)
-                raise exceptions.InterruptedTaskError(
-                    'Task <%s> was interrupted.\nReason: <%s>' %
-                    (self.name, description))
-        except:
-            handle.stop()
-            _message('%s interrupted!' % self.name)
-            raise
-        return calculate.result
-
-
-def _make_buffer(name, contents, empty_goto=True, mode=None):
-    new_buffer = lisp.get_buffer_create(name)
-    lisp.set_buffer(new_buffer)
-    lisp.erase_buffer()
-    if contents or empty_goto:
-        lisp.insert(contents)
-        if mode is not None:
-            lisp[mode + '-mode']()
-        lisp.display_buffer(new_buffer)
-        lisp.goto_line(1)
-
-def _yes_or_no(prompt):
-    return lisp.yes_or_no_p(prompt)
-
-
 DEFVARS = """\
 (defvar rope-confirm-saving t
   "If non-nil, you have to confirm saving all modified

ropemacs/lisputils.py

+import threading
+
+from Pymacs import lisp
+from rope.base import taskhandle, exceptions
+
+
+def yes_or_no(prompt):
+    return lisp.yes_or_no_p(prompt)
+
+
+def make_buffer(name, contents, empty_goto=True, mode=None):
+    new_buffer = lisp.get_buffer_create(name)
+    lisp.set_buffer(new_buffer)
+    lisp.erase_buffer()
+    if contents or empty_goto:
+        lisp.insert(contents)
+        if mode is not None:
+            lisp[mode + '-mode']()
+        lisp.display_buffer(new_buffer)
+        lisp.goto_line(1)
+
+
+class RunTask(object):
+
+    def __init__(self, task, name, interrupts=True):
+        self.task = task
+        self.name = name
+        self.interrupts = interrupts
+
+    def __call__(self):
+        handle = taskhandle.TaskHandle(name=self.name)
+        progress = lisp.make_progress_reporter(
+            '%s ... ' % self.name, 0, 100)
+        def update_progress():
+            jobset = handle.current_jobset()
+            if jobset:
+                percent = jobset.get_percent_done()
+                if percent is not None:
+                    lisp.progress_reporter_update(progress, percent)
+        handle.add_observer(update_progress)
+        class Calculate(object):
+
+            def __init__(self, task):
+                self.task = task
+                self.result = None
+                self.exception = None
+
+            def __call__(self):
+                try:
+                    self.result = self.task(handle)
+                except Exception, e:
+                    self.exception = e
+
+        calculate = Calculate(self.task)
+        thread = threading.Thread(target=calculate)
+        try:
+            thread.start()
+            thread.join()
+            lisp.progress_reporter_done(progress)
+            if calculate.exception is not None:
+                description = type(calculate.exception).__name__ + ': ' + \
+                                   str(calculate.exception)
+                raise exceptions.InterruptedTaskError(
+                    'Task <%s> was interrupted.\nReason: <%s>' %
+                    (self.name, description))
+        except:
+            handle.stop()
+            message('%s interrupted!' % self.name)
+            raise
+        return calculate.result
+
+
+def message(message):
+    lisp.message(message)
+
+
+def askdata(data):
+    """`data` is a `ropemacs.dialog.Data` object"""
+    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)
+
+
+def ask_values(prompt, values, default=None, starting=None, exact=True):
+    if exact and default is not None:
+        prompt = prompt + ('[%s] ' % default)
+    result = lisp.completing_read(prompt, values, None, exact, starting)
+    if result == '' and exact:
+        return default
+    return result
+
+
+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,
+                                        None, default, None)
+    if result == '' and default is not None:
+        return default
+    return result
+
+
+def lispfunction(func):
+    func.lisp = None
+    return func
+
+
+def interactive(func):
+    func.interaction = ''
+    return func

ropemacs/refactor.py

 import rope.refactor.rename
 import rope.refactor.restructure
 
-import ropemacs
-from ropemacs import dialog
+from ropemacs import dialog, lisputils
 
 
 class Refactoring(object):
         self.interface._save_buffers(only_current=not self.saveall)
         self._create_refactoring()
         action, result = dialog.show_dialog(
-            ropemacs._lisp_askdata, ['perform', 'preview', 'cancel'],
+            lisputils.askdata, ['perform', 'preview', 'cancel'],
             self._get_confs(), self._get_optionals())
         if action == 'cancel':
-            ropemacs._message('Cancelled!')
+            lisputils.message('Cancelled!')
             return
         def calculate(handle):
             return self._calculate_changes(result, handle)
         name = 'Calculating %s changes' % self.name
-        changes = ropemacs._RunTask(calculate, name=name)()
+        changes = lisputils.RunTask(calculate, name=name)()
         if action == 'perform':
             self._perform(changes)
         if action == 'preview':
-            ropemacs._make_buffer('*rope-preview*',
-                                  str(changes.get_description()),
-                                  mode='diff')
-            if ropemacs._yes_or_no('Do the changes? '):
+            lisputils.make_buffer('*rope-preview*',
+                                   str(changes.get_description()),
+                                   mode='diff')
+            if lisputils.yes_or_no('Do the changes? '):
                 self._perform(changes)
 
     @property
             self.project.do(changes, task_handle=handle)
             self.interface._reload_buffers(changes.get_changed_resources())
             self._done()
-        ropemacs._RunTask(perform, 'Making %s changes' % self.name,
-                          interrupts=False)()
-        ropemacs._message(str(changes.description) + ' finished')
+        lisputils.RunTask(perform, 'Making %s changes' % self.name,
+                           interrupts=False)()
+        lisputils.message(str(changes.description) + ' finished')
 
     def _get_confs(self):
         return self.confs
     'License :: OSI Approved :: GNU General Public License (GPL)',
     'Natural Language :: English',
     'Programming Language :: Python',
+    'Topic :: Text Editors :: Emacs',
     'Topic :: Software Development']
 
 def get_long_description():
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.