Refactor parallel_diff generation a bit.
This commit is contained in:
		
							parent
							
								
									c179b48c97
								
							
						
					
					
						commit
						c881627d11
					
				|  | @ -13,15 +13,8 @@ module BlobHelper | |||
|   def highlight(blob_name, blob_content, nowrap: false, continue: false) | ||||
|     formatter = rouge_formatter(nowrap: nowrap) | ||||
| 
 | ||||
|     begin | ||||
|       @lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new | ||||
|       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 | ||||
|     @lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText | ||||
|     formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe | ||||
|   end | ||||
| 
 | ||||
|   def no_highlight_files | ||||
|  |  | |||
|  | @ -1,6 +1,4 @@ | |||
| module DiffHelper | ||||
|   BLANK_SPACE = " ".html_safe | ||||
| 
 | ||||
|   def diff_view | ||||
|     params[:view] == 'parallel' ? 'parallel' : 'inline' | ||||
|   end | ||||
|  | @ -49,15 +47,7 @@ module DiffHelper | |||
|     lines = [] | ||||
|     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| | ||||
| 
 | ||||
|       full_line = line.text | ||||
|       type = line.type | ||||
|       line_code = generate_line_code(diff_file.file_path, line) | ||||
|  | @ -72,31 +62,81 @@ module DiffHelper | |||
|         next_line = next_line.text | ||||
|       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 = [type, line_old, full_line, line_code, type, line_new, full_line, line_code] | ||||
|         lines.push(line) | ||||
|       elsif type == 'old' | ||||
|         if next_type == 'new' | ||||
|         lines << { | ||||
|           left: { | ||||
|             type:       type, | ||||
|             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 | ||||
|           line = [type, line_old, full_line, line_code, next_type, line_new, next_line, next_line_code] | ||||
|           lines.push(line) | ||||
|           lines << { | ||||
|             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 | ||||
|         elsif next_type == 'old' || next_type.nil? | ||||
|         when 'old', nil | ||||
|           # Left side has text removed, right side doesn't have any change | ||||
|           # 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.push(line) | ||||
|           lines << { | ||||
|             left: { | ||||
|               type:       type, | ||||
|               number:     line_old, | ||||
|               text:       full_line, | ||||
|               line_code:  line_code, | ||||
|             }, | ||||
|             right: { | ||||
|               type:       next_type, | ||||
|               number:     nil, | ||||
|               text:       "", | ||||
|               line_code:  nil | ||||
|             } | ||||
|           } | ||||
|         end | ||||
|       elsif type == 'new' | ||||
|       when 'new' | ||||
|         if skip_next | ||||
|           # Change has been already included in previous line so no need to do it again | ||||
|           skip_next = false | ||||
|           next | ||||
|         else | ||||
|           # 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.push(line) | ||||
|           lines << { | ||||
|             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 | ||||
|  |  | |||
|  | @ -2,41 +2,34 @@ | |||
| %div.text-file.diff-wrap-lines.code.file-content.js-syntax-highlight | ||||
|   %table | ||||
|     - parallel_diff(diff_file, index).each do |line| | ||||
|       - type_left = line[0] | ||||
|       - line_number_left = line[1] | ||||
|       - 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] | ||||
| 
 | ||||
|       - left = line[:left] | ||||
|       - right = line[:right] | ||||
|       %tr.line_holder.parallel | ||||
|         - if type_left == 'match' | ||||
|           = render "projects/diffs/match_line_parallel", { line: line_content_left, | ||||
|           line_old: line_number_left, line_new: line_number_right } | ||||
|         - elsif type_left ==  'old' || type_left.nil? | ||||
|           %td.old_line{id: line_code_left, class: "#{type_left}"} | ||||
|             = link_to raw(line_number_left), "##{line_code_left}", id: line_code_left | ||||
|         - if left[:type] == 'match' | ||||
|           = render "projects/diffs/match_line_parallel", { line: left[:text], | ||||
|           line_old: left[:number], line_new: right[:number] } | ||||
|         - else | ||||
|           %td.old_line{id: left[:line_code], class: "#{left[:type]}"} | ||||
|             = link_to raw(left[:number]), "##{left[:line_code]}", id: left[:line_code] | ||||
|             - if @comments_allowed && can?(current_user, :create_note, @project) | ||||
|               = link_to_new_diff_note(line_code_left, 'old') | ||||
|             %td.line_content{class: "parallel noteable_line #{type_left} #{line_code_left}", "line_code" => line_code_left }= line_content_left | ||||
|               = link_to_new_diff_note(left[:line_code], 'old') | ||||
|             %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_code = line_code_right | ||||
|             - new_line_code = right[:line_code] | ||||
|           - else | ||||
|             - 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 }} | ||||
|             = link_to raw(line_number_right), "##{new_line_code}", id: new_line_code | ||||
|           %td.new_line{id: new_line_code, class: "#{new_line_class}", data: { linenumber: right[:number] }} | ||||
|             = link_to raw(right[:number]), "##{new_line_code}", id: new_line_code | ||||
|             - if @comments_allowed && can?(current_user, :create_note, @project) | ||||
|               = link_to_new_diff_note(line_code_right, 'new') | ||||
|             %td.line_content.parallel{class: "noteable_line #{new_line_class} #{new_line_code}", "line_code" => new_line_code}= line_content_right | ||||
|               = 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}= diff_line_content(right[:text]) | ||||
| 
 | ||||
|       - 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? | ||||
|           = render "projects/notes/diff_notes_with_reply_parallel", notes_left: comments_left, notes_right: comments_right | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue