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