Mercurial > public > mercurial-scm > hg
comparison mercurial/revset.py @ 28892:0c135f37c6f8
parser: construct alias object by rule-set class
It was odd that the revsetalias did the whole parsing stuff in __init__().
Instead, this patch adds a factory function to the aliasrules class, and
makes the alias (= revsetalias) class a plain-old value object.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 29 Feb 2016 18:33:30 +0900 |
parents | 2e9f5453ab5a |
children | ee11167fe1da |
comparison
equal
deleted
inserted
replaced
28891:ac30adb260ea | 28892:0c135f37c6f8 |
---|---|
2254 """Parsing and expansion rule set of revset aliases""" | 2254 """Parsing and expansion rule set of revset aliases""" |
2255 _section = _('revset alias') | 2255 _section = _('revset alias') |
2256 _parse = staticmethod(_parsealias) | 2256 _parse = staticmethod(_parsealias) |
2257 _getlist = staticmethod(getlist) | 2257 _getlist = staticmethod(getlist) |
2258 | 2258 |
2259 class revsetalias(object): | |
2260 # whether own `error` information is already shown or not. | |
2261 # this avoids showing same warning multiple times at each `findaliases`. | |
2262 warned = False | |
2263 | |
2264 def __init__(self, name, value): | |
2265 '''Aliases like: | |
2266 | |
2267 h = heads(default) | |
2268 b($1) = ancestors($1) - ancestors(default) | |
2269 ''' | |
2270 r = _aliasrules._builddecl(name) | |
2271 self.name, self.tree, self.args, self.error = r | |
2272 if self.error: | |
2273 self.error = _('failed to parse the declaration of revset alias' | |
2274 ' "%s": %s') % (self.name, self.error) | |
2275 return | |
2276 | |
2277 try: | |
2278 self.replacement = _aliasrules._builddefn(value, self.args) | |
2279 except error.ParseError as inst: | |
2280 self.error = _('failed to parse the definition of revset alias' | |
2281 ' "%s": %s') % (self.name, | |
2282 parser.parseerrordetail(inst)) | |
2283 | |
2284 def _getalias(aliases, tree): | 2259 def _getalias(aliases, tree): |
2285 """If tree looks like an unexpanded alias, return it. Return None | 2260 """If tree looks like an unexpanded alias, return it. Return None |
2286 otherwise. | 2261 otherwise. |
2287 """ | 2262 """ |
2288 if isinstance(tree, tuple): | 2263 if isinstance(tree, tuple): |
2312 | 2287 |
2313 def _expandaliases(aliases, tree, expanding, cache): | 2288 def _expandaliases(aliases, tree, expanding, cache): |
2314 """Expand aliases in tree, recursively. | 2289 """Expand aliases in tree, recursively. |
2315 | 2290 |
2316 'aliases' is a dictionary mapping user defined aliases to | 2291 'aliases' is a dictionary mapping user defined aliases to |
2317 revsetalias objects. | 2292 alias objects. |
2318 """ | 2293 """ |
2319 if not isinstance(tree, tuple): | 2294 if not isinstance(tree, tuple): |
2320 # Do not expand raw strings | 2295 # Do not expand raw strings |
2321 return tree | 2296 return tree |
2322 alias = _getalias(aliases, tree) | 2297 alias = _getalias(aliases, tree) |
2345 return result | 2320 return result |
2346 | 2321 |
2347 def findaliases(ui, tree, showwarning=None): | 2322 def findaliases(ui, tree, showwarning=None): |
2348 aliases = {} | 2323 aliases = {} |
2349 for k, v in ui.configitems('revsetalias'): | 2324 for k, v in ui.configitems('revsetalias'): |
2350 alias = revsetalias(k, v) | 2325 alias = _aliasrules.build(k, v) |
2351 aliases[alias.name] = alias | 2326 aliases[alias.name] = alias |
2352 tree = _expandaliases(aliases, tree, [], {}) | 2327 tree = _expandaliases(aliases, tree, [], {}) |
2353 if showwarning: | 2328 if showwarning: |
2354 # warn about problematic (but not referred) aliases | 2329 # warn about problematic (but not referred) aliases |
2355 for name, alias in sorted(aliases.iteritems()): | 2330 for name, alias in sorted(aliases.iteritems()): |