Commit a09558921bafcfae91b85af563ee10144c3b7ace
1 parent
caf5b9e5
Exists in
master
and in
4 other branches
InlineDiff: base implementation
Showing
2 changed files
with
64 additions
and
2 deletions
Show diff stats
app/assets/stylesheets/sections/commits.scss
@@ -82,6 +82,17 @@ | @@ -82,6 +82,17 @@ | ||
82 | color:#333; | 82 | color:#333; |
83 | font-size: 12px; | 83 | font-size: 12px; |
84 | font-family: 'Menlo', 'Liberation Mono', 'Consolas', 'Courier New', 'andale mono','lucida console',monospace; | 84 | font-family: 'Menlo', 'Liberation Mono', 'Consolas', 'Courier New', 'andale mono','lucida console',monospace; |
85 | + .old{ | ||
86 | + span.idiff{ | ||
87 | + background-color:#FAA; | ||
88 | + } | ||
89 | + } | ||
90 | + .new{ | ||
91 | + span.idiff{ | ||
92 | + background-color:#AFA; | ||
93 | + } | ||
94 | + } | ||
95 | + | ||
85 | } | 96 | } |
86 | .diff_file_content_image { | 97 | .diff_file_content_image { |
87 | background:#eee; | 98 | background:#eee; |
app/helpers/commits_helper.rb
@@ -35,8 +35,9 @@ module CommitsHelper | @@ -35,8 +35,9 @@ module CommitsHelper | ||
35 | line_old = 1 | 35 | line_old = 1 |
36 | line_new = 1 | 36 | line_new = 1 |
37 | type = nil | 37 | type = nil |
38 | - | ||
39 | - lines_arr = diff_arr | 38 | + |
39 | + lines_arr = inline_diff diff_arr | ||
40 | + #lines_arr = diff_arr | ||
40 | lines_arr.each do |line| | 41 | lines_arr.each do |line| |
41 | next if line.match(/^\-\-\- \/dev\/null/) | 42 | next if line.match(/^\-\-\- \/dev\/null/) |
42 | next if line.match(/^\+\+\+ \/dev\/null/) | 43 | next if line.match(/^\+\+\+ \/dev\/null/) |
@@ -45,6 +46,9 @@ module CommitsHelper | @@ -45,6 +46,9 @@ module CommitsHelper | ||
45 | 46 | ||
46 | full_line = html_escape(line.gsub(/\n/, '')) | 47 | full_line = html_escape(line.gsub(/\n/, '')) |
47 | 48 | ||
49 | + full_line.gsub!("", "<span class='idiff'>") | ||
50 | + full_line.gsub!("", "</span>") | ||
51 | + | ||
48 | if line.match(/^@@ -/) | 52 | if line.match(/^@@ -/) |
49 | type = "match" | 53 | type = "match" |
50 | 54 | ||
@@ -81,4 +85,51 @@ module CommitsHelper | @@ -81,4 +85,51 @@ module CommitsHelper | ||
81 | nil | 85 | nil |
82 | end | 86 | end |
83 | end | 87 | end |
88 | + | ||
89 | + def inline_diff diff_arr | ||
90 | + chain_of_first_symbols = "" | ||
91 | + diff_arr.each_with_index do |line, i| | ||
92 | + chain_of_first_symbols += line[0] | ||
93 | + end | ||
94 | + chain_of_first_symbols.gsub!(/[^\-\+]/, "#") | ||
95 | + | ||
96 | + offset = 0 | ||
97 | + indexes = [] | ||
98 | + while index = chain_of_first_symbols.index("#-+#", offset) | ||
99 | + indexes << index | ||
100 | + offset = index + 1 | ||
101 | + end | ||
102 | + | ||
103 | + indexes.each do |index| | ||
104 | + first_line = diff_arr[index+1] | ||
105 | + second_line = diff_arr[index+2] | ||
106 | + max_length = [first_line.size, second_line.size].max | ||
107 | + | ||
108 | + first_the_same_symbols = 0 | ||
109 | + (0..max_length + 1).each do |i| | ||
110 | + first_the_same_symbols = i - 1 | ||
111 | + if first_line[i] != second_line[i] && i > 0 | ||
112 | + break | ||
113 | + end | ||
114 | + end | ||
115 | + first_token = first_line[0..first_the_same_symbols][1..-1] | ||
116 | + | ||
117 | + diff_arr[index+1].sub!(first_token, first_token + "") | ||
118 | + diff_arr[index+2].sub!(first_token, first_token + "") | ||
119 | + | ||
120 | + last_the_same_symbols = 0 | ||
121 | + (1..max_length + 1).each do |i| | ||
122 | + last_the_same_symbols = -i | ||
123 | + if first_line[-i] != second_line[-i] | ||
124 | + break | ||
125 | + end | ||
126 | + end | ||
127 | + last_the_same_symbols += 1 | ||
128 | + last_token = first_line[last_the_same_symbols..-1] | ||
129 | + | ||
130 | + diff_arr[index+1].sub!(/#{last_token}$/, "" + last_token) | ||
131 | + diff_arr[index+2].sub!(/#{last_token}$/, "" + last_token) | ||
132 | + end | ||
133 | + diff_arr | ||
134 | + end | ||
84 | end | 135 | end |