[extensions-web] review: Remove metadata from chunks
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] review: Remove metadata from chunks
- Date: Tue, 26 Jun 2012 20:24:04 +0000 (UTC)
commit 75aafe2557f519d0691e070792eb5e29f85ef1a6
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Tue Jun 26 13:13:33 2012 -0400
review: Remove metadata from chunks
We don't use any of the features.
sweettooth/review/diffutils.py | 201 +---------------------------------------
1 files changed, 3 insertions(+), 198 deletions(-)
---
diff --git a/sweettooth/review/diffutils.py b/sweettooth/review/diffutils.py
index 9d4a8fe..3c368d7 100644
--- a/sweettooth/review/diffutils.py
+++ b/sweettooth/review/diffutils.py
@@ -743,15 +743,11 @@ def get_line_changed_regions(oldline, newline):
return (oldchanges, newchanges)
-def new_chunk(lines, collapsable=False, tag='equal', meta=None):
- if not meta:
- meta = {}
-
+def new_chunk(lines, collapsable=False, tag='equal'):
return {
'lines': lines,
'change': tag,
'collapsable': collapsable,
- 'meta': meta,
}
def get_fake_chunk(numlines, tag):
@@ -798,7 +794,7 @@ def get_chunks(a, b):
context_num_lines = 3
collapse_threshold = 2 * context_num_lines + 3
- for tag, i1, i2, j1, j2, meta in opcodes_with_metadata(differ):
+ for tag, i1, i2, j1, j2 in differ.get_opcodes():
numlines = max(i2-i1, j2-j1)
lines = [diff_line(old, new) for old, new in zip(zip(xrange(i1, i2), a), zip(xrange(j1, j2), b))]
@@ -818,7 +814,7 @@ def get_chunks(a, b):
yield new_chunk(lines[context_num_lines:last_range_start], collapsable=True)
yield new_chunk(lines[last_range_start:numlines])
else:
- yield new_chunk(lines[:numlines], collapsable=False, tag=tag, meta=meta)
+ yield new_chunk(lines[:numlines], collapsable=False, tag=tag)
linenum += numlines
@@ -840,197 +836,6 @@ def is_valid_move_range(lines):
return False
-
-def opcodes_with_metadata(differ):
- """Returns opcodes from the differ with extra metadata.
-
- This is a wrapper around a differ's get_opcodes function, which returns
- extra metadata along with each range. That metadata includes information
- on moved blocks of code and whitespace-only lines.
-
- This returns a list of opcodes as tuples in the form of
- (tag, i1, i2, j1, j2, meta).
- """
- groups = []
- removes = {}
- inserts = []
-
- for tag, i1, i2, j1, j2 in differ.get_opcodes():
- meta = {}
- group = (tag, i1, i2, j1, j2, meta)
- groups.append(group)
-
- # Store delete/insert ranges for later lookup. We will be building
- # keys that in most cases will be unique for the particular block
- # of text being inserted/deleted. There is a chance of collision,
- # so we store a list of matching groups under that key.
- #
- # Later, we will loop through the keys and attempt to find insert
- # keys/groups that match remove keys/groups.
- if tag == 'delete':
- for i in xrange(i1, i2):
- line = differ.a[i].strip()
-
- if line:
- removes.setdefault(line, []).append((i, group))
- elif tag == 'insert':
- inserts.append(group)
-
- # We now need to figure out all the moved locations.
- #
- # At this point, we know all the inserted groups, and all the individually
- # deleted lines. We'll be going through and finding consecutive groups
- # of matching inserts/deletes that represent a move block.
- #
- # The algorithm will be documented as we go in the code.
- #
- # We start by looping through all the inserted groups.
- for itag, ii1, ii2, ij1, ij2, imeta in inserts:
- # Store some state on the range we'll be working with inside this
- # insert group.
- #
- # i_move_cur is the current location inside the insert group
- # (from ij1 through ij2).
- #
- # i_move_range is the current range of consecutive lines that we'll
- # use for a move. Each line in this range has a corresponding
- # consecutive delete line.
- #
- # r_move_ranges represents deleted move ranges. The key is a
- # string in the form of "{i1}-{i2}-{j1}-{j2}", with those positions
- # taken from the remove group for the line. The value
- # is an array of tuples of (r_start, r_end, r_group). These values
- # are used to quickly locate deleted lines we've found that match
- # the inserted lines, so we can assemble ranges later.
- i_move_cur = ij1
- i_move_range = (i_move_cur, i_move_cur)
- r_move_ranges = {} # key -> [(start, end, group)]
-
- # Loop through every location from ij1 through ij2 until we've
- # reached the end.
- while i_move_cur <= ij2:
- try:
- iline = differ.b[i_move_cur].strip()
- except IndexError:
- iline = None
-
- if iline is not None and iline in removes:
- # The inserted line at this location has a corresponding
- # removed line.
- #
- # If there's already some information on removed line ranges
- # for this particular move block we're processing then we'll
- # update the range.
- #
- # The way we do that is to find each removed line that
- # matches this inserted line, and for each of those find
- # out if there's an existing move range that the found
- # removed line immediately follows. If there is, we update
- # the existing range.
- #
- # If there isn't any move information for this line, we'll
- # simply add it to the move ranges.
- for ri, rgroup in removes.get(iline, []):
- key = "%s-%s-%s-%s" % rgroup[1:5]
-
- if r_move_ranges:
- for i, r_move_range in \
- enumerate(r_move_ranges.get(key, [])):
- # If the remove information for the line is next in
- # the sequence for this calculated move range...
- if ri == r_move_range[1] + 1:
- r_move_ranges[key][i] = (r_move_range[0], ri,
- rgroup)
- break
- else:
- # We don't have any move ranges yet, so it's time to
- # build one based on any removed lines we find that
- # match the inserted line.
- r_move_ranges[key] = [(ri, ri, rgroup)]
-
- # On to the next line in the sequence...
- i_move_cur += 1
- else:
- # We've reached the very end of the insert group. See if
- # we have anything that looks like a move.
- if r_move_ranges:
- r_move_range = None
-
- # Go through every range of lines we've found and
- # find the longest.
- #
- # The longest move range wins. If we find two ranges that
- # are equal, though, we'll ignore both. The idea is that
- # if we have two identical moves, then it's probably
- # common enough code that we don't want to show the move.
- # An example might be some standard part of a comment
- # block, with no real changes in content.
- #
- # Note that with the current approach, finding duplicate
- # moves doesn't cause us to reset the winning range
- # to the second-highest identical match. We may want to
- # do that down the road, but it means additional state,
- # and this is hopefully uncommon enough to not be a real
- # problem.
- for ranges in r_move_ranges.itervalues():
- for r1, r2, rgroup in ranges:
- if not r_move_range:
- r_move_range = (r1, r2, rgroup)
- else:
- len1 = r_move_range[2] - r_move_range[1]
- len2 = r2 - r1
-
- if len1 < len2:
- r_move_range = (r1, r2, rgroup)
- elif len1 == len2:
- # If there are two that are the same, it
- # may be common code that we don't want to
- # see moves for. Comments, for example.
- r_move_range = None
-
- # If we have a move range, see if it's one we want to
- # include or filter out. Some moves are not impressive
- # enough to display. For example, a small portion of a
- # comment, or whitespace-only changes.
- if (r_move_range and
- is_valid_move_range(
- differ.a[r_move_range[0]:r_move_range[1]])):
-
- # Rebuild the insert and remove ranges based on
- # where we are now and which range we won.
- #
- # The new ranges will be actual lists of positions,
- # rather than a beginning and end. These will be
- # provided to the renderer.
- #
- # The ranges expected by the renderers are 1-based,
- # whereas our calculations for this algorithm are
- # 0-based, so we add 1 to the numbers.
- #
- # The upper boundaries passed to the range() function
- # must actually be one higher than the value we want.
- # So, for r_move_range, we actually increment by 2.
- # We only increment i_move_cur by one, because
- # i_move_cur already factored in the + 1 by being
- # at the end of the while loop.
- i_move_range = range(i_move_range[0] + 1,
- i_move_cur + 1)
- r_move_range = range(r_move_range[0] + 1,
- r_move_range[1] + 2)
-
- rmeta = rgroup[-1]
- rmeta.setdefault('moved', {}).update(
- dict(zip(r_move_range, i_move_range)))
- imeta.setdefault('moved', {}).update(
- dict(zip(i_move_range, r_move_range)))
-
- # Reset the state for the next range.
- i_move_cur += 1
- i_move_range = (i_move_cur, i_move_cur)
- r_move_ranges = {}
-
- return groups
-
def _test(oldfile, newfile):
old, new = open(oldfile, 'r'), open(newfile, 'r')
a, b = old.read().splitlines(), new.read().splitlines()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]