2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-15 10:02:48 -05:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class InlineDiff
|
2016-01-29 13:37:17 -05:00
|
|
|
attr_accessor :old_line, :new_line, :offset
|
2016-01-15 10:02:48 -05:00
|
|
|
|
2016-01-29 13:37:17 -05:00
|
|
|
def initialize(old_line, new_line, offset: 0)
|
|
|
|
@old_line = old_line[offset..-1]
|
|
|
|
@new_line = new_line[offset..-1]
|
|
|
|
@offset = offset
|
|
|
|
end
|
|
|
|
|
2021-03-09 22:09:10 -05:00
|
|
|
def inline_diffs
|
2016-01-29 13:37:17 -05:00
|
|
|
# Skip inline diff if empty line was replaced with content
|
|
|
|
return if old_line == ""
|
|
|
|
|
2021-03-09 22:09:10 -05:00
|
|
|
CharDiff.new(old_line, new_line).changed_ranges(offset: offset)
|
2016-01-29 13:37:17 -05:00
|
|
|
end
|
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
# Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/324638
|
2016-07-19 08:43:11 -04:00
|
|
|
class << self
|
2021-03-09 22:09:10 -05:00
|
|
|
def for_lines(lines)
|
2021-03-04 19:09:24 -05:00
|
|
|
pair_selector = Gitlab::Diff::PairSelector.new(lines)
|
2016-01-15 10:02:48 -05:00
|
|
|
|
2016-07-19 08:43:11 -04:00
|
|
|
inline_diffs = []
|
2016-07-11 01:08:01 -04:00
|
|
|
|
2021-03-04 19:09:24 -05:00
|
|
|
pair_selector.each do |old_index, new_index|
|
2016-07-19 08:43:11 -04:00
|
|
|
old_line = lines[old_index]
|
|
|
|
new_line = lines[new_index]
|
2016-07-11 01:08:01 -04:00
|
|
|
|
2021-03-09 22:09:10 -05:00
|
|
|
old_diffs, new_diffs = new(old_line, new_line, offset: 1).inline_diffs
|
2016-07-11 01:08:01 -04:00
|
|
|
|
2016-07-19 08:43:11 -04:00
|
|
|
inline_diffs[old_index] = old_diffs
|
|
|
|
inline_diffs[new_index] = new_diffs
|
2016-07-11 01:08:01 -04:00
|
|
|
end
|
2016-07-19 08:43:11 -04:00
|
|
|
|
|
|
|
inline_diffs
|
2016-01-15 10:02:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|