36
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
45
|
3 |
All Rights Reserved
|
36
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
TwoColumnTextView subclass:#DiffTextView
|
45
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Views-Text'
|
36
|
18 |
!
|
|
19 |
|
|
20 |
DiffTextView comment:'
|
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
45
|
22 |
All Rights Reserved
|
36
|
23 |
|
69
|
24 |
$Header: /cvs/stx/stx/libtool/DiffTextView.st,v 1.5 1995-02-22 01:24:40 claus Exp $
|
36
|
25 |
'!
|
|
26 |
|
|
27 |
!DiffTextView class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
45
|
32 |
All Rights Reserved
|
36
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
69
|
45 |
$Header: /cvs/stx/stx/libtool/DiffTextView.st,v 1.5 1995-02-22 01:24:40 claus Exp $
|
36
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
45
|
51 |
a view showing diff (see unix manual pages) output in a
|
|
52 |
user-friendly form.
|
36
|
53 |
The view is created and opened with:
|
|
54 |
|
|
55 |
d := DiffTextView openOn:text1 and:text2.
|
|
56 |
|
|
57 |
and it will show the differences side-by-side
|
45
|
58 |
For a real world application, see the ChangesBrowsers
|
|
59 |
compare function.
|
36
|
60 |
"
|
|
61 |
! !
|
|
62 |
|
45
|
63 |
!DiffTextView class methodsFor:'defaults'!
|
|
64 |
|
|
65 |
diffCommand
|
|
66 |
^ 'diff -b'
|
|
67 |
! !
|
|
68 |
|
36
|
69 |
!DiffTextView methodsFor:'private'!
|
|
70 |
|
|
71 |
updateListsFrom:text1 and:text2 diffs:diffList
|
69
|
72 |
"given the two texts in text1 and text2, and the diff-output in diffList,
|
|
73 |
update my views contents"
|
|
74 |
|
36
|
75 |
|idx1 idx2 dIdx dEnd state s nr1 nr2 nr3 op entry c l1 l2 any delta|
|
|
76 |
|
|
77 |
l1 := OrderedCollection new.
|
|
78 |
l2 := OrderedCollection new.
|
|
79 |
|
|
80 |
idx1 := 1.
|
|
81 |
idx2 := 1.
|
|
82 |
|
|
83 |
dIdx := 1.
|
|
84 |
dEnd := diffList size + 1.
|
|
85 |
state := #initial.
|
|
86 |
[dIdx <= dEnd] whileTrue:[
|
45
|
87 |
dIdx == dEnd ifTrue:[
|
|
88 |
"dummy cleanup entry"
|
|
89 |
entry := nil.
|
|
90 |
state := #initial.
|
|
91 |
] ifFalse:[
|
|
92 |
entry := diffList at:dIdx.
|
|
93 |
].
|
36
|
94 |
|
45
|
95 |
state == #initial ifTrue:[
|
|
96 |
"entry is of the form <nr> <op> <offs> [<offs2>]"
|
36
|
97 |
|
45
|
98 |
"
|
|
99 |
fill up to size difference from previous change
|
|
100 |
"
|
|
101 |
delta := l1 size - l2 size.
|
|
102 |
delta > 0 ifTrue:[
|
|
103 |
delta timesRepeat:[l2 add:nil]
|
|
104 |
] ifFalse:[
|
|
105 |
delta < 0 ifTrue:[
|
|
106 |
delta negated timesRepeat:[l1 add:nil]
|
|
107 |
]
|
|
108 |
].
|
36
|
109 |
|
45
|
110 |
"
|
|
111 |
except for the first chunk, add a separating line
|
|
112 |
"
|
|
113 |
l1 size ~~ 0 ifTrue:[
|
|
114 |
l1 add:'--------'.
|
|
115 |
l2 add:'--------'.
|
|
116 |
].
|
36
|
117 |
|
45
|
118 |
"
|
|
119 |
in cleanup ?
|
|
120 |
"
|
|
121 |
entry isNil ifTrue:[
|
|
122 |
nr1 := text1 size + 1.
|
|
123 |
nr2 := text2 size + 1.
|
|
124 |
state := #finish.
|
|
125 |
] ifFalse:[
|
|
126 |
s := ReadStream on:entry.
|
|
127 |
nr1 := Integer readFrom:s.
|
|
128 |
s peek == $, ifTrue:[
|
|
129 |
s next.
|
|
130 |
Integer readFrom:s
|
|
131 |
].
|
|
132 |
op := s next.
|
|
133 |
nr2 := Integer readFrom:s.
|
|
134 |
s peek == $, ifTrue:[
|
|
135 |
s next.
|
|
136 |
nr3 := Integer readFrom:s
|
|
137 |
] ifFalse:[
|
|
138 |
nr3 := nil
|
|
139 |
].
|
36
|
140 |
|
45
|
141 |
op == $c ifTrue:[
|
|
142 |
state := #changed.
|
|
143 |
] ifFalse:[
|
|
144 |
op == $a ifTrue:[
|
|
145 |
state := #added.
|
|
146 |
l1 add:(text1 at:idx1).
|
|
147 |
idx1 := idx1 + 1.
|
|
148 |
] ifFalse:[
|
|
149 |
op == $d ifTrue:[
|
|
150 |
state := #deleted
|
|
151 |
]
|
|
152 |
]
|
|
153 |
].
|
36
|
154 |
|
45
|
155 |
].
|
36
|
156 |
|
45
|
157 |
"
|
|
158 |
copy over unchanged lines
|
|
159 |
"
|
|
160 |
any := false.
|
|
161 |
[idx1 < nr1] whileTrue:[
|
|
162 |
l1 add:(text1 at:idx1).
|
|
163 |
idx1 := idx1 + 1.
|
|
164 |
any := true.
|
|
165 |
].
|
|
166 |
[idx2 < nr2] whileTrue:[
|
|
167 |
l2 add:(text2 at:idx2).
|
|
168 |
idx2 := idx2 + 1.
|
|
169 |
any := true.
|
|
170 |
].
|
36
|
171 |
|
45
|
172 |
"
|
|
173 |
add a separating line, except at end
|
|
174 |
"
|
|
175 |
any ifTrue:[
|
|
176 |
state ~~ #finish ifTrue:[
|
|
177 |
l1 add:'--------'.
|
|
178 |
l2 add:'--------'.
|
|
179 |
]
|
|
180 |
].
|
36
|
181 |
|
45
|
182 |
] ifFalse:[
|
|
183 |
state == #changed ifTrue:[
|
|
184 |
(entry at:1) == $< ifTrue:[
|
|
185 |
l1 add:(text1 at:idx1).
|
|
186 |
idx1 := idx1 + 1
|
|
187 |
] ifFalse:[
|
|
188 |
(entry at:1) == $> ifTrue:[
|
|
189 |
l2 add:(text2 at:idx2).
|
|
190 |
idx2 := idx2 + 1
|
|
191 |
] ifFalse:[
|
|
192 |
(entry at:1) == $- ifTrue:[
|
|
193 |
] ifFalse:[
|
|
194 |
state := #initial.
|
|
195 |
dIdx := dIdx - 1
|
|
196 |
]
|
|
197 |
]
|
|
198 |
]
|
|
199 |
] ifFalse:[
|
|
200 |
state == #added ifTrue:[
|
|
201 |
(entry at:1) == $> ifTrue:[
|
|
202 |
l2 add:(text2 at:idx2).
|
|
203 |
idx2 := idx2 + 1.
|
|
204 |
l1 add:nil
|
|
205 |
] ifFalse:[
|
|
206 |
state := #initial.
|
|
207 |
dIdx := dIdx - 1
|
|
208 |
]
|
|
209 |
] ifFalse:[
|
|
210 |
state == #deleted ifTrue:[
|
|
211 |
(entry at:1) == $< ifTrue:[
|
|
212 |
l1 add:(text1 at:idx1).
|
|
213 |
idx1 := idx1 + 1.
|
|
214 |
l2 add:nil
|
|
215 |
] ifFalse:[
|
|
216 |
state := #initial.
|
|
217 |
dIdx := dIdx - 1
|
|
218 |
]
|
|
219 |
]
|
|
220 |
"must be in finish otherwise"
|
|
221 |
]
|
|
222 |
]
|
|
223 |
].
|
|
224 |
dIdx := dIdx + 1
|
36
|
225 |
].
|
|
226 |
[l1 size < l2 size] whileTrue:[
|
45
|
227 |
l1 add:''.
|
36
|
228 |
].
|
|
229 |
[l2 size < l1 size] whileTrue:[
|
45
|
230 |
l2 add:''.
|
36
|
231 |
].
|
|
232 |
textView1 list:l1.
|
|
233 |
textView2 list:l2
|
|
234 |
! !
|
|
235 |
|
|
236 |
!DiffTextView methodsFor:'accessing'!
|
|
237 |
|
|
238 |
text1:t1 text2:t2
|
69
|
239 |
|tmpFile1 tmpFile2 Name1 tmpName2 stream line text1 text2 diffList pidString|
|
36
|
240 |
|
69
|
241 |
text1 := t1 asStringCollection.
|
|
242 |
text2 := t2 asStringCollection.
|
36
|
243 |
|
|
244 |
"
|
|
245 |
save them texts in two temporary files ...
|
|
246 |
"
|
69
|
247 |
tmpFile1 := Filename newTemporary.
|
|
248 |
stream := tmpFile1 writeStream.
|
36
|
249 |
text1 do:[:line |
|
45
|
250 |
stream nextPutAll:line; cr
|
36
|
251 |
].
|
|
252 |
stream close.
|
|
253 |
|
69
|
254 |
tmpFile2 := Filename newTemporary.
|
|
255 |
stream := tmpFile2 writeStream.
|
36
|
256 |
text2 do:[:line |
|
45
|
257 |
stream nextPutAll:line; cr
|
36
|
258 |
].
|
|
259 |
stream close.
|
|
260 |
|
|
261 |
"
|
|
262 |
start diff on it ...
|
|
263 |
"
|
45
|
264 |
stream := PipeStream
|
|
265 |
readingFrom:self class diffCommand , ' ' ,
|
69
|
266 |
tmpFile1 asString, ' ' , tmpFile2 asString.
|
36
|
267 |
stream isNil ifTrue:[
|
45
|
268 |
self error:'cannot execute diff'.
|
|
269 |
text1 := text2 := nil.
|
|
270 |
] ifFalse:[
|
|
271 |
diffList := OrderedCollection new.
|
|
272 |
[stream atEnd] whileFalse:[
|
|
273 |
line := stream nextLine.
|
|
274 |
line notNil ifTrue:[diffList add:line]
|
|
275 |
].
|
|
276 |
stream close.
|
36
|
277 |
].
|
45
|
278 |
|
69
|
279 |
tmpFile1 delete.
|
|
280 |
tmpFile2 delete.
|
36
|
281 |
|
|
282 |
self updateListsFrom:text1 and:text2 diffs:diffList
|
|
283 |
|
|
284 |
"
|
|
285 |
|v|
|
|
286 |
|
|
287 |
v := HVScrollableView for:DiffTextView.
|
|
288 |
v scrolledView text1:('../libview/Color.st' asFilename readStream contents)
|
45
|
289 |
text2:('../libview/Color.st.old' asFilename readStream contents).
|
36
|
290 |
v open
|
|
291 |
"
|
|
292 |
|
|
293 |
"
|
|
294 |
|v t1 t2|
|
|
295 |
|
|
296 |
t1 := '
|
|
297 |
one
|
|
298 |
two
|
|
299 |
three
|
|
300 |
four
|
|
301 |
'.
|
|
302 |
t2 := '
|
|
303 |
one
|
|
304 |
two-a
|
|
305 |
two-b
|
|
306 |
three
|
|
307 |
three-b
|
|
308 |
four
|
|
309 |
'.
|
|
310 |
|
|
311 |
v := DiffTextView new.
|
|
312 |
v text1:t1 text2:t2.
|
69
|
313 |
v open
|
36
|
314 |
"
|
|
315 |
! !
|