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