Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2016-01-15 18:29:26 +0300
committerDouwe Maan <douwe@gitlab.com>2016-01-15 18:29:26 +0300
commit12546f895f86929ffe69299a02c93692370ee55a (patch)
tree94ec1cd6cdb9a9fe927833d6cfb518cebfcdbc26 /lib/gitlab/diff
parent5de8971ddfa10cbc6a082316eee7377038670f75 (diff)
Add tests
Diffstat (limited to 'lib/gitlab/diff')
-rw-r--r--lib/gitlab/diff/inline_diff.rb1
-rw-r--r--lib/gitlab/diff/inline_diff_marker.rb10
2 files changed, 11 insertions, 0 deletions
diff --git a/lib/gitlab/diff/inline_diff.rb b/lib/gitlab/diff/inline_diff.rb
index bae297ab00f..e5986fd69e2 100644
--- a/lib/gitlab/diff/inline_diff.rb
+++ b/lib/gitlab/diff/inline_diff.rb
@@ -38,6 +38,7 @@ module Gitlab
private
+ # Find runs of single line edits
def local_edit_indexes
line_prefixes = @lines.map { |line| line.match(/\A([+-])/) ? $1 : ' ' }
joined_line_prefixes = " #{line_prefixes.join} "
diff --git a/lib/gitlab/diff/inline_diff_marker.rb b/lib/gitlab/diff/inline_diff_marker.rb
index 4bb755e3d3d..405465a641f 100644
--- a/lib/gitlab/diff/inline_diff_marker.rb
+++ b/lib/gitlab/diff/inline_diff_marker.rb
@@ -11,9 +11,12 @@ module Gitlab
def mark(line_inline_diffs)
offset = 0
line_inline_diffs.each do |inline_diff_range|
+ # Map the inline-diff range based on the raw line to character positions in the rich line
inline_diff_positions = position_mapping[inline_diff_range]
+ # Turn the array of character positions into ranges
marker_ranges = collapse_ranges(inline_diff_positions)
+ # Mark each range
marker_ranges.each do |range|
offset = insert_around_range(rich_line, range, "<span class='idiff'>", "</span>", offset)
end
@@ -22,6 +25,9 @@ module Gitlab
rich_line
end
+ private
+
+ # Mapping of character positions in the raw line, to the rich (highlighted) line
def position_mapping
@position_mapping ||= begin
mapping = []
@@ -31,6 +37,8 @@ module Gitlab
raw_char = raw_line[raw_pos]
rich_char = rich_line[rich_pos]
+ # The raw and rich lines are the same except for HTML tags,
+ # so skip over any `<...>` segment
while rich_char == '<'
until rich_char == '>'
rich_pos += 1
@@ -50,6 +58,7 @@ module Gitlab
end
end
+ # Takes an array of integers, and returns an array of ranges covering the same integers
def collapse_ranges(positions)
return [] if positions.empty?
ranges = []
@@ -71,6 +80,7 @@ module Gitlab
ranges
end
+ # Inserts tags around the characters identified by the given range
def insert_around_range(text, range, before, after, offset = 0)
text.insert(offset + range.begin, before)
offset += before.length