Patchwork D9344: errors: morph reportsimilar() into similarity_hint()

login
register
mail settings
Submitter phabricator
Date Nov. 21, 2020, 12:26 a.m.
Message ID <differential-rev-PHID-DREV-uklk6l3epgovxwfglo7a-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47622/
State Superseded
Headers show

Comments

phabricator - Nov. 21, 2020, 12:26 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The next step is to eagerly create the hint in `UnknownIdentifier`'s
  constructor.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9344

AFFECTED FILES
  mercurial/dispatch.py
  mercurial/scmutil.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -150,12 +150,14 @@ 
     return [s for s in symbols if sim(s) > 0.6]
 
 
-def reportsimilar(write, similar):
+def similarity_hint(similar):
     if len(similar) == 1:
-        write(_(b"(did you mean %s?)\n") % similar[0])
+        return _(b"did you mean %s?") % similar[0]
     elif similar:
         ss = b", ".join(sorted(similar))
-        write(_(b"(did you mean one of %s?)\n") % ss)
+        return _(b"did you mean one of %s?") % ss
+    else:
+        return None
 
 
 def formatparse(write, inst):
@@ -169,7 +171,9 @@ 
     if isinstance(inst, error.UnknownIdentifier):
         # make sure to check fileset first, as revset can invoke fileset
         similar = getsimilar(inst.symbols, inst.function)
-        reportsimilar(write, similar)
+        hint = similarity_hint(similar)
+        if hint:
+            write(b"(%s)\n" % hint)
     elif inst.hint:
         write(_(b"(%s)\n") % inst.hint)
 
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -487,7 +487,7 @@ 
                 sim = scmutil.getsimilar(inst.all_commands, inst.command)
                 if sim:
                     ui.warn(nocmdmsg)
-                    scmutil.reportsimilar(ui.warn, sim)
+                    ui.warn(b"(%s)\n" % scmutil.similarity_hint(sim))
                     suggested = True
             if not suggested:
                 ui.warn(nocmdmsg)