Commits

Anonymous committed efdaa40

New prefix meaning for code-assist commands

Prefixes to rope-code-assist specify how to insert common prefix and
prefixes to rope-lucky-assist specify which proposal to insert.

Comments (0)

Files changed (2)

     (require 'pymacs)
     (pymacs-load "ropemacs" "rope-")
     (rope-init)
-    (setq rope-confirm-saving 'nil)
   )
 
 And execute ``load-ropemacs`` whenever you want to use ropemacs.  Also
 -----------
 
 ``rope-code-assist`` (``M-/`` by default) function will let you select
-from a list of completions.  By giving this function an argument, you
-can select which proposal to use without prompting; ``C-u 1 M-/`` uses
-the second, for instance.
+from a list of completions.  If prefixed (``C-u M-/``), ropemacs
+inserts the common prefix, automatically.  If a numeric argument is
+given, rope will insert the common prefix for that many of the first
+proposals.
 
-The ``rope-lucky-assist`` (``M-?``) is exactly like ``C-u 0 M-/``.  It
-applies the first completion without asking.
+The ``rope-lucky-assist`` (``M-?``) does not ask, anything; instead,
+it inserts the first proposal.  By prefixing it, you can select which
+proposal to use.  ``C-u 1 M-?`` uses the second propsal, for instance.
+
+Here::
+
+  xxaa = None
+  xxab = None
+  xxba = None
+  xxbb = None
+
+  x^
+
+consider cursor is at ``^`` position.  This table shows what happens
+when code-assist commands are used:
+
+============  ==========  =======================
+Key           Inserts     Minibuffer Completions
+============  ==========  =======================
+M-/                       xxaa, xxab, xxba, xxbb
+C-u M-/       x           xxaa, xxab, xxba, xxbb
+C-u 2 M-/     xa          xxaa, xxab
+M-?           xxaa
+C-u 1 M-/     xxab
+C-u 3 M-/     xxbb
+============  ==========  =======================
 
 
 Dialog batchset command
 C-c r m         rope-extract-method
 C-c r i         rope-inline
 C-c r v         rope-move
+C-c r x         rope-restructure
 C-c r 1 r       rope-rename-current-module
 C-c r 1 v       rope-move-current-module
 C-c r 1 p       rope-module-to-package

ropemacs/__init__.py

 
     @rawprefixed
     def code_assist(self, prefix):
-        arg = None
+        starting_offset, names = self._calculate_proposals()
         if prefix is not None:
             arg = lisp.prefix_numeric_value(prefix)
-        self._code_assist(arg)
+            if arg == 0:
+                arg = len(names)
+            common_start = self._calculate_prefix(names[:arg])
+            lisp.insert(common_start[self._get_offset() - starting_offset:])
+        source = lisp.buffer_string()
+        offset = self._get_offset()
+        starting = source[starting_offset:offset]
+        prompt = 'Completion for %s: ' % starting
+        result = lisputils.ask_values(prompt, names,
+                                      starting=starting, exact=None)
+        lisp.delete_region(starting_offset + 1, offset + 1)
+        lisp.insert(result)
 
-    @interactive
-    def lucky_assist(self):
-        self.code_assist(0)
+    @rawprefixed
+    def lucky_assist(self, prefix):
+        starting_offset, names = self._calculate_proposals()
+        source = lisp.buffer_string()
+        offset = self._get_offset()
+        starting = source[starting_offset:offset]
+        selected = 0
+        if prefix is not None:
+            selected = lisp.prefix_numeric_value(prefix)
+        if 0 <= selected < len(names):
+            result = names[selected]
+        else:
+            lisputils.message('Not enough proposals!')
+            pass
+        lisp.delete_region(starting_offset + 1, offset + 1)
+        lisp.insert(result)
 
-    def _code_assist(self, index):
+    def _calculate_proposals(self):
         self._check_project()
         resource, offset = self._get_location()
         source = lisp.buffer_string()
         proposals = codeassist.sorted_proposals(proposals)
         starting_offset = codeassist.starting_offset(source, offset)
         names = [proposal.name for proposal in proposals]
-        starting = source[starting_offset:offset]
-        if index is not None:
-            if 0 <= index < len(names):
-                result = names[index]
-            else:
-                lisputils.message('Not enough proposals!')
-                return
-        else:
-            prompt = 'Completion for %s: ' % starting
-            result = lisputils.ask_values(prompt, names,
-                                          starting=starting, exact=None)
-        lisp.delete_region(starting_offset + 1, offset + 1)
-        lisp.insert(result)
+        return starting_offset, names
+
+    def _calculate_prefix(self, names):
+        if not names:
+            return ''
+        prefix = names[0]
+        for name in names:
+            common = 0
+            for c1, c2 in zip(prefix, name):
+                if c1 == c2:
+                    common += 1
+                else:
+                    break
+            prefix = prefix[:common]
+        return prefix
 
     @interactive
     def find_file(self):
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.