comparison mercurial/patch.py @ 37730:8d730f96e792

patch: move yielding "\n" to the end of loop The original logic makes it harder to reason about - it yields the "\n" character belonging to the last line in the next loop iteration. The new code is in theory a little bit slower. But is more readable. It makes the following changes easier to read. Differential Revision: https://phab.mercurial-scm.org/D3210
author Jun Wu <quark@fb.com>
date Mon, 19 Mar 2018 04:28:29 -0700
parents 5537d8f5e989
children 5471348921c1
comparison
equal deleted inserted replaced
37729:6e137da59ad9 37730:8d730f96e792
2503 for chunk in func(*args, **kw): 2503 for chunk in func(*args, **kw):
2504 lines = chunk.split('\n') 2504 lines = chunk.split('\n')
2505 matches = {} 2505 matches = {}
2506 if inlinecolor: 2506 if inlinecolor:
2507 matches = _findmatches(lines) 2507 matches = _findmatches(lines)
2508 linecount = len(lines)
2508 for i, line in enumerate(lines): 2509 for i, line in enumerate(lines):
2509 if i != 0:
2510 yield ('\n', '')
2511 if head: 2510 if head:
2512 if line.startswith('@'): 2511 if line.startswith('@'):
2513 head = False 2512 head = False
2514 else: 2513 else:
2515 if line and not line.startswith((' ', '+', '-', '@', '\\')): 2514 if line and not line.startswith((' ', '+', '-', '@', '\\')):
2544 break 2543 break
2545 else: 2544 else:
2546 yield (line, '') 2545 yield (line, '')
2547 if line != stripline: 2546 if line != stripline:
2548 yield (line[len(stripline):], 'diff.trailingwhitespace') 2547 yield (line[len(stripline):], 'diff.trailingwhitespace')
2548 if i + 1 < linecount:
2549 yield ('\n', '')
2549 2550
2550 def _findmatches(slist): 2551 def _findmatches(slist):
2551 '''Look for insertion matches to deletion and returns a dict of 2552 '''Look for insertion matches to deletion and returns a dict of
2552 correspondences. 2553 correspondences.
2553 ''' 2554 '''