jessegrosjean / bdocuments

This URL has Read+Write access

bdocuments / BDocumentDifferencesWindowController.m
100644 106 lines (88 sloc) 3.435 kb
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
//
// BDocumentDifferencesWindowController.m
// BDocuments
//
// Created by Jesse Grosjean on 1/7/09.
// Copyright 2009 Hog Bay Software. All rights reserved.
//
 
#import "BDocumentDifferencesWindowController.h"
#import "BDiffMatchPatch.h"
 
@implementation BDocumentDifferencesWindowController
 
- (id)init {
if (self = [super initWithWindowNibName:@"BDocumentDifferencesWindow"]) {
}
return self;
}
 
- (id)initWithText1:(NSString *)aText1 text2:(NSString *)aText2 {
if (self = [self init]) {
text1 = aText1;
text2 = aText2;
}
return self;
}
 
- (void)awakeFromNib {
BDiffMatchPatch *dmp = [[BDiffMatchPatch alloc] init];
NSMutableArray *diffs = [dmp diffMainText1:text1 text2:text2];
[dmp diffCleanupSemantic:diffs];
NSAttributedString *prettyAttributedString = [dmp diffPrettyAttributedString:diffs];
[[textView textStorage] replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:prettyAttributedString];
}
 
- (IBAction)nextChange:(id)sender {
NSTextStorage *textStorage = [textView textStorage];
NSRange selectedRange = [textView selectedRange];
NSRange limitRange = NSMakeRange(NSMaxRange(selectedRange), [textStorage length] - NSMaxRange(selectedRange));
NSRange effectiveRange;
NSNumber *changetype;
BOOL firstPass = NO;
 
while (limitRange.length > 0) {
changetype = [textStorage attribute:BDocumentDiffTypeAttributeName atIndex:limitRange.location longestEffectiveRange:&effectiveRange inRange:limitRange];
if (firstPass) {
firstPass = NO;
} else if ([changetype integerValue] != BDiffEqual) {
[textView scrollRangeToVisible:effectiveRange];
[textView setSelectedRange:effectiveRange];
[textView showFindIndicatorForRange:effectiveRange];
break;
}
limitRange = NSMakeRange(NSMaxRange(effectiveRange), NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
if (limitRange.length == 0) {
limitRange = NSMakeRange(0, [textStorage length]);
}
}
}
 
- (void)processChanges:(BOOL)acceptingChanges {
NSTextStorage *textStorage = [textView textStorage];
NSRange selectedRange = [textView selectedRange];
NSRange limitRange = NSMakeRange(NSMaxRange(selectedRange), [textStorage length] - NSMaxRange(selectedRange));
NSRange effectiveRange;
NSNumber *changetype;
 
while (limitRange.length > 0) {
changetype = [textStorage attribute:BDocumentDiffTypeAttributeName atIndex:limitRange.location longestEffectiveRange:&effectiveRange inRange:limitRange];
if ([changetype integerValue] == BDiffDelete) {
if (acceptingChanges) {
[textStorage replaceCharactersInRange:effectiveRange withString:@""];
} else {
[textStorage removeAttribute:BDocumentDiffTypeAttributeName range:effectiveRange];
[textStorage removeAttribute:NSBackgroundColorAttributeName range:effectiveRange];
}
} else if ([changetype integerValue] == BDiffInsert) {
if (acceptingChanges) {
[textStorage removeAttribute:BDocumentDiffTypeAttributeName range:effectiveRange];
[textStorage removeAttribute:NSBackgroundColorAttributeName range:effectiveRange];
} else {
[textStorage replaceCharactersInRange:effectiveRange withString:@""];
}
}
limitRange = NSMakeRange(NSMaxRange(effectiveRange), NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
}
}
 
- (IBAction)previousChange:(id)sender {
 
}
 
- (IBAction)acceptChange:(id)sender {
 
}
 
- (IBAction)rejectChange:(id)sender {
 
}
 
- (IBAction)close:(id)sender {
[NSApp endSheet:[self window]];
}
 
@end