Class: DiffChunkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/diff_chunk_builder.rb

Overview

Motiro - A project tracking tool

Copyright (C) 2006-2007  Thiago Arrais

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Instance Method Summary collapse

Constructor Details

#initializeDiffChunkBuilder

Returns a new instance of DiffChunkBuilder.



20
21
22
23
24
25
# File 'lib/diff_chunk_builder.rb', line 20

def initialize
  @chunks = []
  @unmatched_deletions = []
  @old_line_num = @new_line_num = 1
  needs_new_chunk!
end

Instance Method Details

#get_chunksObject



59
60
61
62
# File 'lib/diff_chunk_builder.rb', line 59

def get_chunks
  consume_unmatched_deletions
  @chunks
end

#push_addition(text) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/diff_chunk_builder.rb', line 47

def push_addition(text)
  unless @unmatched_deletions.empty?
    chunk = target_chunk_for(:modification)
    old_text, old_position = @unmatched_deletions.shift
    chunk << Line.new(old_text, old_position, text, @new_line_num)
  else
    target_chunk_for(:addition) << Line.new(nil, @old_line_num,
                                            text, @new_line_num)
  end
  @new_line_num += 1
end

#push_deletion(text) ⇒ Object



34
35
36
37
# File 'lib/diff_chunk_builder.rb', line 34

def push_deletion(text)
  @unmatched_deletions << [text, @old_line_num]
  @old_line_num += 1
end

#push_unchanged(text) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/diff_chunk_builder.rb', line 39

def push_unchanged(text)
  consume_unmatched_deletions
  target_chunk_for(:unchanged) << Line.new(text, @old_line_num,
                                           text, @new_line_num)
  @old_line_num += 1
  @new_line_num += 1
end

#start_line(old_line_num, new_line_num = old_line_num) ⇒ Object



27
28
29
30
31
32
# File 'lib/diff_chunk_builder.rb', line 27

def start_line(old_line_num, new_line_num=old_line_num)
  consume_unmatched_deletions
  needs_new_chunk!
  @chunks << Separator.new(old_line_num - @old_line_num) unless @chunks.empty?
  @old_line_num, @new_line_num = old_line_num, new_line_num 
end