help: extract logic for listing commands and topics
authorLudovic Chabant <ludovic@chabant.com>
Wed, 30 Sep 2020 00:33:53 -0700
changeset 45641 65cb924a1430
parent 45640 2693659c2b34
child 45642 56281b75264a
help: extract logic for listing commands and topics Differential Revision: https://phab.mercurial-scm.org/D9134
mercurial/help.py
--- a/mercurial/help.py	Fri Sep 25 15:05:08 2020 +0200
+++ b/mercurial/help.py	Wed Sep 30 00:33:53 2020 -0700
@@ -638,6 +638,53 @@
     return re.sub(br'( *)%s' % re.escape(marker), sub, doc)
 
 
+def _getcategorizedhelpcmds(ui, cmdtable, name, select=None):
+    # Category -> list of commands
+    cats = {}
+    # Command -> short description
+    h = {}
+    # Command -> string showing synonyms
+    syns = {}
+    for c, e in pycompat.iteritems(cmdtable):
+        fs = cmdutil.parsealiases(c)
+        f = fs[0]
+        syns[f] = fs
+        func = e[0]
+        if select and not select(f):
+            continue
+        doc = pycompat.getdoc(func)
+        if filtercmd(ui, f, func, name, doc):
+            continue
+        doc = gettext(doc)
+        if not doc:
+            doc = _(b"(no help text available)")
+        h[f] = doc.splitlines()[0].rstrip()
+
+        cat = getattr(func, 'helpcategory', None) or (
+            registrar.command.CATEGORY_NONE
+        )
+        cats.setdefault(cat, []).append(f)
+    return cats, h, syns
+
+
+def _getcategorizedhelptopics(ui, topictable):
+    # Group commands by category.
+    topiccats = {}
+    syns = {}
+    for topic in topictable:
+        names, header, doc = topic[0:3]
+        if len(topic) > 3 and topic[3]:
+            category = topic[3]
+        else:
+            category = TOPIC_CATEGORY_NONE
+
+        topicname = names[0]
+        syns[topicname] = list(names)
+        if not filtertopic(ui, topicname):
+            topiccats.setdefault(category, []).append((topicname, header))
+    return topiccats, syns
+
+
 addtopichook(b'config', inserttweakrc)
 
 
@@ -760,31 +807,9 @@
         return rst
 
     def helplist(select=None, **opts):
-        # Category -> list of commands
-        cats = {}
-        # Command -> short description
-        h = {}
-        # Command -> string showing synonyms
-        syns = {}
-        for c, e in pycompat.iteritems(commands.table):
-            fs = cmdutil.parsealiases(c)
-            f = fs[0]
-            syns[f] = b', '.join(fs)
-            func = e[0]
-            if select and not select(f):
-                continue
-            doc = pycompat.getdoc(func)
-            if filtercmd(ui, f, func, name, doc):
-                continue
-            doc = gettext(doc)
-            if not doc:
-                doc = _(b"(no help text available)")
-            h[f] = doc.splitlines()[0].rstrip()
-
-            cat = getattr(func, 'helpcategory', None) or (
-                registrar.command.CATEGORY_NONE
-            )
-            cats.setdefault(cat, []).append(f)
+        cats, h, syns = _getcategorizedhelpcmds(
+            ui, commands.table, name, select
+        )
 
         rst = []
         if not h:
@@ -805,7 +830,7 @@
             cmds = sorted(cmds)
             for c in cmds:
                 if ui.verbose:
-                    rst.append(b" :%s: %s\n" % (syns[c], h[c]))
+                    rst.append(b" :%s: %s\n" % (b', '.join(syns[c]), h[c]))
                 else:
                     rst.append(b' :%s: %s\n' % (c, h[c]))
 
@@ -844,20 +869,7 @@
                 rst.extend(exts)
 
             rst.append(_(b"\nadditional help topics:\n"))
-            # Group commands by category.
-            topiccats = {}
-            for topic in helptable:
-                names, header, doc = topic[0:3]
-                if len(topic) > 3 and topic[3]:
-                    category = topic[3]
-                else:
-                    category = TOPIC_CATEGORY_NONE
-
-                topicname = names[0]
-                if not filtertopic(ui, topicname):
-                    topiccats.setdefault(category, []).append(
-                        (topicname, header)
-                    )
+            topiccats, topicsyns = _getcategorizedhelptopics(ui, helptable)
 
             # Check that all categories have an order.
             missing_order = set(topiccats.keys()) - set(TOPIC_CATEGORY_ORDER)