author | Claus Gittinger <cg@exept.de> |
Mon, 20 Nov 1995 14:23:29 +0100 | |
changeset 183 | 908f1f2c3ca0 |
parent 178 | 609b67e6140a |
child 242 | 85bf88a27903 |
permissions | -rw-r--r-- |
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 |
||
183 | 13 |
'From Smalltalk/X, Version:2.10.8 on 20-nov-1995 at 14:13:20' ! |
14 |
||
36 | 15 |
TwoColumnTextView subclass:#DiffTextView |
177
36c5c99b9ffb
fixed off-by-one error / added color marking of changes
Claus Gittinger <cg@exept.de>
parents:
165
diff
changeset
|
16 |
instanceVariableNames:'useColors showSeparators addedColor addedBgColor removedColor |
183 | 17 |
removedBgColor changedColor changedBgColor' |
45 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Views-Text' |
|
36 | 21 |
! |
22 |
||
23 |
!DiffTextView class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
45 | 28 |
All Rights Reserved |
36 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
45 | 41 |
a view showing diff (see unix manual pages) output in a |
42 |
user-friendly form. |
|
36 | 43 |
The view is created and opened with: |
44 |
||
45 |
d := DiffTextView openOn:text1 and:text2. |
|
46 |
||
47 |
and it will show the differences side-by-side |
|
45 | 48 |
For a real world application, see the ChangesBrowsers |
49 |
compare function. |
|
36 | 50 |
" |
183 | 51 |
! |
52 |
||
53 |
version |
|
54 |
^ '$Header: /cvs/stx/stx/libtool/Attic/DiffTxtV.st,v 1.12 1995-11-20 13:23:29 cg Exp $'! ! |
|
36 | 55 |
|
45 | 56 |
!DiffTextView class methodsFor:'defaults'! |
57 |
||
58 |
diffCommand |
|
59 |
^ 'diff -b' |
|
60 |
! ! |
|
61 |
||
36 | 62 |
!DiffTextView methodsFor:'accessing'! |
63 |
||
64 |
text1:t1 text2:t2 |
|
110 | 65 |
|tmpFile1 tmpFile2 name1 tmpName2 stream line text1 text2 diffList pidString| |
36 | 66 |
|
69 | 67 |
text1 := t1 asStringCollection. |
68 |
text2 := t2 asStringCollection. |
|
36 | 69 |
|
70 |
" |
|
71 |
save them texts in two temporary files ... |
|
72 |
" |
|
69 | 73 |
tmpFile1 := Filename newTemporary. |
74 |
stream := tmpFile1 writeStream. |
|
36 | 75 |
text1 do:[:line | |
45 | 76 |
stream nextPutAll:line; cr |
36 | 77 |
]. |
78 |
stream close. |
|
79 |
||
69 | 80 |
tmpFile2 := Filename newTemporary. |
81 |
stream := tmpFile2 writeStream. |
|
36 | 82 |
text2 do:[:line | |
45 | 83 |
stream nextPutAll:line; cr |
36 | 84 |
]. |
85 |
stream close. |
|
86 |
||
87 |
" |
|
88 |
start diff on it ... |
|
89 |
" |
|
45 | 90 |
stream := PipeStream |
91 |
readingFrom:self class diffCommand , ' ' , |
|
69 | 92 |
tmpFile1 asString, ' ' , tmpFile2 asString. |
36 | 93 |
stream isNil ifTrue:[ |
45 | 94 |
self error:'cannot execute diff'. |
95 |
text1 := text2 := nil. |
|
96 |
] ifFalse:[ |
|
97 |
diffList := OrderedCollection new. |
|
98 |
[stream atEnd] whileFalse:[ |
|
99 |
line := stream nextLine. |
|
100 |
line notNil ifTrue:[diffList add:line] |
|
101 |
]. |
|
102 |
stream close. |
|
36 | 103 |
]. |
45 | 104 |
|
69 | 105 |
tmpFile1 delete. |
106 |
tmpFile2 delete. |
|
36 | 107 |
|
108 |
self updateListsFrom:text1 and:text2 diffs:diffList |
|
109 |
||
110 |
" |
|
111 |
|v| |
|
112 |
||
113 |
v := HVScrollableView for:DiffTextView. |
|
114 |
v scrolledView text1:('../libview/Color.st' asFilename readStream contents) |
|
45 | 115 |
text2:('../libview/Color.st.old' asFilename readStream contents). |
36 | 116 |
v open |
117 |
" |
|
118 |
||
119 |
" |
|
120 |
|v t1 t2| |
|
121 |
||
122 |
t1 := ' |
|
123 |
one |
|
124 |
two |
|
125 |
three |
|
126 |
four |
|
127 |
'. |
|
128 |
t2 := ' |
|
129 |
one |
|
130 |
two-a |
|
131 |
two-b |
|
132 |
three |
|
133 |
three-b |
|
134 |
four |
|
135 |
'. |
|
136 |
||
137 |
v := DiffTextView new. |
|
138 |
v text1:t1 text2:t2. |
|
69 | 139 |
v open |
36 | 140 |
" |
141 |
! ! |
|
183 | 142 |
|
143 |
!DiffTextView methodsFor:'initialization'! |
|
144 |
||
145 |
initStyle |
|
146 |
super initStyle. |
|
147 |
||
148 |
showSeparators := false. |
|
149 |
||
150 |
useColors := ColoredListEntry notNil. |
|
151 |
useColors ifTrue:[ |
|
152 |
device hasColors ifTrue:[ |
|
153 |
addedColor := Color black. |
|
154 |
addedBgColor := Color green. |
|
155 |
||
156 |
removedColor := Color white. |
|
157 |
removedBgColor := Color red. |
|
158 |
||
159 |
changedColor := Color white. |
|
160 |
changedBgColor := Color blue. |
|
161 |
] ifFalse:[ |
|
162 |
addedBgColor := removedBgColor := changedBgColor := Color black. |
|
163 |
addedColor := removedColor := changedColor := Color white. |
|
164 |
] |
|
165 |
]. |
|
166 |
||
167 |
"Created: 16.11.1995 / 16:59:48 / cg" |
|
168 |
! ! |
|
169 |
||
170 |
!DiffTextView methodsFor:'private'! |
|
171 |
||
172 |
updateListsFrom:text1 and:text2 diffs:diffList |
|
173 |
"given the two texts in text1 and text2, and the diff-output in diffList, |
|
174 |
update my views contents" |
|
175 |
||
176 |
|idx1 idx2 dIdx dEnd state s nr1 nr2 nr3 op entry c l1 l2 any delta |
|
177 |
textView1 textView2| |
|
178 |
||
179 |
textView1 := textViews at:1. |
|
180 |
textView2 := textViews at:2. |
|
181 |
||
182 |
l1 := OrderedCollection new. |
|
183 |
l2 := OrderedCollection new. |
|
184 |
||
185 |
idx1 := 1. |
|
186 |
idx2 := 1. |
|
187 |
||
188 |
dIdx := 1. |
|
189 |
dEnd := diffList size + 1. |
|
190 |
state := #initial. |
|
191 |
[dIdx <= dEnd] whileTrue:[ |
|
192 |
dIdx == dEnd ifTrue:[ |
|
193 |
"dummy cleanup entry" |
|
194 |
entry := nil. |
|
195 |
state := #initial. |
|
196 |
] ifFalse:[ |
|
197 |
entry := diffList at:dIdx. |
|
198 |
]. |
|
199 |
||
200 |
state == #initial ifTrue:[ |
|
201 |
"entry is of the form <nr> <op> <offs> [<offs2>]" |
|
202 |
||
203 |
" |
|
204 |
fill up to size difference from previous change |
|
205 |
" |
|
206 |
delta := l1 size - l2 size. |
|
207 |
delta > 0 ifTrue:[ |
|
208 |
delta timesRepeat:[l2 add:nil] |
|
209 |
] ifFalse:[ |
|
210 |
delta < 0 ifTrue:[ |
|
211 |
delta negated timesRepeat:[l1 add:nil] |
|
212 |
] |
|
213 |
]. |
|
214 |
||
215 |
" |
|
216 |
except for the first chunk, add a separating line |
|
217 |
" |
|
218 |
l1 size ~~ 0 ifTrue:[ |
|
219 |
showSeparators ifTrue:[ |
|
220 |
l1 add:'--------'. |
|
221 |
l2 add:'--------'. |
|
222 |
] |
|
223 |
]. |
|
224 |
||
225 |
" |
|
226 |
in cleanup ? |
|
227 |
" |
|
228 |
entry isNil ifTrue:[ |
|
229 |
nr1 := text1 size + 1. |
|
230 |
nr2 := text2 size + 1. |
|
231 |
state := #finish. |
|
232 |
] ifFalse:[ |
|
233 |
s := ReadStream on:entry. |
|
234 |
nr1 := Integer readFrom:s. |
|
235 |
s peek == $, ifTrue:[ |
|
236 |
s next. |
|
237 |
Integer readFrom:s |
|
238 |
]. |
|
239 |
op := s next. |
|
240 |
nr2 := Integer readFrom:s. |
|
241 |
s peek == $, ifTrue:[ |
|
242 |
s next. |
|
243 |
nr3 := Integer readFrom:s |
|
244 |
] ifFalse:[ |
|
245 |
nr3 := nil |
|
246 |
]. |
|
247 |
||
248 |
op == $c ifTrue:[ |
|
249 |
state := #changed. |
|
250 |
] ifFalse:[ |
|
251 |
op == $a ifTrue:[ |
|
252 |
state := #added. |
|
253 |
] ifFalse:[ |
|
254 |
op == $d ifTrue:[ |
|
255 |
state := #deleted |
|
256 |
] ifFalse:[ |
|
257 |
self halt:'unexpected diff entry'. |
|
258 |
] |
|
259 |
] |
|
260 |
]. |
|
261 |
||
262 |
]. |
|
263 |
||
264 |
"/ nr1 print. ' ' print. op print. ' ' print. nr2 print. ' , ' print. nr3 printNL. |
|
265 |
||
266 |
" |
|
267 |
copy over unchanged lines |
|
268 |
" |
|
269 |
any := false. |
|
270 |
[idx1 < nr1] whileTrue:[ |
|
271 |
"/ '< add:' print. idx1 printNL. |
|
272 |
l1 add:(text1 at:idx1). |
|
273 |
idx1 := idx1 + 1. |
|
274 |
any := true. |
|
275 |
]. |
|
276 |
[idx2 < nr2] whileTrue:[ |
|
277 |
"/ '> add:' print. idx2 printNL. |
|
278 |
l2 add:(text2 at:idx2). |
|
279 |
idx2 := idx2 + 1. |
|
280 |
any := true. |
|
281 |
]. |
|
282 |
||
283 |
state == #added ifTrue:[ |
|
284 |
l1 add:(text1 at:idx1). |
|
285 |
idx1 := idx1 + 1. |
|
286 |
]. |
|
287 |
state == #deleted ifTrue:[ |
|
288 |
l2 add:(text2 at:idx2). |
|
289 |
idx2 := idx2 + 1. |
|
290 |
]. |
|
291 |
||
292 |
||
293 |
" |
|
294 |
add a separating line, except at end |
|
295 |
" |
|
296 |
any ifTrue:[ |
|
297 |
state ~~ #finish ifTrue:[ |
|
298 |
showSeparators ifTrue:[ |
|
299 |
l1 add:'--------'. |
|
300 |
l2 add:'--------'. |
|
301 |
] |
|
302 |
] |
|
303 |
]. |
|
304 |
||
305 |
] ifFalse:[ |
|
306 |
state == #changed ifTrue:[ |
|
307 |
(entry at:1) == $< ifTrue:[ |
|
308 |
useColors ifTrue:[ |
|
309 |
l1 add:(ColoredListEntry string:(text1 at:idx1) foregroundColor:changedColor backgroundColor:changedBgColor). |
|
310 |
] ifFalse:[ |
|
311 |
l1 add:(text1 at:idx1). |
|
312 |
]. |
|
313 |
idx1 := idx1 + 1 |
|
314 |
] ifFalse:[ |
|
315 |
(entry at:1) == $> ifTrue:[ |
|
316 |
useColors ifTrue:[ |
|
317 |
l2 add:(ColoredListEntry string:(text2 at:idx2) foregroundColor:changedColor backgroundColor:changedBgColor) |
|
318 |
] ifFalse:[ |
|
319 |
l2 add:(text2 at:idx2). |
|
320 |
]. |
|
321 |
idx2 := idx2 + 1 |
|
322 |
] ifFalse:[ |
|
323 |
(entry at:1) == $- ifTrue:[ |
|
324 |
] ifFalse:[ |
|
325 |
state := #initial. |
|
326 |
dIdx := dIdx - 1 |
|
327 |
] |
|
328 |
] |
|
329 |
] |
|
330 |
] ifFalse:[ |
|
331 |
state == #added ifTrue:[ |
|
332 |
(entry at:1) == $> ifTrue:[ |
|
333 |
useColors ifTrue:[ |
|
334 |
l2 add:(ColoredListEntry string:(text2 at:idx2) foregroundColor:addedColor backgroundColor:addedBgColor ) |
|
335 |
] ifFalse:[ |
|
336 |
l2 add:(text2 at:idx2). |
|
337 |
]. |
|
338 |
idx2 := idx2 + 1. |
|
339 |
l1 add:nil |
|
340 |
] ifFalse:[ |
|
341 |
state := #initial. |
|
342 |
dIdx := dIdx - 1 |
|
343 |
] |
|
344 |
] ifFalse:[ |
|
345 |
state == #deleted ifTrue:[ |
|
346 |
(entry at:1) == $< ifTrue:[ |
|
347 |
useColors ifTrue:[ |
|
348 |
l1 add:(ColoredListEntry string:(text1 at:idx1) foregroundColor:removedColor backgroundColor:removedBgColor ). |
|
349 |
] ifFalse:[ |
|
350 |
l1 add:(text1 at:idx1). |
|
351 |
]. |
|
352 |
idx1 := idx1 + 1. |
|
353 |
l2 add:nil |
|
354 |
] ifFalse:[ |
|
355 |
state := #initial. |
|
356 |
dIdx := dIdx - 1 |
|
357 |
] |
|
358 |
] |
|
359 |
"must be in finish otherwise" |
|
360 |
] |
|
361 |
] |
|
362 |
]. |
|
363 |
dIdx := dIdx + 1 |
|
364 |
]. |
|
365 |
[l1 size < l2 size] whileTrue:[ |
|
366 |
l1 add:''. |
|
367 |
]. |
|
368 |
[l2 size < l1 size] whileTrue:[ |
|
369 |
l2 add:''. |
|
370 |
]. |
|
371 |
textView1 list:l1. |
|
372 |
textView2 list:l2 |
|
373 |
||
374 |
"Modified: 20.11.1995 / 13:18:06 / cg" |
|
375 |
! ! |
|
376 |