Skip to content

Commit 0e992a3

Browse files
committed
Properly highlight lines around '\ No newline at end of file'
1 parent 577f2fb commit 0e992a3

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

app/views/projects/diffs/_parallel_view.html.haml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
- if left[:type] == 'match'
99
= render "projects/diffs/match_line_parallel", { line: left[:text],
1010
line_old: left[:number], line_new: right[:number] }
11+
- elsif left[:type] == 'nonewline'
12+
%td.old_line
13+
%td.line_content.parallel.matched= left[:text]
14+
%td.new_line
15+
%td.line_content.parallel.matched= left[:text]
1116
- else
1217
%td.old_line{id: left[:line_code], class: "#{left[:type]}"}
1318
= link_to raw(left[:number]), "##{left[:line_code]}", id: left[:line_code]

app/views/projects/diffs/_text_file.html.haml

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
- if type == "match"
1616
= render "projects/diffs/match_line", {line: line.text,
1717
line_old: line_old, line_new: line.new_pos, bottom: false, new_file: diff_file.new_file}
18+
- elsif type == 'nonewline'
19+
%td.old_line.diff-line-num
20+
%td.new_line.diff-line-num
21+
%td.line_content.matched= line.text
1822
- else
1923
%td.old_line
2024
= link_to raw(type == "new" ? " " : line_old), "##{line_code}", id: line_code

lib/gitlab/diff/highlight.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(diff_file)
1414
def highlight
1515
@diff_lines.each_with_index do |diff_line, i|
1616
# ignore highlighting for "match" lines
17-
next if diff_line.type == 'match'
17+
next if diff_line.type == 'match' || diff_line.type == 'nonewline'
1818

1919
rich_line = highlight_line(diff_line, i)
2020

@@ -33,7 +33,7 @@ def highlight
3333
def highlight_line(diff_line, index)
3434
return html_escape(diff_line.text) unless diff_file.diff_refs
3535

36-
line_prefix = diff_line.text.match(/\A([+-])/) ? $1 : ' '
36+
line_prefix = diff_line.text.match(/\A(.)/) ? $1 : ' '
3737

3838
case diff_line.type
3939
when 'new', nil

lib/gitlab/diff/parallel_diff.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def parallelize
6262
}
6363
}
6464
skip_next = true
65-
when 'old', nil
65+
when 'old', 'nonewline', nil
6666
# Left side has text removed, right side doesn't have any change
6767
# No next line code, no new line number, no new line text
6868
lines << {

lib/gitlab/diff/parser.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,24 @@ def parse(lines)
2626
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
2727
line_obj_index += 1
2828
next
29+
elsif line[0] == '\\'
30+
type = 'nonewline'
31+
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
32+
line_obj_index += 1
2933
else
3034
type = identification_type(line)
3135
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
3236
line_obj_index += 1
3337
end
3438

3539

36-
if line[0] == "+"
40+
case line[0]
41+
when "+"
3742
line_new += 1
38-
elsif line[0] == "-"
43+
when "-"
3944
line_old += 1
45+
when "\\"
46+
# No increment
4047
else
4148
line_new += 1
4249
line_old += 1
@@ -59,9 +66,10 @@ def filename?(line)
5966
end
6067

6168
def identification_type(line)
62-
if line[0] == "+"
69+
case line[0]
70+
when "+"
6371
"new"
64-
elsif line[0] == "-"
72+
when "-"
6573
"old"
6674
else
6775
nil

0 commit comments

Comments
 (0)