2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class InlineDiff
|
2016-04-02 18:10:28 +05:30
|
|
|
attr_accessor :old_line, :new_line, :offset
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def initialize(old_line, new_line, offset: 0)
|
|
|
|
@old_line = old_line[offset..-1]
|
|
|
|
@new_line = new_line[offset..-1]
|
|
|
|
@offset = offset
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def inline_diffs
|
2016-04-02 18:10:28 +05:30
|
|
|
# Skip inline diff if empty line was replaced with content
|
|
|
|
return if old_line == ""
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
CharDiff.new(old_line, new_line).changed_ranges(offset: offset)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
# Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/324638
|
2016-09-13 17:45:13 +05:30
|
|
|
class << self
|
2021-04-17 20:07:23 +05:30
|
|
|
def for_lines(lines)
|
|
|
|
pair_selector = Gitlab::Diff::PairSelector.new(lines)
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
inline_diffs = []
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
pair_selector.each do |old_index, new_index|
|
2016-09-13 17:45:13 +05:30
|
|
|
old_line = lines[old_index]
|
|
|
|
new_line = lines[new_index]
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
old_diffs, new_diffs = new(old_line, new_line, offset: 1).inline_diffs
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
inline_diffs[old_index] = old_diffs
|
|
|
|
inline_diffs[new_index] = new_diffs
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
inline_diffs
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|