##// END OF EJS Templates
grep: refactor loop that yields matched text with label...
Yuya Nishihara -
r29854:b842b1ad default
parent child Browse files
Show More
@@ -4345,19 +4345,24 def grep(ui, repo, pattern, *pats, **opt
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 = {}
@@ -111,6 +111,12 follow
111 color:2:-:orange
111 color:2:-:orange
112 color:1:+:orange
112 color:1:+:orange
113
113
114 test substring match: '^' should only match at the beginning
115
116 $ hg grep '^.' --config extensions.color= --color debug
117 [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|b]lack
118 [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|o]range
119 [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|b]lue
114
120
115 match in last "line" without newline
121 match in last "line" without newline
116
122
General Comments 0
You need to be logged in to leave comments. Login now