Commit 6eba13bf authored by Felipe's avatar Felipe

Include line code and position in DiscussionEntity and DiffFileEntity

parent f058b570
Pipeline #17333997 failed with stages
in 36 minutes and 7 seconds
......@@ -40,10 +40,6 @@ class DiffFileEntity < Grape::Entity
end
expose :highlighted_diff_lines do |diff_file|
diff_file.highlighted_diff_lines
end
expose :diff_lines do |diff_file|
diff_file.diff_lines
lines = diff_file.highlighted_diff_lines
end
end
......@@ -45,4 +45,12 @@ class DiscussionEntity < Grape::Entity
formats: [:html]
)
end
expose :position, if: -> (d, _) { defined? d.diff_file } do |discussion|
discussion.position
end
expose :line_code, if: -> (d, _) { defined? d.diff_file } do |discussion|
discussion.line_code
end
end
......@@ -126,11 +126,13 @@ module Gitlab
# Array of Gitlab::Diff::Line objects
def diff_lines
@diff_lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
@diff_lines ||=
Gitlab::Diff::Parser.new.parse(raw_diff.each_line, diff_file: self).to_a
end
def highlighted_diff_lines
@highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
@highlighted_diff_lines ||=
Gitlab::Diff::Highlight.new(diff_lines, repository: self.repository).highlight
end
# Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
......
......@@ -5,16 +5,12 @@ module Gitlab
delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff
def initialize(diff_lines, repository: nil)
def initialize(diff_lines, repository: nil, include_line_code: false)
@repository = repository
if diff_lines.is_a?(Gitlab::Diff::File)
@diff_file = diff_lines
@diff_lines = @diff_file.diff_lines
else
@diff_lines = diff_lines
end
@diff_lines = diff_lines
@include_line_code = include_line_code
@raw_lines = @diff_lines.map(&:text)
end
......
module Gitlab
module Diff
class Line
attr_reader :type, :index, :old_pos, :new_pos
attr_reader :type, :index, :old_pos, :new_pos, :line_code
attr_writer :rich_text
attr_accessor :text
......@@ -9,6 +9,7 @@ module Gitlab
@text, @type, @index = text, type, index
@old_pos, @new_pos = old_pos, new_pos
@parent_file = parent_file
@line_code = set_line_code
end
def self.init_from_hash(hash)
......@@ -58,7 +59,7 @@ module Gitlab
end
def rich_text
@parent_file.highlight_lines! if @parent_file && !@rich_text
@parent_file.try(:highlight_lines!) if @parent_file && !@rich_text
@rich_text
end
......@@ -69,9 +70,18 @@ module Gitlab
old_line: old_line,
new_line: new_line,
text: text,
rich_text: rich_text || text
rich_text: rich_text || text,
line_code: line_code
}
end
private
def set_line_code
return nil unless @parent_file
@parent_file.line_code(self)
end
end
end
end
......@@ -3,7 +3,7 @@ module Gitlab
class Parser
include Enumerable
def parse(lines)
def parse(lines, diff_file: nil)
return [] if lines.blank?
@lines = lines
......@@ -31,17 +31,17 @@ module Gitlab
next if line_old <= 1 && line_new <= 1 # top of file
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: diff_file)
line_obj_index += 1
next
elsif line[0] == '\\'
type = "#{context}-nonewline"
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: diff_file)
line_obj_index += 1
else
type = identification_type(line)
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
yielder << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new, parent_file: diff_file)
line_obj_index += 1
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment