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