author | Claus Gittinger <cg@exept.de> |
Wed, 08 May 1996 17:00:32 +0200 | |
changeset 533 | 5bf1a4a2adad |
parent 519 | 95f7a0a41c05 |
child 544 | dea45065e7f1 |
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 |
||
163 |
useColors := ColoredListEntry notNil. |
|
164 |
useColors ifTrue:[ |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
165 |
device hasColors ifTrue:[ |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
166 |
addedColor := Color black. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
167 |
addedBgColor := Color green. |
183 | 168 |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
169 |
removedColor := Color white. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
170 |
removedBgColor := Color red. |
183 | 171 |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
172 |
changedColor := Color white. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
173 |
changedBgColor := Color blue. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
174 |
|
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
175 |
changedSpacesOnlyColor := Color white. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
176 |
changedSpacesOnlyBgColor := Color blue lightened. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
177 |
] ifFalse:[ |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
178 |
addedBgColor := removedBgColor := changedBgColor := Color black. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
179 |
addedColor := removedColor := changedColor := Color white. |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
180 |
] |
183 | 181 |
]. |
182 |
||
183 |
"Created: 16.11.1995 / 16:59:48 / cg" |
|
184 |
! ! |
|
185 |
||
186 |
!DiffTextView methodsFor:'private'! |
|
187 |
||
188 |
updateListsFrom:text1 and:text2 diffs:diffList |
|
189 |
"given the two texts in text1 and text2, and the diff-output in diffList, |
|
190 |
update my views contents" |
|
191 |
||
192 |
|idx1 idx2 dIdx dEnd state s nr1 nr2 nr3 op entry c l1 l2 any delta |
|
318 | 193 |
textView1 textView2 s1 s2| |
183 | 194 |
|
507 | 195 |
diffLineNumbers := OrderedCollection new. |
196 |
||
183 | 197 |
textView1 := textViews at:1. |
198 |
textView2 := textViews at:2. |
|
199 |
||
200 |
l1 := OrderedCollection new. |
|
201 |
l2 := OrderedCollection new. |
|
202 |
||
203 |
idx1 := 1. |
|
204 |
idx2 := 1. |
|
205 |
||
206 |
dIdx := 1. |
|
207 |
dEnd := diffList size + 1. |
|
208 |
state := #initial. |
|
209 |
[dIdx <= dEnd] whileTrue:[ |
|
210 |
dIdx == dEnd ifTrue:[ |
|
211 |
"dummy cleanup entry" |
|
212 |
entry := nil. |
|
213 |
state := #initial. |
|
214 |
] ifFalse:[ |
|
215 |
entry := diffList at:dIdx. |
|
216 |
]. |
|
217 |
||
218 |
state == #initial ifTrue:[ |
|
219 |
"entry is of the form <nr> <op> <offs> [<offs2>]" |
|
220 |
||
221 |
" |
|
222 |
fill up to size difference from previous change |
|
223 |
" |
|
224 |
delta := l1 size - l2 size. |
|
225 |
delta > 0 ifTrue:[ |
|
226 |
delta timesRepeat:[l2 add:nil] |
|
227 |
] ifFalse:[ |
|
228 |
delta < 0 ifTrue:[ |
|
229 |
delta negated timesRepeat:[l1 add:nil] |
|
230 |
] |
|
231 |
]. |
|
232 |
||
233 |
" |
|
234 |
except for the first chunk, add a separating line |
|
235 |
" |
|
236 |
l1 size ~~ 0 ifTrue:[ |
|
237 |
showSeparators ifTrue:[ |
|
238 |
l1 add:'--------'. |
|
239 |
l2 add:'--------'. |
|
240 |
] |
|
241 |
]. |
|
242 |
" |
|
243 |
in cleanup ? |
|
244 |
" |
|
245 |
entry isNil ifTrue:[ |
|
246 |
nr1 := text1 size + 1. |
|
247 |
nr2 := text2 size + 1. |
|
248 |
state := #finish. |
|
249 |
] ifFalse:[ |
|
507 | 250 |
diffLineNumbers add:l1 size. |
251 |
||
183 | 252 |
s := ReadStream on:entry. |
253 |
nr1 := Integer readFrom:s. |
|
254 |
s peek == $, ifTrue:[ |
|
255 |
s next. |
|
256 |
Integer readFrom:s |
|
257 |
]. |
|
258 |
op := s next. |
|
259 |
nr2 := Integer readFrom:s. |
|
260 |
s peek == $, ifTrue:[ |
|
261 |
s next. |
|
262 |
nr3 := Integer readFrom:s |
|
263 |
] ifFalse:[ |
|
264 |
nr3 := nil |
|
265 |
]. |
|
266 |
||
267 |
op == $c ifTrue:[ |
|
268 |
state := #changed. |
|
269 |
] ifFalse:[ |
|
270 |
op == $a ifTrue:[ |
|
271 |
state := #added. |
|
272 |
] ifFalse:[ |
|
273 |
op == $d ifTrue:[ |
|
274 |
state := #deleted |
|
275 |
] ifFalse:[ |
|
276 |
self halt:'unexpected diff entry'. |
|
277 |
] |
|
278 |
] |
|
279 |
]. |
|
280 |
||
281 |
]. |
|
282 |
||
283 |
"/ nr1 print. ' ' print. op print. ' ' print. nr2 print. ' , ' print. nr3 printNL. |
|
284 |
||
285 |
" |
|
286 |
copy over unchanged lines |
|
287 |
" |
|
288 |
any := false. |
|
289 |
[idx1 < nr1] whileTrue:[ |
|
290 |
"/ '< add:' print. idx1 printNL. |
|
291 |
l1 add:(text1 at:idx1). |
|
292 |
idx1 := idx1 + 1. |
|
293 |
any := true. |
|
294 |
]. |
|
295 |
[idx2 < nr2] whileTrue:[ |
|
296 |
"/ '> add:' print. idx2 printNL. |
|
297 |
l2 add:(text2 at:idx2). |
|
298 |
idx2 := idx2 + 1. |
|
299 |
any := true. |
|
300 |
]. |
|
301 |
||
302 |
state == #added ifTrue:[ |
|
303 |
l1 add:(text1 at:idx1). |
|
304 |
idx1 := idx1 + 1. |
|
305 |
]. |
|
306 |
state == #deleted ifTrue:[ |
|
307 |
l2 add:(text2 at:idx2). |
|
308 |
idx2 := idx2 + 1. |
|
309 |
]. |
|
310 |
||
311 |
||
312 |
" |
|
313 |
add a separating line, except at end |
|
314 |
" |
|
315 |
any ifTrue:[ |
|
316 |
state ~~ #finish ifTrue:[ |
|
317 |
showSeparators ifTrue:[ |
|
318 |
l1 add:'--------'. |
|
319 |
l2 add:'--------'. |
|
320 |
] |
|
321 |
] |
|
322 |
]. |
|
323 |
||
324 |
] ifFalse:[ |
|
325 |
state == #changed ifTrue:[ |
|
326 |
(entry at:1) == $< ifTrue:[ |
|
327 |
useColors ifTrue:[ |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
328 |
(l2 size >= idx1 |
318 | 329 |
and:[(s2 := l2 at:idx1) notNil |
330 |
and:[s2 asString withoutSeparators = (text1 at:idx1) withoutSeparators]]) ifTrue:[ |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
331 |
l1 add:(ColoredListEntry string:(text1 at:idx1) foregroundColor:changedSpacesOnlyColor backgroundColor:changedSpacesOnlyBgColor). |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
332 |
l2 at:idx1 put:(ColoredListEntry string:(l2 at:idx1) asString foregroundColor:changedSpacesOnlyColor backgroundColor:changedSpacesOnlyBgColor). |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
333 |
] ifFalse:[ |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
334 |
l1 add:(ColoredListEntry string:(text1 at:idx1) foregroundColor:changedColor backgroundColor:changedBgColor). |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
335 |
] |
183 | 336 |
] ifFalse:[ |
337 |
l1 add:(text1 at:idx1). |
|
338 |
]. |
|
339 |
idx1 := idx1 + 1 |
|
340 |
] ifFalse:[ |
|
341 |
(entry at:1) == $> ifTrue:[ |
|
342 |
useColors ifTrue:[ |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
343 |
(l1 size >= idx2 |
318 | 344 |
and:[(s1 := l1 at:idx2) notNil |
345 |
and:[s1 asString withoutSeparators = (text2 at:idx2) withoutSeparators]]) ifTrue:[ |
|
313
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
346 |
l2 add:(ColoredListEntry string:(text2 at:idx2) foregroundColor:changedSpacesOnlyColor backgroundColor:changedSpacesOnlyBgColor). |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
347 |
l1 at:idx2 put:(ColoredListEntry string:(l1 at:idx2) asString foregroundColor:changedSpacesOnlyColor backgroundColor:changedSpacesOnlyBgColor). |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
348 |
] ifFalse:[ |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
349 |
l2 add:(ColoredListEntry string:(text2 at:idx2) foregroundColor:changedColor backgroundColor:changedBgColor) |
c143d37be2e9
lines which differ only in blanks (indent) are shown light-blue
Claus Gittinger <cg@exept.de>
parents:
242
diff
changeset
|
350 |
] |
183 | 351 |
] ifFalse:[ |
352 |
l2 add:(text2 at:idx2). |
|
353 |
]. |
|
354 |
idx2 := idx2 + 1 |
|
355 |
] ifFalse:[ |
|
356 |
(entry at:1) == $- ifTrue:[ |
|
357 |
] ifFalse:[ |
|
358 |
state := #initial. |
|
359 |
dIdx := dIdx - 1 |
|
360 |
] |
|
361 |
] |
|
362 |
] |
|
363 |
] ifFalse:[ |
|
364 |
state == #added ifTrue:[ |
|
365 |
(entry at:1) == $> ifTrue:[ |
|
366 |
useColors ifTrue:[ |
|
367 |
l2 add:(ColoredListEntry string:(text2 at:idx2) foregroundColor:addedColor backgroundColor:addedBgColor ) |
|
368 |
] ifFalse:[ |
|
369 |
l2 add:(text2 at:idx2). |
|
370 |
]. |
|
371 |
idx2 := idx2 + 1. |
|
372 |
l1 add:nil |
|
373 |
] ifFalse:[ |
|
374 |
state := #initial. |
|
375 |
dIdx := dIdx - 1 |
|
376 |
] |
|
377 |
] ifFalse:[ |
|
378 |
state == #deleted ifTrue:[ |
|
379 |
(entry at:1) == $< ifTrue:[ |
|
380 |
useColors ifTrue:[ |
|
381 |
l1 add:(ColoredListEntry string:(text1 at:idx1) foregroundColor:removedColor backgroundColor:removedBgColor ). |
|
382 |
] ifFalse:[ |
|
383 |
l1 add:(text1 at:idx1). |
|
384 |
]. |
|
385 |
idx1 := idx1 + 1. |
|
386 |
l2 add:nil |
|
387 |
] ifFalse:[ |
|
388 |
state := #initial. |
|
389 |
dIdx := dIdx - 1 |
|
390 |
] |
|
391 |
] |
|
392 |
"must be in finish otherwise" |
|
393 |
] |
|
394 |
] |
|
395 |
]. |
|
396 |
dIdx := dIdx + 1 |
|
397 |
]. |
|
398 |
[l1 size < l2 size] whileTrue:[ |
|
399 |
l1 add:''. |
|
400 |
]. |
|
401 |
[l2 size < l1 size] whileTrue:[ |
|
402 |
l2 add:''. |
|
403 |
]. |
|
404 |
textView1 list:l1. |
|
405 |
textView2 list:l2 |
|
406 |
||
507 | 407 |
"Modified: 25.4.1996 / 13:29:53 / cg" |
183 | 408 |
! ! |
409 |
||
242 | 410 |
!DiffTextView class methodsFor:'documentation'! |
411 |
||
412 |
version |
|
533 | 413 |
^ '$Header: /cvs/stx/stx/libtool/DiffTextView.st,v 1.18 1996-05-08 15:00:32 cg Exp $'! ! |