new command debugcomplete
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Sun, 12 Mar 2006 11:32:03 +0100
changeset 1887 913397c27cd8
parent 1886 d4a3a8a332ab
child 1888 283d2ab1e020
new command debugcomplete add a new command debugcomplete, it lists all the possible completion for the specified command. make the bash_completion script uses it instead of the awk code
contrib/bash_completion
mercurial/commands.py
--- a/contrib/bash_completion	Sun Mar 12 08:08:22 2006 +0100
+++ b/contrib/bash_completion	Sun Mar 12 11:32:03 2006 +0100
@@ -1,30 +1,5 @@
 shopt -s extglob
 
-_hg_command_list()
-{
-    "$hg" --debug help 2>/dev/null | \
-	awk -F', ' '/^list of commands:/ {commands=1}
-	    commands==1 && /^ [^ ]/ {
-		line = substr($0, 2)
-		colon = index(line, ":")
-		if (colon > 0)
-		    line = substr(line, 1, colon-1)
-		n = split(line, aliases)
-		command = aliases[1]
-		if (index(command, "debug") == 1) {
-		    for (i=1; i<=n; i++)
-			debug[j++] = aliases[i]
-		    next
-		}
-		print command
-		for (i=2; i<=n; i++)
-		    if (index(command, aliases[i]) != 1)
-			print aliases[i]
-	    }
-	    /^global options:/ {exit 0}
-	    END {for (i in debug) print debug[i]}'
-}
-
 _hg_option_list()
 {
     "$hg" -v help $1 2>/dev/null | \
@@ -40,21 +15,8 @@
 
 _hg_commands()
 {
-    local all commands result
-
-    all=$(_hg_command_list)
-    commands=${all%%$'\n'debug*}
-    result=$(compgen -W '$commands' -- "$cur")
-
-    # hide debug commands from users, but complete them if
-    # there is no other possible command
-    if [ "$result" = "" ]; then
-	local debug
-	debug=debug${all#*$'\n'debug}
-	result=$(compgen -W '$debug' -- "$cur")
-    fi
-
-    COMPREPLY=(${COMPREPLY[@]:-} $result)
+    local commands="$("$hg" debugcomplete "$cur")"
+    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
 }
 
 _hg_paths()
--- a/mercurial/commands.py	Sun Mar 12 08:08:22 2006 +0100
+++ b/mercurial/commands.py	Sun Mar 12 11:32:03 2006 +0100
@@ -1013,6 +1013,12 @@
     a = r.ancestor(r.lookup(rev1), r.lookup(rev2))
     ui.write("%d:%s\n" % (r.rev(a), hex(a)))
 
+def debugcomplete(ui, cmd):
+    """returns the completion list associated with the given command"""
+    clist = findpossible(cmd).keys()
+    clist.sort()
+    ui.write("%s\n" % " ".join(clist))
+
 def debugrebuildstate(ui, repo, rev=None):
     """rebuild the dirstate as it would look like for the given revision"""
     if not rev:
@@ -2427,6 +2433,7 @@
           ('X', 'exclude', [], _('exclude names matching the given patterns'))],
          _('hg copy [OPTION]... [SOURCE]... DEST')),
     "debugancestor": (debugancestor, [], _('debugancestor INDEX REV1 REV2')),
+    "debugcomplete": (debugcomplete, [], _('debugcomplete CMD')),
     "debugrebuildstate":
         (debugrebuildstate,
          [('r', 'rev', '', _('revision to rebuild to'))],
@@ -2658,42 +2665,49 @@
     ('h', 'help', None, _('display help and exit')),
 ]
 
-norepo = ("clone init version help debugancestor debugdata"
+norepo = ("clone init version help debugancestor debugcomplete debugdata"
           " debugindex debugindexdot")
 optionalrepo = ("paths debugconfig")
 
-def find(cmd):
-    """Return (aliases, command table entry) for command string."""
-    choice = []
-    debugchoice = []
+def findpossible(cmd):
+    """
+    Return cmd -> (aliases, command table entry)
+    for each matching command
+    """
+    choice = {}
+    debugchoice = {}
     for e in table.keys():
         aliases = e.lstrip("^").split("|")
         if cmd in aliases:
-            return aliases, table[e]
+            choice[cmd] = (aliases, table[e])
+            continue
         for a in aliases:
             if a.startswith(cmd):
                 if aliases[0].startswith("debug"):
-                    debugchoice.append([aliases, table[e]])
+                    debugchoice[a] = (aliases, table[e])
                 else:
-                    choice.append([aliases, table[e]])
+                    choice[a] = (aliases, table[e])
                 break
 
     if not choice and debugchoice:
         choice = debugchoice
 
+    return choice
+
+def find(cmd):
+    """Return (aliases, command table entry) for command string."""
+    choice = findpossible(cmd)
+
+    if choice.has_key(cmd):
+        return choice[cmd]
+
     if len(choice) > 1:
-        clist = []
-        for aliases, table_e in choice:
-            if aliases[0].startswith(cmd):
-                clist.append(aliases[0])
-            for a in aliases[1:]:
-                if a.startswith(cmd) and not aliases[0].startswith(a):
-                    clist.append(a)
+        clist = choice.keys()
         clist.sort()
         raise AmbiguousCommand(cmd, clist)
 
     if choice:
-        return choice[0]
+        return choice.values()[0]
 
     raise UnknownCommand(cmd)