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