Commit c881627d authored by Douwe Maan's avatar Douwe Maan

Refactor parallel_diff generation a bit.

parent c179b48c
...@@ -13,15 +13,8 @@ module BlobHelper ...@@ -13,15 +13,8 @@ module BlobHelper
def highlight(blob_name, blob_content, nowrap: false, continue: false) def highlight(blob_name, blob_content, nowrap: false, continue: false)
formatter = rouge_formatter(nowrap: nowrap) formatter = rouge_formatter(nowrap: nowrap)
begin @lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
@lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe
result = formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe
rescue
@lexer = Rouge::Lexers::PlainText
result = formatter.format(@lexer.lex(blob_content)).html_safe
end
result
end end
def no_highlight_files def no_highlight_files
......
module DiffHelper module DiffHelper
BLANK_SPACE = " ".html_safe
def diff_view def diff_view
params[:view] == 'parallel' ? 'parallel' : 'inline' params[:view] == 'parallel' ? 'parallel' : 'inline'
end end
...@@ -49,15 +47,7 @@ module DiffHelper ...@@ -49,15 +47,7 @@ module DiffHelper
lines = [] lines = []
skip_next = false skip_next = false
# Building array of lines
#
# [
# left_type, left_line_number, left_line_content, left_line_code,
# right_line_type, right_line_number, right_line_content, right_line_code
# ]
#
diff_file.highlighted_diff_lines.each do |line| diff_file.highlighted_diff_lines.each do |line|
full_line = line.text full_line = line.text
type = line.type type = line.type
line_code = generate_line_code(diff_file.file_path, line) line_code = generate_line_code(diff_file.file_path, line)
...@@ -72,31 +62,81 @@ module DiffHelper ...@@ -72,31 +62,81 @@ module DiffHelper
next_line = next_line.text next_line = next_line.text
end end
if type == 'match' || type.nil? case type
when 'match', nil
# line in the right panel is the same as in the left one # line in the right panel is the same as in the left one
line = [type, line_old, full_line, line_code, type, line_new, full_line, line_code] lines << {
lines.push(line) left: {
elsif type == 'old' type: type,
if next_type == 'new' number: line_old,
text: full_line,
line_code: line_code,
},
right: {
type: type,
number: line_new,
text: full_line,
line_code: line_code
}
}
when 'old'
case next_type
when 'new'
# Left side has text removed, right side has text added # Left side has text removed, right side has text added
line = [type, line_old, full_line, line_code, next_type, line_new, next_line, next_line_code] lines << {
lines.push(line) left: {
type: type,
number: line_old,
text: full_line,
line_code: line_code,
},
right: {
type: next_type,
number: line_new,
text: next_line,
line_code: next_line_code
}
}
skip_next = true skip_next = true
elsif next_type == 'old' || next_type.nil? when 'old', nil
# Left side has text removed, right side doesn't have any change # Left side has text removed, right side doesn't have any change
# No next line code, no new line number, no new line text # No next line code, no new line number, no new line text
line = [type, line_old, full_line, line_code, next_type, nil, BLANK_SPACE, nil] lines << {
lines.push(line) left: {
type: type,
number: line_old,
text: full_line,
line_code: line_code,
},
right: {
type: next_type,
number: nil,
text: "",
line_code: nil
}
}
end end
elsif type == 'new' when 'new'
if skip_next if skip_next
# Change has been already included in previous line so no need to do it again # Change has been already included in previous line so no need to do it again
skip_next = false skip_next = false
next next
else else
# Change is only on the right side, left side has no change # Change is only on the right side, left side has no change
line = [nil, nil, BLANK_SPACE, line_code, type, line_new, full_line, line_code] lines << {
lines.push(line) left: {
type: nil,
number: nil,
text: "",
line_code: line_code,
},
right: {
type: type,
number: line_new,
text: full_line,
line_code: line_code
}
}
end end
end end
end end
......
...@@ -2,41 +2,34 @@ ...@@ -2,41 +2,34 @@
%div.text-file.diff-wrap-lines.code.file-content.js-syntax-highlight %div.text-file.diff-wrap-lines.code.file-content.js-syntax-highlight
%table %table
- parallel_diff(diff_file, index).each do |line| - parallel_diff(diff_file, index).each do |line|
- type_left = line[0] - left = line[:left]
- line_number_left = line[1] - right = line[:right]
- line_content_left = line[2]
- line_code_left = line[3]
- type_right = line[4]
- line_number_right = line[5]
- line_content_right = line[6]
- line_code_right = line[7]
%tr.line_holder.parallel %tr.line_holder.parallel
- if type_left == 'match' - if left[:type] == 'match'
= render "projects/diffs/match_line_parallel", { line: line_content_left, = render "projects/diffs/match_line_parallel", { line: left[:text],
line_old: line_number_left, line_new: line_number_right } line_old: left[:number], line_new: right[:number] }
- elsif type_left == 'old' || type_left.nil? - else
%td.old_line{id: line_code_left, class: "#{type_left}"} %td.old_line{id: left[:line_code], class: "#{left[:type]}"}
= link_to raw(line_number_left), "##{line_code_left}", id: line_code_left = link_to raw(left[:number]), "##{left[:line_code]}", id: left[:line_code]
- if @comments_allowed && can?(current_user, :create_note, @project) - if @comments_allowed && can?(current_user, :create_note, @project)
= link_to_new_diff_note(line_code_left, 'old') = link_to_new_diff_note(left[:line_code], 'old')
%td.line_content{class: "parallel noteable_line #{type_left} #{line_code_left}", "line_code" => line_code_left }= line_content_left %td.line_content{class: "parallel noteable_line #{left[:type]} #{left[:line_code]}", "line_code" => left[:line_code] }= diff_line_content(left[:text])
- if type_right == 'new' - if right[:type] == 'new'
- new_line_class = 'new' - new_line_class = 'new'
- new_line_code = line_code_right - new_line_code = right[:line_code]
- else - else
- new_line_class = nil - new_line_class = nil
- new_line_code = line_code_left - new_line_code = left[:line_code]
%td.new_line{id: new_line_code, class: "#{new_line_class}", data: { linenumber: line_number_right }} %td.new_line{id: new_line_code, class: "#{new_line_class}", data: { linenumber: right[:number] }}
= link_to raw(line_number_right), "##{new_line_code}", id: new_line_code = link_to raw(right[:number]), "##{new_line_code}", id: new_line_code
- if @comments_allowed && can?(current_user, :create_note, @project) - if @comments_allowed && can?(current_user, :create_note, @project)
= link_to_new_diff_note(line_code_right, 'new') = link_to_new_diff_note(right[:line_code], 'new')
%td.line_content.parallel{class: "noteable_line #{new_line_class} #{new_line_code}", "line_code" => new_line_code}= line_content_right %td.line_content.parallel{class: "noteable_line #{new_line_class} #{new_line_code}", "line_code" => new_line_code}= diff_line_content(right[:text])
- if @reply_allowed - if @reply_allowed
- comments_left, comments_right = organize_comments(type_left, type_right, line_code_left, line_code_right) - comments_left, comments_right = organize_comments(left[:type], right[:type], left[:line_code], right[:line_code])
- if comments_left.present? || comments_right.present? - if comments_left.present? || comments_right.present?
= render "projects/notes/diff_notes_with_reply_parallel", notes_left: comments_left, notes_right: comments_right = render "projects/notes/diff_notes_with_reply_parallel", notes_left: comments_left, notes_right: comments_right
......
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