]> source.dussan.org Git - redmine.git/commitdiff
Highlight changes inside diff lines (#7139).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 11 Mar 2011 20:23:29 +0000 (20:23 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 11 Mar 2011 20:23:29 +0000 (20:23 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5094 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/views/common/_diff.rhtml
lib/redmine/unified_diff.rb
public/stylesheets/application.css
test/fixtures/diffs/partials.diff [new file with mode: 0644]
test/unit/lib/redmine/unified_diff_test.rb

index 619790c1378bbfd106a76bbbedefa98b2614fc8d..03b06a0cec522302fd0bb8e244c61c77e47812c7 100644 (file)
@@ -1,66 +1,56 @@
 <% diff = Redmine::UnifiedDiff.new(diff, :type => diff_type, :max_lines => Setting.diff_max_lines_displayed.to_i) -%>
+
 <% diff.each do |table_file| -%>
 <div class="autoscroll">
-<% if diff_type == 'sbs' -%>
+<% if diff.diff_type == 'sbs' -%>
 <table class="filecontent">
 <thead>
 <tr><th colspan="4" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
 </thead>
 <tbody>
-<% prev_line_left, prev_line_right = nil, nil -%>
-<% table_file.keys.sort.each do |key| -%>
-<% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
+<% table_file.each_line do |spacing, line| -%>
+<% if spacing -%>
 <tr class="spacing">
-<th class="line-num">...</th><td></td><th class="line-num">...</th><td></td>
+  <th class="line-num">...</th><td></td><th class="line-num">...</th><td></td>
+</tr>
 <% end -%>
 <tr>
-  <th class="line-num"><%= table_file[key].nb_line_left %></th>
-  <td class="line-code <%= table_file[key].type_diff_left %>">
-    <pre><%=to_utf8 table_file[key].line_left %></pre>
+  <th class="line-num"><%= line.nb_line_left %></th>
+  <td class="line-code <%= line.type_diff_left %>">
+    <pre><%=to_utf8 line.html_line_left %></pre>
   </td>
-  <th class="line-num"><%= table_file[key].nb_line_right %></th>
-  <td class="line-code <%= table_file[key].type_diff_right %>">
-    <pre><%=to_utf8 table_file[key].line_right %></pre>
+  <th class="line-num"><%= line.nb_line_right %></th>
+  <td class="line-code <%= line.type_diff_right %>">
+    <pre><%=to_utf8 line.html_line_right %></pre>
   </td>
 </tr>
-<% prev_line_left, prev_line_right = table_file[key].nb_line_left.to_i, table_file[key].nb_line_right.to_i -%>
 <% end -%>
 </tbody>
 </table>
 
 <% else -%>
-<table class="filecontent syntaxhl">
+<table class="filecontent">
 <thead>
 <tr><th colspan="3" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
 </thead>
 <tbody>
-<% prev_line_left, prev_line_right = nil, nil -%>
-<% table_file.keys.sort.each do |key, line| %>
-<% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
+<% table_file.each_line do |spacing, line| %>
+<% if spacing -%>
 <tr class="spacing">
-<th class="line-num">...</th><th class="line-num">...</th><td></td>
+  <th class="line-num">...</th><th class="line-num">...</th><td></td>
 </tr>
 <% end -%>
 <tr>
-  <th class="line-num"><%= table_file[key].nb_line_left %></th>
-  <th class="line-num"><%= table_file[key].nb_line_right %></th>
-  <% if table_file[key].line_left.empty? -%>
-  <td class="line-code <%= table_file[key].type_diff_right %>">
-    <pre><%=to_utf8 table_file[key].line_right %></pre>
+  <th class="line-num"><%= line.nb_line_left %></th>
+  <th class="line-num"><%= line.nb_line_right %></th>
+  <td class="line-code <%= line.type_diff %>">
+    <pre><%=to_utf8 line.html_line %></pre>
   </td>
-  <% else -%>
-  <td class="line-code <%= table_file[key].type_diff_left %>">
-    <pre><%=to_utf8 table_file[key].line_left %></pre>
-  </td>
-  <% end -%>
 </tr>
-<% prev_line_left = table_file[key].nb_line_left.to_i if table_file[key].nb_line_left.to_i > 0 -%>
-<% prev_line_right = table_file[key].nb_line_right.to_i if table_file[key].nb_line_right.to_i > 0 -%>
 <% end -%>
 </tbody>
 </table>
 <% end -%>
-
 </div>
 <% end -%>
 
index 430b1254a4d8471c3d547be93e475501c0bc8d73..f77721d6f8b89457e3d48d4f984e39396fb7c8f5 100644 (file)
@@ -1,5 +1,5 @@
-# redMine - project management software
-# Copyright (C) 2006-2008  Jean-Philippe Lang
+# Redmine - project management software
+# Copyright (C) 2006-2011  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 
 module Redmine
   # Class used to parse unified diffs
-  class UnifiedDiff < Array  
+  class UnifiedDiff < Array
+    attr_reader :diff_type
+    
     def initialize(diff, options={})
       options.assert_valid_keys(:type, :max_lines)
       diff = diff.split("\n") if diff.is_a?(String)
-      diff_type = options[:type] || 'inline'
+      @diff_type = options[:type] || 'inline'
       lines = 0
       @truncated = false
-      diff_table = DiffTable.new(diff_type)
+      diff_table = DiffTable.new(@diff_type)
       diff.each do |line|
         line_encoding = nil
         if line.respond_to?(:force_encoding)
@@ -53,17 +55,15 @@ module Redmine
   end
 
   # Class that represents a file diff
-  class DiffTable < Hash  
-    attr_reader :file_name, :line_num_l, :line_num_r    
+  class DiffTable < Array  
+    attr_reader :file_name
 
     # Initialize with a Diff file and the type of Diff View
     # The type view must be inline or sbs (side_by_side)
     def initialize(type="inline")
       @parsing = false
-      @nb_line = 1
-      @start = false
-      @before = 'same'
-      @second = true
+      @added = 0
+      @removed = 0
       @type = type
     end
 
@@ -86,11 +86,21 @@ module Redmine
           @line_num_l = $2.to_i
           @line_num_r = $5.to_i
         else
-          @nb_line += 1 if parse_line(line, @type)          
+          parse_line(line, @type)          
         end
       end
       return true
     end
+    
+    def each_line
+      prev_line_left, prev_line_right = nil, nil
+      each do |line|
+        spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1)
+        yield spacing, line
+        prev_line_left = line.nb_line_left.to_i if line.nb_line_left.to_i > 0
+        prev_line_right = line.nb_line_right.to_i if line.nb_line_right.to_i > 0
+      end
+    end
 
     def inspect
       puts '### DIFF TABLE ###'
@@ -100,74 +110,91 @@ module Redmine
       end
     end
 
-  private  
-    # Test if is a Side By Side type
-    def sbs?(type, func)
-      if @start and type == "sbs"
-        if @before == func and @second
-          tmp_nb_line = @nb_line
-          self[tmp_nb_line] = Diff.new
-        else
-            @second = false
-            tmp_nb_line = @start
-            @start += 1
-            @nb_line -= 1
-        end
-      else
-        tmp_nb_line = @nb_line
-        @start = @nb_line
-        self[tmp_nb_line] = Diff.new
-        @second = true
-      end
-      unless self[tmp_nb_line]
-        @nb_line += 1
-        self[tmp_nb_line] = Diff.new
-      else
-        self[tmp_nb_line]
-      end
-    end
+    private
 
     # Escape the HTML for the diff
     def escapeHTML(line)
         CGI.escapeHTML(line)
     end
+      
+    def diff_for_added_line
+      if @type == 'sbs' && @removed > 0 && @added < @removed
+        self[-(@removed - @added)]
+      else
+        diff = Diff.new
+        self << diff
+        diff
+      end
+    end
 
     def parse_line(line, type="inline")
       if line[0, 1] == "+"
-        diff = sbs? type, 'add'
-        @before = 'add'
+        diff = diff_for_added_line
         diff.line_right = escapeHTML line[1..-1]
         diff.nb_line_right = @line_num_r
         diff.type_diff_right = 'diff_in'
         @line_num_r += 1
+        @added += 1
         true
       elsif line[0, 1] == "-"
-        diff = sbs? type, 'remove'
-        @before = 'remove'
-        diff.line_left = escapeHTML line[1..-1]
-        diff.nb_line_left = @line_num_l
-        diff.type_diff_left = 'diff_out'
-        @line_num_l += 1
-        true
-      elsif line[0, 1] =~ /\s/
-        @before = 'same'
-        @start = false
         diff = Diff.new
-        diff.line_right = escapeHTML line[1..-1]
-        diff.nb_line_right = @line_num_r
         diff.line_left = escapeHTML line[1..-1]
         diff.nb_line_left = @line_num_l
-        self[@nb_line] = diff
+        diff.type_diff_left = 'diff_out'
+        self << diff
         @line_num_l += 1
-        @line_num_r += 1
+        @removed += 1
         true
-      elsif line[0, 1] = "\\"
+      else
+        write_offsets
+        if line[0, 1] =~ /\s/
+          diff = Diff.new
+          diff.line_right = escapeHTML line[1..-1]
+          diff.nb_line_right = @line_num_r
+          diff.line_left = escapeHTML line[1..-1]
+          diff.nb_line_left = @line_num_l
+          self << diff
+          @line_num_l += 1
+          @line_num_r += 1
+          true
+        elsif line[0, 1] = "\\"
           true
         else
           false
         end
       end
     end
+    
+    def write_offsets
+      if @added > 0 && @added == @removed
+        @added.times do |i|
+          line = self[-(1 + i)]
+          removed = (@type == 'sbs') ? line : self[-(1 + @added + i)]
+          offsets = offsets(removed.line_left, line.line_right)
+          removed.offsets = line.offsets = offsets
+        end
+      end
+      @added = 0
+      @removed = 0
+    end
+    
+    def offsets(line_left, line_right)
+      if line_left.present? && line_right.present? && line_left != line_right
+        max = [line_left.size, line_right.size].min
+        starting = 0
+        while starting < max && line_left[starting] == line_right[starting]
+          starting += 1
+        end
+        ending = -1
+        while ending >= -(max - starting) && line_left[ending] == line_right[ending]
+          ending -= 1
+        end
+        unless starting == 0 && ending == -1
+          [starting, ending]
+        end
+      end
+    end
+  end
 
   # A line of diff
   class Diff  
@@ -177,6 +204,7 @@ module Redmine
     attr_accessor :line_right
     attr_accessor :type_diff_right
     attr_accessor :type_diff_left
+    attr_accessor :offsets
     
     def initialize()
       self.nb_line_left = ''
@@ -186,6 +214,38 @@ module Redmine
       self.type_diff_right = ''
       self.type_diff_left = ''
     end
+    
+    def type_diff
+      type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
+    end
+    
+    def line
+      type_diff_right == 'diff_in' ? line_right : line_left
+    end
+    
+    def html_line_left
+      if offsets
+        line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
+      else
+        line_left
+      end
+    end
+    
+    def html_line_right
+      if offsets
+        line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
+      else
+        line_right
+      end
+    end
+    
+    def html_line
+      if offsets
+        line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>')
+      else
+        line
+      end
+    end
 
     def inspect
       puts '### Start Line Diff ###'
index fb762d15a7e8041c7951acb00ad61036b6915521..d3420205b4281c38d7f445f297ec8f9888959c08 100644 (file)
@@ -672,7 +672,9 @@ div.autocomplete ul li span.informal {
 
 /***** Diff *****/
 .diff_out { background: #fcc; }
+.diff_out span { background: #faa; }
 .diff_in { background: #cfc; }
+.diff_in span { background: #afa; }
 
 .text-diff {
 padding: 1em;
diff --git a/test/fixtures/diffs/partials.diff b/test/fixtures/diffs/partials.diff
new file mode 100644 (file)
index 0000000..f745776
--- /dev/null
@@ -0,0 +1,46 @@
+--- partials.txt       Wed Jan 19 12:06:17 2011
++++ partials.1.txt     Wed Jan 19 12:06:10 2011
+@@ -1,31 +1,31 @@
+-Lorem ipsum dolor sit amet, consectetur adipiscing elit
++Lorem ipsum dolor sit amet, consectetur adipiscing xx
+ Praesent et sagittis dui. Vivamus ac diam diam
+-Ut sed auctor justo
++xxx auctor justo
+ Suspendisse venenatis sollicitudin magna quis suscipit
+-Sed blandit gravida odio ac ultrices
++Sed blandit gxxxxa odio ac ultrices
+ Morbi rhoncus est ut est aliquam tempus
+-Morbi id nisi vel felis tincidunt tempus
++Morbi id nisi vel felis xx tempus
+ Mauris auctor sagittis ante eu luctus
+-Fusce commodo felis sed ligula congue molestie
++Fusce commodo felis sed ligula congue
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit
+-Praesent et sagittis dui. Vivamus ac diam diam
++et sagittis dui. Vivamus ac diam diam
+ Ut sed auctor justo
+ Suspendisse venenatis sollicitudin magna quis suscipit
+ Sed blandit gravida odio ac ultrices
+-Lorem ipsum dolor sit amet, consectetur adipiscing elit
+-Praesent et sagittis dui. Vivamus ac diam diam
++Lorem ipsum dolor sit amet, xxxx adipiscing elit
+ Ut sed auctor justo
+ Suspendisse venenatis sollicitudin magna quis suscipit
+ Sed blandit gravida odio ac ultrices
+-Morbi rhoncus est ut est aliquam tempus
++Morbi rhoncus est ut est xxxx tempus
++New line
+ Morbi id nisi vel felis tincidunt tempus
+ Mauris auctor sagittis ante eu luctus
+ Fusce commodo felis sed ligula congue molestie
+-Lorem ipsum dolor sit amet, consectetur adipiscing elit
+-Praesent et sagittis dui. Vivamus ac diam diam
+-Ut sed auctor justo
++Lorem ipsum dolor sit amet, xxxxtetur adipiscing elit
++Praesent et xxxxx. Vivamus ac diam diam
++Ut sed auctor
+ Suspendisse venenatis sollicitudin magna quis suscipit
+ Sed blandit gravida odio ac ultrices
+ Morbi rhoncus est ut est aliquam tempus
index e6da01c8d3d13c497cf44178e3338522fb89834e..13653e3a9b4b5e978a4ec0095e312a9d1a9fda7a 100644 (file)
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2008  Jean-Philippe Lang
+# Copyright (C) 2006-2011  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -34,6 +34,63 @@ class Redmine::UnifiedDiffTest < ActiveSupport::TestCase
     assert_equal 2, diff.size
   end
   
+  def test_inline_partials
+    diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
+    assert_equal 1, diff.size
+    diff = diff.first
+    assert_equal 43, diff.size
+    
+    assert_equal [51, -1], diff[0].offsets
+    assert_equal [51, -1], diff[1].offsets
+    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line
+    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line
+    
+    assert_nil diff[2].offsets
+    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line
+    
+    assert_equal [0, -14], diff[3].offsets
+    assert_equal [0, -14], diff[4].offsets
+    assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line
+    assert_equal '<span>xxx</span> auctor justo', diff[4].html_line
+    
+    assert_equal [13, -19], diff[6].offsets
+    assert_equal [13, -19], diff[7].offsets
+    
+    assert_equal [24, -8], diff[9].offsets
+    assert_equal [24, -8], diff[10].offsets
+    
+    assert_equal [37, -1], diff[12].offsets
+    assert_equal [37, -1], diff[13].offsets
+    
+    assert_equal [0, -38], diff[15].offsets
+    assert_equal [0, -38], diff[16].offsets
+  end
+  
+  def test_side_by_side_partials
+    diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
+    assert_equal 1, diff.size
+    diff = diff.first
+    assert_equal 32, diff.size
+    
+    assert_equal [51, -1], diff[0].offsets
+    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left
+    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right
+    
+    assert_nil diff[1].offsets
+    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left
+    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right
+    
+    assert_equal [0, -14], diff[2].offsets
+    assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left
+    assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right
+
+    assert_equal [13, -19], diff[4].offsets
+    assert_equal [24, -8], diff[6].offsets
+    assert_equal [37, -1], diff[8].offsets
+    assert_equal [0, -38], diff[10].offsets
+    
+  end
+  
   def test_line_starting_with_dashes
     diff = Redmine::UnifiedDiff.new(<<-DIFF
 --- old.txt Wed Nov 11 14:24:58 2009