Blob of vendor/diff-display/lib/diff/display/data_structure.rb (raw blob data)

1 module Diff
2 module Display
3 class Data < Array
4 def initialize
5 super
6 end
7
8 def to_diff
9 diff = ""
10 each do |block|
11 block.each do |line|
12 case line
13 when HeaderLine
14 diff << "#{line}\n"
15 when UnModLine
16 diff << " #{line}\n"
17 when SepLine
18 diff << "\n"
19 when AddLine
20 diff << "+#{line}\n"
21 when RemLine
22 diff << "-#{line}\n"
23 end
24 end
25 end
26 diff.chomp
27 end
28 end
29
30 # Every line from the passed in diff gets transformed into an instance of
31 # one of line Line class's subclasses. One subclass exists for each line
32 # type in a diff. As such there is an AddLine class for added lines, a RemLine
33 # class for removed lines, an UnModLine class for lines which remain unchanged and
34 # a SepLine class which represents all the lines that aren't part of the diff.
35 class Line < String
36 class << self
37 def add(line, line_number)
38 AddLine.new(line, line_number)
39 end
40
41 def rem(line, line_number)
42 RemLine.new(line, line_number)
43 end
44
45 def unmod(line, old_number, new_number)
46 UnModLine.new(line, old_number, new_number)
47 end
48
49 def header(line)
50 HeaderLine.new(line)
51 end
52 end
53
54 def initialize(line, old_number = nil, new_number = nil)
55 super(line)
56 @old_number, @new_number = old_number, new_number
57 end
58 attr_reader :old_number, :new_number
59
60 def inspect
61 %Q{#<#{self.class.name} [#{old_number.inspect}-#{new_number.inspect}] "#{self}">}
62 end
63 end
64
65 class AddLine < Line
66 def initialize(line, line_number)
67 super(line, nil, line_number)
68 end
69 end
70
71 class RemLine < Line
72 def initialize(line, line_number)
73 super(line, line_number, nil)
74 end
75 end
76
77 class UnModLine < Line
78 def initialize(line, old_number, new_number)
79 super(line, old_number, new_number)
80 end
81 end
82
83 class SepLine < Line
84 def initialize(line = '...')
85 super(line)
86 end
87 end
88
89 class HeaderLine < Line
90 def initialize(line)
91 super(line)
92 end
93 end
94
95 # This class is an array which contains Line objects. Just like Line
96 # classes, several Block classes inherit from Block. If all the lines
97 # in the block are added lines then it is an AddBlock. If all lines
98 # in the block are removed lines then it is a RemBlock. If the lines
99 # in the block are all unmodified then it is an UnMod block. If the
100 # lines in the block are a mixture of added and removed lines then
101 # it is a ModBlock. There are no blocks that contain a mixture of
102 # modified and unmodified lines.
103 class Block < Array
104 class << self
105 def add; AddBlock.new end
106 def rem; RemBlock.new end
107 def mod; ModBlock.new end
108 def unmod; UnModBlock.new end
109 def header; HeaderBlock.new end
110 end
111 end
112
113 #:stopdoc:#
114 class AddBlock < Block; end
115 class RemBlock < Block; end
116 class ModBlock < Block; end
117 class UnModBlock < Block; end
118 class SepBlock < Block; end
119 class HeaderBlock < Block; end
120 #:startdoc:#
121 end
122 end