comparison mercurial/help.py @ 45643:65cb924a1430

help: extract logic for listing commands and topics Differential Revision: https://phab.mercurial-scm.org/D9134
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 30 Sep 2020 00:33:53 -0700
parents 79f6f9fa18c1
children 65e2b64670b5
comparison
equal deleted inserted replaced
45642:2693659c2b34 45643:65cb924a1430
636 return b'\n'.join(lines) 636 return b'\n'.join(lines)
637 637
638 return re.sub(br'( *)%s' % re.escape(marker), sub, doc) 638 return re.sub(br'( *)%s' % re.escape(marker), sub, doc)
639 639
640 640
641 def _getcategorizedhelpcmds(ui, cmdtable, name, select=None):
642 # Category -> list of commands
643 cats = {}
644 # Command -> short description
645 h = {}
646 # Command -> string showing synonyms
647 syns = {}
648 for c, e in pycompat.iteritems(cmdtable):
649 fs = cmdutil.parsealiases(c)
650 f = fs[0]
651 syns[f] = fs
652 func = e[0]
653 if select and not select(f):
654 continue
655 doc = pycompat.getdoc(func)
656 if filtercmd(ui, f, func, name, doc):
657 continue
658 doc = gettext(doc)
659 if not doc:
660 doc = _(b"(no help text available)")
661 h[f] = doc.splitlines()[0].rstrip()
662
663 cat = getattr(func, 'helpcategory', None) or (
664 registrar.command.CATEGORY_NONE
665 )
666 cats.setdefault(cat, []).append(f)
667 return cats, h, syns
668
669
670 def _getcategorizedhelptopics(ui, topictable):
671 # Group commands by category.
672 topiccats = {}
673 syns = {}
674 for topic in topictable:
675 names, header, doc = topic[0:3]
676 if len(topic) > 3 and topic[3]:
677 category = topic[3]
678 else:
679 category = TOPIC_CATEGORY_NONE
680
681 topicname = names[0]
682 syns[topicname] = list(names)
683 if not filtertopic(ui, topicname):
684 topiccats.setdefault(category, []).append((topicname, header))
685 return topiccats, syns
686
687
641 addtopichook(b'config', inserttweakrc) 688 addtopichook(b'config', inserttweakrc)
642 689
643 690
644 def help_( 691 def help_(
645 ui, 692 ui,
758 ) 805 )
759 806
760 return rst 807 return rst
761 808
762 def helplist(select=None, **opts): 809 def helplist(select=None, **opts):
763 # Category -> list of commands 810 cats, h, syns = _getcategorizedhelpcmds(
764 cats = {} 811 ui, commands.table, name, select
765 # Command -> short description 812 )
766 h = {}
767 # Command -> string showing synonyms
768 syns = {}
769 for c, e in pycompat.iteritems(commands.table):
770 fs = cmdutil.parsealiases(c)
771 f = fs[0]
772 syns[f] = b', '.join(fs)
773 func = e[0]
774 if select and not select(f):
775 continue
776 doc = pycompat.getdoc(func)
777 if filtercmd(ui, f, func, name, doc):
778 continue
779 doc = gettext(doc)
780 if not doc:
781 doc = _(b"(no help text available)")
782 h[f] = doc.splitlines()[0].rstrip()
783
784 cat = getattr(func, 'helpcategory', None) or (
785 registrar.command.CATEGORY_NONE
786 )
787 cats.setdefault(cat, []).append(f)
788 813
789 rst = [] 814 rst = []
790 if not h: 815 if not h:
791 if not ui.quiet: 816 if not ui.quiet:
792 rst.append(_(b'no commands defined\n')) 817 rst.append(_(b'no commands defined\n'))
803 828
804 def appendcmds(cmds): 829 def appendcmds(cmds):
805 cmds = sorted(cmds) 830 cmds = sorted(cmds)
806 for c in cmds: 831 for c in cmds:
807 if ui.verbose: 832 if ui.verbose:
808 rst.append(b" :%s: %s\n" % (syns[c], h[c])) 833 rst.append(b" :%s: %s\n" % (b', '.join(syns[c]), h[c]))
809 else: 834 else:
810 rst.append(b' :%s: %s\n' % (c, h[c])) 835 rst.append(b' :%s: %s\n' % (c, h[c]))
811 836
812 if name in (b'shortlist', b'debug'): 837 if name in (b'shortlist', b'debug'):
813 # List without categories. 838 # List without categories.
842 if exts: 867 if exts:
843 rst.append(b'\n') 868 rst.append(b'\n')
844 rst.extend(exts) 869 rst.extend(exts)
845 870
846 rst.append(_(b"\nadditional help topics:\n")) 871 rst.append(_(b"\nadditional help topics:\n"))
847 # Group commands by category. 872 topiccats, topicsyns = _getcategorizedhelptopics(ui, helptable)
848 topiccats = {}
849 for topic in helptable:
850 names, header, doc = topic[0:3]
851 if len(topic) > 3 and topic[3]:
852 category = topic[3]
853 else:
854 category = TOPIC_CATEGORY_NONE
855
856 topicname = names[0]
857 if not filtertopic(ui, topicname):
858 topiccats.setdefault(category, []).append(
859 (topicname, header)
860 )
861 873
862 # Check that all categories have an order. 874 # Check that all categories have an order.
863 missing_order = set(topiccats.keys()) - set(TOPIC_CATEGORY_ORDER) 875 missing_order = set(topiccats.keys()) - set(TOPIC_CATEGORY_ORDER)
864 if missing_order: 876 if missing_order:
865 ui.develwarn( 877 ui.develwarn(