-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiffTextView.m
492 lines (392 loc) · 11.6 KB
/
DiffTextView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* DiffTextView.m
*
* Copyright (c) 2002 Pierre-Yves Rivaille <[email protected]>
* 2012-2020 The GNUstep project
*
* This file is part of EasyDiff.app.
*
* EasyDiff.app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* EasyDiff.app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyDiff.app; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#import <Foundation/Foundation.h>
#import "DiffTextView.h"
#include <math.h>
#if defined(__APPLE__) && (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4)
#define NSUInteger unsigned int
#endif
@implementation DiffTextView
- (id) initWithFrame: (NSRect) aRect
{
self = [super initWithFrame: aRect];
if (self)
{
lineRangesArray = nil;
blockCharacterRangesArray = [[NSMutableArray alloc] init];
blockRectsArray = NSZoneMalloc([self zone], sizeof(NSRect)*10);
blockRectsArraySize = 10;
blockRectsArrayFirstFree = 0;
lastLine = 0;
firstCharOfLastLine = 0;
[self setEditable: NO];
[self setSelectable: YES];
[self setDrawsBackground: NO];
}
return self;
}
- (void) dealloc
{
[lineRangesArray release];
[changes release];
[colors release];
[super dealloc];
}
- (void) setChanges: (NSArray *)anArray
{
int i;
int count;
if (changes != anArray)
{
[changes release];
changes = anArray;
[anArray retain];
}
[colors release];
count = [changes count] / 2;
colors = [[NSMutableArray alloc] initWithCapacity: count];
for (i = 0; i < count; i++)
{
[colors addObject: [NSColor colorWithDeviceRed:0.7
green:0.7
blue:1.
alpha:1.]];
}
// NSLog(@"changes count %d", [changes count]);
}
- (void) setColor: (NSColor *) aColor
forChangeNumber: (int) number
{
if (colors == nil)
return;
[colors replaceObjectAtIndex: number
withObject: aColor];
blockRectsArray[number].size.width = [self visibleRect].size.width;
blockRectsArray[number].origin.x = [self visibleRect].origin.x;
[self setNeedsDisplayInRect:
blockRectsArray[number]];
}
- (void) computeLineRangesFromUpTo: (int) number
{
NSArray *array;
int count;
NSLayoutManager *lm = [self layoutManager];
NSTextContainer *tc = [self textContainer];
array = changes;
count = [array count];
NSLog(@"count %d", count);
{
NSString *internalString = [self string];
int startLine;
int endLine;
int currentLine;
NSUInteger firstCharOfLine = firstCharOfLastLine;
NSUInteger firstCharOfNextLine;
int i;
int a, b;
separationPosition = (float *)
NSZoneMalloc([self zone], sizeof(float) *
(count + 2));
separationDiff = (float *)
NSZoneMalloc([self zone], sizeof(float) *
(count + 1));
separationPosition[0] = 0;
currentLine = lastLine;
for (i = 0; i < count; i += 2)
{
startLine = [[array objectAtIndex: i] intValue];
endLine = [[array objectAtIndex: i+1] intValue] - 1;
while (currentLine < startLine)
{
[internalString getLineStart: NULL
end: &firstCharOfNextLine
contentsEnd: NULL
forRange: NSMakeRange(firstCharOfLine, 0)];
currentLine ++;
firstCharOfLine = firstCharOfNextLine;
}
a = firstCharOfLine;
[blockCharacterRangesArray
addObject: [NSNumber numberWithInt: firstCharOfLine]];
while (currentLine <= endLine)
{
[internalString getLineStart: NULL
end: &firstCharOfNextLine
contentsEnd: NULL
forRange: NSMakeRange(firstCharOfLine, 0)];
currentLine ++;
firstCharOfLine = firstCharOfNextLine;
}
b = firstCharOfLine - 1;
[blockCharacterRangesArray
addObject: [NSNumber numberWithInt: firstCharOfLine - 1]];
// NSLog(@"%d %d", blockRectsArraySize, blockRectsArrayFirstFree);
if (blockRectsArraySize == blockRectsArrayFirstFree)
{
blockRectsArray =
NSZoneRealloc([self zone],
blockRectsArray,
blockRectsArraySize * 2 * sizeof(NSRect));
blockRectsArraySize *= 2;
}
// NSLog(@"a=%d b=%d", a, b);
if (a == [internalString length])
{
blockRectsArray[blockRectsArrayFirstFree++] =
[lm boundingRectForGlyphRange:
[lm glyphRangeForCharacterRange:
NSMakeRange(a-1, 1)
actualCharacterRange:NULL]
inTextContainer:tc];
blockRectsArray[blockRectsArrayFirstFree - 1].origin.y =
NSMaxY(blockRectsArray[blockRectsArrayFirstFree - 1]);
blockRectsArray[blockRectsArrayFirstFree - 1].size.height = 0;
separationPosition[i + 1] =
NSMinY(blockRectsArray[blockRectsArrayFirstFree - 1]);
separationPosition[i + 2] =
NSMinY(blockRectsArray[blockRectsArrayFirstFree - 1]);
separationDiff[i] = separationPosition[i+1] -
separationPosition[i];
separationDiff[i+1] = separationPosition[i+2] -
separationPosition[i+1];
blockRectsArray[blockRectsArrayFirstFree - 1].size.height = 2;
blockRectsArray[blockRectsArrayFirstFree - 1].origin.y--;
}
else if (a >= b)
{
blockRectsArray[blockRectsArrayFirstFree++] =
[lm boundingRectForGlyphRange:
[lm glyphRangeForCharacterRange:
NSMakeRange(a, 1)
actualCharacterRange:NULL]
inTextContainer:tc];
separationPosition[i + 1] =
NSMinY(blockRectsArray[blockRectsArrayFirstFree - 1]);
separationPosition[i + 2] =
NSMinY(blockRectsArray[blockRectsArrayFirstFree - 1]);
NSLog(@"a %d %f", i + 1, separationPosition[i + 1]);
NSLog(@"a %d %f", i + 2, separationPosition[i + 2]);
separationDiff[i] = separationPosition[i+1] -
separationPosition[i];
separationDiff[i+1] = separationPosition[i+2] -
separationPosition[i+1];
blockRectsArray[blockRectsArrayFirstFree - 1].size.height = 2;
blockRectsArray[blockRectsArrayFirstFree - 1].origin.y--;
}
else
{
blockRectsArray[blockRectsArrayFirstFree++] =
[lm boundingRectForGlyphRange:
[lm glyphRangeForCharacterRange:
NSMakeRange(a, b - a)
actualCharacterRange:NULL]
inTextContainer:tc];
separationPosition[i + 1] =
NSMinY(blockRectsArray[blockRectsArrayFirstFree - 1]);
separationPosition[i + 2] =
NSMaxY(blockRectsArray[blockRectsArrayFirstFree - 1]);
NSLog(@"b %d %f", i + 1, separationPosition[i + 1]);
NSLog(@"b %d %f", i + 2, separationPosition[i + 2]);
separationDiff[i] = separationPosition[i+1] -
separationPosition[i];
separationDiff[i+1] = separationPosition[i+2] -
separationPosition[i+1];
}
// NSLog(@"%@", NSStringFromRect
// (blockRectsArray[blockRectsArrayFirstFree-1]));
}
separationPosition[count + 1] = NSMaxY([self bounds]);
separationDiff[count] = separationPosition[count + 1] -
separationPosition[count];
firstCharOfLastLine = firstCharOfLine;
lastLine = currentLine;
// NSLog(@"%d %@", [blockCharacterRangesArray count],
// [blockCharacterRangesArray description]);
}
{
}
}
/*
- (void) computeAllLineRanges
{
NSString *internalString = [self string];
int end;
int len = [internalString length];
RELEASE(lineRangesArray);
lineRangesArray = [[NSMutableArray alloc] init];
[lineRangesArray addObject:
[NSNumber numberWithInt: -1]];
end = -1;
while (end < len - 1)
{
[internalString getLineStart: NULL
end: NULL
contentsEnd: &end
forRange: NSMakeRange(end + 1, 0)];
if (end >= len)
{
[lineRangesArray addObject:
[NSNumber numberWithInt: len - 1]];
}
else
{
[lineRangesArray addObject:
[NSNumber numberWithInt: end]];
}
}
}
*/
- (void) setLineRanges: (NSArray *) lineRanges
{
if (lineRangesArray != lineRanges)
{
[lineRangesArray release];
lineRangesArray = lineRanges;
[lineRangesArray retain];
}
// NSLog(@"lineRanges count %d", [lineRanges count]);
}
- (NSArray *) lineRangesArray
{
return lineRangesArray;
}
- (NSRange) lineRangesForRect: (NSRect) aRect
{
int i, count;
int startLine = -1;
int endLine = -1;
NSRange glyphRange, characterRange;
NSLayoutManager *lm = [self layoutManager];
NSTextContainer *tc = [self textContainer];
glyphRange = [lm glyphRangeForBoundingRect: aRect
inTextContainer: tc];
characterRange = [lm characterRangeForGlyphRange: glyphRange
actualGlyphRange: NULL];
count = [lineRangesArray count];
i = 0;
while (i < count)
{
if ((int) characterRange.location <=
[[lineRangesArray objectAtIndex: i] intValue] + 1)
{
startLine = i;
break;
}
i++;
}
while (i < count)
{
if ((int) NSMaxRange(characterRange) <=
[[lineRangesArray objectAtIndex: i] intValue] + 1)
{
endLine = i;
break;
}
i++;
}
/*
NSLog(@"glyphRange : %@", NSStringFromRange(glyphRange));
NSLog(@"characterRange: %@", NSStringFromRange(characterRange));
*/
return NSMakeRange(startLine, endLine - startLine);
}
/*
- (void) highlightLinesInRange: (NSRange)aRange
{
unsigned end;
unsigned len = [string length];
end = -1;
for (i = 0; i < aRange.start; i++)
{
[string getLineStart: nil
end: nil
contentsEnd: &end
forRange: NSMakeRange(end + 1, 0)];
}
}
*/
- (void) drawRect: (NSRect) aRect
{
NSRange glyphRange, characterRange;
NSLayoutManager *lm = [self layoutManager];
NSTextContainer *tc = [self textContainer];
[[NSColor whiteColor] set];
NSRectFill(aRect);
glyphRange = [lm glyphRangeForBoundingRect:aRect
inTextContainer: tc];
if (!NSEqualRanges(glyphRange, NSMakeRange(0, 0)))
{
int i = 0;
int count = [blockCharacterRangesArray count];
int charStart, charEnd;
characterRange = [lm characterRangeForGlyphRange: glyphRange
actualGlyphRange: NULL];
for (i = 0; i < count; i += 2)
{
charEnd = [[blockCharacterRangesArray objectAtIndex: i+1]
intValue];
if (charEnd >= characterRange.location)
{
break;
}
}
for (; i < count; i += 2)
{
charStart = [[blockCharacterRangesArray objectAtIndex: i]
intValue];
if (charStart > NSMaxRange(characterRange))
{
break;
}
blockRectsArray[i/2].size.width = aRect.size.width;
blockRectsArray[i/2].origin.x = aRect.origin.x;
[(NSColor *)[colors objectAtIndex: i/2] set];
NSRectFill(NSIntersectionRect(aRect, blockRectsArray[i/2]));
}
}
[super drawRect: aRect];
}
- (void) superviewFrameChanged: (NSNotification *)aNotification
{
NSSize size;
float superWidth, selfWidth;
superWidth = [self convertRect: [[self superview] bounds]
fromView: [self superview]].size.width;
selfWidth = [[self layoutManager]
usedRectForTextContainer: [self textContainer]].size.width;
if (superWidth > selfWidth)
size.width = superWidth;
else
size.width = selfWidth;
size.height = [self frame].size.height;
[self setFrameSize: size];
}
- (void) scrollWheel: (NSEvent *)theEvent
{
if ([self nextResponder])
return [[self nextResponder] scrollWheel: theEvent];
else
return [self noResponderFor: @selector(scrollWheel:)];
}
@end