summaryrefslogtreecommitdiffstats
path: root/vendor/diff-display
diff options
context:
space:
mode:
authorJohan Sørensen <johan@johansorensen.com>2008-02-22 23:33:03 +0100
committerJohan Sørensen <johan@johansorensen.com>2008-02-22 23:33:03 +0100
commita3bc77aebc3befa7a7756c52ddce7191baf36768 (patch)
tree8cf96350cbe81818dba0ebe5379a7d36c3c67463 /vendor/diff-display
parentd6b6bd3d232cae428221c47a3487f6459c33edbb (diff)
downloadgitorious-mainline-outdated-a3bc77aebc3befa7a7756c52ddce7191baf36768.zip
gitorious-mainline-outdated-a3bc77aebc3befa7a7756c52ddce7191baf36768.tar.gz
gitorious-mainline-outdated-a3bc77aebc3befa7a7756c52ddce7191baf36768.tar.bz2
updated diff-display lib (technoweenie); fixes diff line numbering
Diffstat (limited to 'vendor/diff-display')
-rw-r--r--vendor/diff-display/lib/diff/display/data_structure.rb24
-rw-r--r--vendor/diff-display/lib/diff/display/unified/generator.rb61
-rw-r--r--vendor/diff-display/spec/data_structure_spec.rb12
-rw-r--r--vendor/diff-display/spec/generator_spec.rb50
4 files changed, 83 insertions, 64 deletions
diff --git a/vendor/diff-display/lib/diff/display/data_structure.rb b/vendor/diff-display/lib/diff/display/data_structure.rb
index b8e6a47..e9696eb 100644
--- a/vendor/diff-display/lib/diff/display/data_structure.rb
+++ b/vendor/diff-display/lib/diff/display/data_structure.rb
@@ -42,8 +42,8 @@ module Diff
RemLine.new(line, line_number)
end
- def unmod(line, line_number)
- UnModLine.new(line, line_number)
+ def unmod(line, old_number, new_number)
+ UnModLine.new(line, old_number, new_number)
end
def header(line)
@@ -51,44 +51,44 @@ module Diff
end
end
- def initialize(line, line_number)
+ def initialize(line, old_number = nil, new_number = nil)
super(line)
- @number = line_number
+ @old_number, @new_number = old_number, new_number
end
- attr_reader :number
+ attr_reader :old_number, :new_number
def inspect
- %Q{#<#{self.class.name} [#{number.inspect}] "#{self}">}
+ %Q{#<#{self.class.name} [#{old_number.inspect}-#{new_number.inspect}] "#{self}">}
end
end
class AddLine < Line
def initialize(line, line_number)
- super(line, line_number)
+ super(line, nil, line_number)
end
end
class RemLine < Line
def initialize(line, line_number)
- super(line, line_number)
+ super(line, line_number, nil)
end
end
class UnModLine < Line
- def initialize(line, line_number)
- super(line, line_number)
+ def initialize(line, old_number, new_number)
+ super(line, old_number, new_number)
end
end
class SepLine < Line
def initialize(line = '...')
- super(line, nil)
+ super(line)
end
end
class HeaderLine < Line
def initialize(line)
- super(line, nil)
+ super(line)
end
end
diff --git a/vendor/diff-display/lib/diff/display/unified/generator.rb b/vendor/diff-display/lib/diff/display/unified/generator.rb
index 1a39a0d..680adba 100644
--- a/vendor/diff-display/lib/diff/display/unified/generator.rb
+++ b/vendor/diff-display/lib/diff/display/unified/generator.rb
@@ -27,8 +27,7 @@ module Diff::Display
@prev_buffer = []
@line_type = nil
@prev_line_type = nil
- @offset_base = 0
- @offset_changed = 0
+ @offset = [0, 0]
@data = Data.new
self
end
@@ -58,10 +57,9 @@ module Diff::Display
identify_block
push Block.header
current_block << Line.header(line)
- add_separator unless @offset_changed.zero?
- @line_type = nil
- @offset_base = $1.to_i - 1
- @offset_changed = $3.to_i - 1
+ add_separator unless @offset[0].zero?
+ @line_type = nil
+ @offset = Array.new(2) { $3.to_i - 1 }
return
end
@@ -96,37 +94,16 @@ module Diff::Display
end
def identify_block
- if @prev_line_type.eql?(LINE_TYPES['-']) and @line_type.eql?(LINE_TYPES['+'])
- process_block(:mod, true, true)
- else
- if LINE_TYPES.values.include?(@line_type)
- process_block(@line_type, true)
- end
+ if LINE_TYPES.values.include?(@line_type)
+ process_block(@line_type)
end
@prev_line_type = nil
end
- def process_block(diff_line_type, new = false, old = false)
+ def process_block(diff_line_type)
push Block.send(diff_line_type)
- # Mod block
- if diff_line_type.eql?(:mod) && @prev_buffer.size && @buffer.size == 1
- process_line(@prev_buffer.first, @buffer.first)
- return
- end
- unroll_prev_buffer if old
- unroll_buffer if new
- end
-
- # TODO Needs a better name...it does process a line (two in fact) but
- # its primary function is to add a Rem and an Add pair which
- # potentially have inline changes
- def process_line(oldline, newline)
- # -
- current_block << Line.rem(oldline, @offset_base += 1)
-
- # +
- current_block << Line.add(newline, @offset_changed += 1)
+ unroll_buffer
end
def add_separator
@@ -160,19 +137,21 @@ module Diff::Display
@prev_buffer
end
- def unroll_prev_buffer
- return if @prev_buffer.empty?
- @prev_buffer.each do |line|
- @offset_base += 1
- current_block << Line.send(@prev_line_type, line, @offset_base)
- end
- end
-
def unroll_buffer
return if @buffer.empty?
@buffer.each do |line|
- @offset_changed += 1
- current_block << Line.send(@line_type, line, @offset_changed)
+ case @line_type
+ when :add
+ @offset[1] += 1
+ current_block << Line.send(@line_type, line, @offset[1])
+ when :rem
+ @offset[0] += 1
+ current_block << Line.send(@line_type, line, @offset[0])
+ when :unmod
+ @offset[0] += 1
+ @offset[1] += 1
+ current_block << Line.send(@line_type, line, *@offset)
+ end
end
end
diff --git a/vendor/diff-display/spec/data_structure_spec.rb b/vendor/diff-display/spec/data_structure_spec.rb
index c9b0c29..d5a726f 100644
--- a/vendor/diff-display/spec/data_structure_spec.rb
+++ b/vendor/diff-display/spec/data_structure_spec.rb
@@ -12,9 +12,15 @@ describe "Diff::Display data structures" do
end
describe "Line" do
- it "initializes with a line number" do
+ it "initializes with an old line number" do
line = Diff::Display::Line.new("foo", 12)
- line.number.should == 12
+ line.old_number.should == 12
+ end
+
+ it "initializes with numbers" do
+ line = Diff::Display::Line.new("foo", 12, 13)
+ line.old_number.should == 12
+ line.new_number.should == 13
end
it "has a class method for creating an AddLine" do
@@ -28,7 +34,7 @@ describe "Diff::Display data structures" do
end
it "has a class method for creating a UnModLine" do
- line = Diff::Display::Line.unmod("foo", 7)
+ line = Diff::Display::Line.unmod("foo", 7, 8)
line.should be_instance_of(Diff::Display::UnModLine)
end
diff --git a/vendor/diff-display/spec/generator_spec.rb b/vendor/diff-display/spec/generator_spec.rb
index 84dec09..bff1024 100644
--- a/vendor/diff-display/spec/generator_spec.rb
+++ b/vendor/diff-display/spec/generator_spec.rb
@@ -39,14 +39,48 @@ describe Diff::Display::Unified::Generator do
data.to_diff.should == diff_data.chomp
end
- it "doesn't parse linenumbers that isn't part if the diff" do
- diff_data = load_diff("pseudo_recursive")
- data = Diff::Display::Unified::Generator.run(diff_data)
- linenos = []
- data.each{|blk| blk.each{|line| linenos << line.number } }
- linenos.compact.should == (1..14).to_a
+ #it "doesn't parse linenumbers that isn't part if the diff" do
+ # line_numbers_for(:pseudo_recursive).compact.should == (1..14).to_a
+ #end
+ end
+
+ describe "line numbering" do
+ it "numbers correctly for multiple_adds_after_rem" do
+ line_numbers_for(:multiple_adds_after_rem).should == [
+ [193, 193],
+ [194, nil],
+ [nil, 194],
+ [nil, 195],
+ [nil, 196],
+ [nil, 197],
+ [nil, 198],
+ [195, 199]
+ ]
+ end
+
+ it "numbers correctly for simple" do
+ line_numbers_for(:simple).should == [
+ [1, 1],
+ [2, 2],
+ [3, nil],
+ [4, nil],
+ [nil, 3],
+ [nil, 4],
+ [nil, 5],
+ ]
end
-
end
-
+
+ def line_numbers_for(diff)
+ diff_data = load_diff(diff)
+ data = Diff::Display::Unified::Generator.run(diff_data)
+ linenos = []
+ data.each do |blk|
+ blk.each do |line|
+ next if line.class == Diff::Display::HeaderLine
+ linenos << [line.old_number, line.new_number]
+ end
+ end
+ linenos
+ end
end