Skip to content

Commit

Permalink
Reduce cyclomatic complexity by spliting _normalize() out
Browse files Browse the repository at this point in the history
  • Loading branch information
ymattw committed Dec 8, 2024
1 parent 8034b73 commit b1d32cf
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,11 @@ def parse(self):
diff._hunks[-1].append(diff.parse_hunk_line(line))

elif diff.is_hunk_meta(line):
hunk_meta = line
try:
old_addr, new_addr = diff.parse_hunk_meta(hunk_meta)
old_addr, new_addr = diff.parse_hunk_meta(line)
except (IndexError, ValueError):
raise RuntimeError('invalid hunk meta: %s' % hunk_meta)
hunk = Hunk(headers, hunk_meta, old_addr, new_addr)
raise RuntimeError('invalid hunk meta: %s' % line)
hunk = Hunk(headers, line, old_addr, new_addr)
headers = []
diff._hunks.append(hunk)

Expand Down Expand Up @@ -471,23 +470,22 @@ def _markup_unified(self, diff):
else:
yield self._tint(' ' + old[1], 'common_line')

def _normalize(self, line):
index = 0
while True:
index = line.find('\t', index)
if index == -1:
break
# ignore special codes
offset = (line.count('\0', 0, index) * 2 +
line.count('\1', 0, index))
# next stop modulo tab width
width = self._tab_width - (index - offset) % self._tab_width
line = line[:index] + ' ' * width + line[(index + 1):]
return line.replace('\n', '').replace('\r', '')

def _markup_side_by_side(self, diff):
"""Returns a generator"""

def _normalize(line):
index = 0
while True:
index = line.find('\t', index)
if index == -1:
break
# ignore special codes
offset = (line.count('\0', 0, index) * 2 +
line.count('\1', 0, index))
# next stop modulo tab width
width = self._tab_width - (index - offset) % self._tab_width
line = line[:index] + ' ' * width + line[(index + 1):]
return line.replace('\n', '').replace('\r', '')

# Set up number width, note last hunk might be empty
try:
(start, offset) = diff._hunks[-1]._old_addr
Expand Down Expand Up @@ -535,8 +533,8 @@ def _normalize(line):
else:
right_num = ' '

left = _normalize(old[1])
right = _normalize(new[1])
left = self._normalize(old[1])
right = self._normalize(new[1])

if changed:
if not old[0]:
Expand Down

0 comments on commit b1d32cf

Please sign in to comment.