comparison mercurial/commands.py @ 29854:b842b1adfea2

grep: refactor loop that yields matched text with label As preparation for formatter support, this and the next patch split linestate.__iter__() into two functions, line scanner and displayer. New code uses regexp.search(str, pos) in place of regexp.search(substr), which appears to fix a bug of highlighting.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 18 Aug 2016 14:03:25 +0900
parents f148bfa40489
children fac24eab65a4
comparison
equal deleted inserted replaced
29849:f148bfa40489 29854:b842b1adfea2
4343 return hash((self.linenum, self.line)) 4343 return hash((self.linenum, self.line))
4344 4344
4345 def __eq__(self, other): 4345 def __eq__(self, other):
4346 return self.line == other.line 4346 return self.line == other.line
4347 4347
4348 def findpos(self):
4349 """Iterate all (start, end) indices of matches"""
4350 yield self.colstart, self.colend
4351 p = self.colend
4352 while p < len(self.line):
4353 m = regexp.search(self.line, p)
4354 if not m:
4355 break
4356 yield m.span()
4357 p = m.end()
4358
4348 def __iter__(self): 4359 def __iter__(self):
4349 yield (self.line[:self.colstart], '') 4360 p = 0
4350 yield (self.line[self.colstart:self.colend], 'grep.match') 4361 for s, e in self.findpos():
4351 rest = self.line[self.colend:] 4362 yield self.line[p:s], ''
4352 while rest != '': 4363 yield self.line[s:e], 'grep.match'
4353 match = regexp.search(rest) 4364 p = e
4354 if not match: 4365 yield self.line[p:], ''
4355 yield (rest, '')
4356 break
4357 mstart, mend = match.span()
4358 yield (rest[:mstart], '')
4359 yield (rest[mstart:mend], 'grep.match')
4360 rest = rest[mend:]
4361 4366
4362 matches = {} 4367 matches = {}
4363 copies = {} 4368 copies = {}
4364 def grepbody(fn, rev, body): 4369 def grepbody(fn, rev, body):
4365 matches[rev].setdefault(fn, []) 4370 matches[rev].setdefault(fn, [])