##// END OF EJS Templates
errors: move similarity_hint() to error module...
Martin von Zweigbergk -
r46494:8cc9e7f7 default
parent child Browse files
Show More
@@ -484,10 +484,10 b' def _callcatch(ui, func):'
484 except (error.UnknownCommand, error.Abort):
484 except (error.UnknownCommand, error.Abort):
485 suggested = False
485 suggested = False
486 if inst.all_commands:
486 if inst.all_commands:
487 sim = scmutil.getsimilar(inst.all_commands, inst.command)
487 sim = error.getsimilar(inst.all_commands, inst.command)
488 if sim:
488 if sim:
489 ui.warn(nocmdmsg)
489 ui.warn(nocmdmsg)
490 ui.warn(b"(%s)\n" % scmutil.similarity_hint(sim))
490 ui.warn(b"(%s)\n" % error.similarity_hint(sim))
491 suggested = True
491 suggested = True
492 if not suggested:
492 if not suggested:
493 ui.warn(nocmdmsg)
493 ui.warn(nocmdmsg)
@@ -13,6 +13,8 b' imports.'
13
13
14 from __future__ import absolute_import
14 from __future__ import absolute_import
15
15
16 import difflib
17
16 # Do not import anything but pycompat here, please
18 # Do not import anything but pycompat here, please
17 from . import pycompat
19 from . import pycompat
18
20
@@ -270,6 +272,25 b' class PatchError(Exception):'
270 __bytes__ = _tobytes
272 __bytes__ = _tobytes
271
273
272
274
275 def getsimilar(symbols, value):
276 sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio()
277 # The cutoff for similarity here is pretty arbitrary. It should
278 # probably be investigated and tweaked.
279 return [s for s in symbols if sim(s) > 0.6]
280
281
282 def similarity_hint(similar):
283 from .i18n import _
284
285 if len(similar) == 1:
286 return _(b"did you mean %s?") % similar[0]
287 elif similar:
288 ss = b", ".join(sorted(similar))
289 return _(b"did you mean one of %s?") % ss
290 else:
291 return None
292
293
273 class UnknownIdentifier(ParseError):
294 class UnknownIdentifier(ParseError):
274 """Exception raised when a {rev,file}set references an unknown identifier"""
295 """Exception raised when a {rev,file}set references an unknown identifier"""
275
296
@@ -7,7 +7,6 b''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import difflib
11 import errno
10 import errno
12 import glob
11 import glob
13 import os
12 import os
@@ -143,23 +142,6 b' def nochangesfound(ui, repo, excluded=No'
143 ui.status(_(b"no changes found\n"))
142 ui.status(_(b"no changes found\n"))
144
143
145
144
146 def getsimilar(symbols, value):
147 sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio()
148 # The cutoff for similarity here is pretty arbitrary. It should
149 # probably be investigated and tweaked.
150 return [s for s in symbols if sim(s) > 0.6]
151
152
153 def similarity_hint(similar):
154 if len(similar) == 1:
155 return _(b"did you mean %s?") % similar[0]
156 elif similar:
157 ss = b", ".join(sorted(similar))
158 return _(b"did you mean one of %s?") % ss
159 else:
160 return None
161
162
163 def formatparse(write, inst):
145 def formatparse(write, inst):
164 if inst.location is not None:
146 if inst.location is not None:
165 write(
147 write(
@@ -170,8 +152,8 b' def formatparse(write, inst):'
170 write(_(b"hg: parse error: %s\n") % inst.message)
152 write(_(b"hg: parse error: %s\n") % inst.message)
171 if isinstance(inst, error.UnknownIdentifier):
153 if isinstance(inst, error.UnknownIdentifier):
172 # make sure to check fileset first, as revset can invoke fileset
154 # make sure to check fileset first, as revset can invoke fileset
173 similar = getsimilar(inst.symbols, inst.function)
155 similar = error.getsimilar(inst.symbols, inst.function)
174 hint = similarity_hint(similar)
156 hint = error.similarity_hint(similar)
175 if hint:
157 if hint:
176 write(b"(%s)\n" % hint)
158 write(b"(%s)\n" % hint)
177 elif inst.hint:
159 elif inst.hint:
General Comments 0
You need to be logged in to leave comments. Login now