changeset 45881:bb1b7a5bc96b

errors: morph reportsimilar() into similarity_hint() The next step is to eagerly create the hint in `UnknownIdentifier`'s constructor. Differential Revision: https://phab.mercurial-scm.org/D9344
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 19 Nov 2020 09:19:44 -0800
parents 7a273336d3d3
children 8cc9e7f762d6
files mercurial/dispatch.py mercurial/scmutil.py
diffstat 2 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Thu Nov 19 10:29:06 2020 -0800
+++ b/mercurial/dispatch.py	Thu Nov 19 09:19:44 2020 -0800
@@ -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)
--- a/mercurial/scmutil.py	Thu Nov 19 10:29:06 2020 -0800
+++ b/mercurial/scmutil.py	Thu Nov 19 09:19:44 2020 -0800
@@ -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)