Patchwork [2,of,8] grep: factor out function that prints matched line with labels

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 5, 2016, 8:07 p.m.
Message ID <fac24eab65a4dcee65fa.1473106070@waste.org>
Download mbox | patch
Permalink /patch/16553/
State Not Applicable
Headers show

Comments

Yuya Nishihara - Sept. 5, 2016, 8:07 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1471496989 -32400
#      Thu Aug 18 14:09:49 2016 +0900
# Node ID fac24eab65a4dcee65fa8be81f716859519f58b0
# Parent  b842b1adfea2ae19a07f5a43ee8b53954d00b83e
grep: factor out function that prints matched line with labels

Prepares for formatter support.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4356,14 +4356,6 @@ 
                 yield m.span()
                 p = m.end()
 
-        def __iter__(self):
-            p = 0
-            for s, e in self.findpos():
-                yield self.line[p:s], ''
-                yield self.line[s:e], 'grep.match'
-                p = e
-            yield self.line[p:], ''
-
     matches = {}
     copies = {}
     def grepbody(fn, rev, body):
@@ -4424,14 +4416,21 @@ 
                 if not opts.get('text') and binary():
                     ui.write(_(" Binary file matches"))
                 else:
-                    for s, label in l:
-                        ui.write(s, label=label)
+                    displaymatches(l)
             ui.write(eol)
             found = True
             if opts.get('files_with_matches'):
                 break
         return found
 
+    def displaymatches(l):
+        p = 0
+        for s, e in l.findpos():
+            ui.write(l.line[p:s])
+            ui.write(l.line[s:e], label='grep.match')
+            p = e
+        ui.write(l.line[p:])
+
     skip = {}
     revfiles = {}
     matchfn = scmutil.match(repo[None], pats, opts)