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