From e565be241f39cf4119cb1d3e2668455ed11630fe Mon Sep 17 00:00:00 2001
From: KennyTM~ <kennytm@gmail.com>
Date: Sat, 2 Feb 2013 15:25:57 +0800
Subject: [PATCH] =?UTF-8?q?Show=20only=20=E2=89=A416=20lines=20of=20code?=
 =?UTF-8?q?=20in=20a=20discussion=20(fix=20issue=20#2860).?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/helpers/commits_helper.rb              | 25 ++++++++++++++++++++++
 app/views/notes/_discussion_diff.html.haml |  3 +--
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/app/helpers/commits_helper.rb b/app/helpers/commits_helper.rb
index 6d2ce2feea..acdd48e04e 100644
--- a/app/helpers/commits_helper.rb
+++ b/app/helpers/commits_helper.rb
@@ -57,6 +57,31 @@ module CommitsHelper
     end
   end
 
+  def each_diff_line_near(diff, index, expected_line_code)
+    max_number_of_lines = 16
+
+    prev_match_line = nil
+    prev_lines = []
+
+    each_diff_line(diff, index) do |full_line, type, line_code, line_new, line_old|
+      line = [full_line, type, line_code, line_new, line_old]
+      if line_code != expected_line_code
+        if type == "match"
+          prev_lines.clear
+          prev_match_line = line
+        else
+          prev_lines.push(line)
+          prev_lines.shift if prev_lines.length >= max_number_of_lines
+        end
+      else
+        yield(prev_match_line) if !prev_match_line.nil?
+        prev_lines.each { |ln| yield(ln) }
+        yield(line)
+        break
+      end
+    end
+  end
+
   def image_diff_class(diff)
     if diff.deleted_file
       "deleted"
diff --git a/app/views/notes/_discussion_diff.html.haml b/app/views/notes/_discussion_diff.html.haml
index 790b77333d..20bdb3f356 100644
--- a/app/views/notes/_discussion_diff.html.haml
+++ b/app/views/notes/_discussion_diff.html.haml
@@ -9,7 +9,7 @@
   %br/
 .content
   %table
-    - each_diff_line(diff, note.diff_file_index) do |line, type, line_code, line_new, line_old|
+    - each_diff_line_near(diff, note.diff_file_index, note.line_code) do |line, type, line_code, line_new, line_old|
       %tr.line_holder{ id: line_code }
         - if type == "match"
           %td.old_line= "..."
@@ -22,4 +22,3 @@
 
           - if line_code == note.line_code
             = render "notes/diff_notes_with_reply", notes: discussion_notes
-            - break # cut off diff after notes
-- 
2.30.9