Skip to content

Commit c3b5d3d

Browse files
committed
Fix crash when using Xcode commit
1 parent 7364a77 commit c3b5d3d

1 file changed

Lines changed: 29 additions & 22 deletions

File tree

Classes/GitDiff.mm

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ @interface IDESourceCodeDocument : NSDocument
140140
@implementation IDESourceCodeDocument(GitDiff)
141141

142142
// source file is being saved
143-
- (void)_finishSavingToURL:(id)a0 ofType:(id)a1 forSaveOperation:(unsigned long)a2 changeCount:(id)a3 {
143+
- (void)_finishSavingToURL:(id)a0 ofType:(id)a1 forSaveOperation:(unsigned long)a2 changeCount:(id)a3
144+
{
144145
[super _finishSavingToURL:a0 ofType:a1 forSaveOperation:a2 changeCount:a3];
145146
[[GitFileDiffs alloc] performSelectorInBackground:@selector(initFile:) withObject:[[self fileURL] path]];
146147
}
@@ -162,7 +163,11 @@ - (NSTextView *)sourceTextView
162163

163164
- (GitFileDiffs *)gitDiffs
164165
{
165-
IDESourceCodeDocument *doc = [(id)[[self sourceTextView] delegate] document];
166+
NSTextView *sourceTextView = [self sourceTextView];
167+
if ( ![sourceTextView respondsToSelector:@selector(delegate)] )
168+
return nil;
169+
170+
IDESourceCodeDocument *doc = [(id)[sourceTextView delegate] document];
166171
NSString *path = [[doc fileURL] path];
167172

168173
GitFileDiffs *diffs = gitDiffPlugin.diffsByFile[path];
@@ -177,28 +182,30 @@ - (void)gitdiff_drawLineNumbersInSidebarRect:(CGRect)rect foldedIndexes:(unsigne
177182
{
178183
GitFileDiffs *diffs = [self gitDiffs];
179184

180-
[self lockFocus];
185+
if ( diffs ) {
186+
[self lockFocus];
181187

182-
for ( int i=0 ; i<indexCount ; i++ ) {
183-
unsigned long line = indexes[i];
184-
NSColor *highlight = !exists( diffs->added, line ) ? nil :
185-
exists( diffs->modified, line ) ? gitDiffPlugin.modifiedColor : gitDiffPlugin.addedColor;
186-
CGRect a0, a1;
188+
for ( int i=0 ; i<indexCount ; i++ ) {
189+
unsigned long line = indexes[i];
190+
NSColor *highlight = !exists( diffs->added, line ) ? nil :
191+
exists( diffs->modified, line ) ? gitDiffPlugin.modifiedColor : gitDiffPlugin.addedColor;
192+
CGRect a0, a1;
187193

188-
if ( highlight ) {
189-
[highlight setFill];
190-
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:line];
191-
NSRectFill( CGRectInset(a0,1.,1.) );
192-
}
193-
else if ( exists( diffs->deleted, line ) ) {
194-
[gitDiffPlugin.deletedColor setFill];
195-
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:line];
196-
a0.size.height = 1.;
197-
NSRectFill( a0 );
194+
if ( highlight ) {
195+
[highlight setFill];
196+
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:line];
197+
NSRectFill( CGRectInset(a0,1.,1.) );
198+
}
199+
else if ( exists( diffs->deleted, line ) ) {
200+
[gitDiffPlugin.deletedColor setFill];
201+
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:line];
202+
a0.size.height = 1.;
203+
NSRectFill( a0 );
204+
}
198205
}
199-
}
200206

201-
[self unlockFocus];
207+
[self unlockFocus];
208+
}
202209

203210
[self gitdiff_drawLineNumbersInSidebarRect:rect foldedIndexes:indexes count:indexCount
204211
linesToInvert:a3 linesToReplace:a4 getParaRectBlock:rectBlock];
@@ -213,8 +220,8 @@ - (id)gitdiff_annotationAtSidebarPoint:(CGPoint)p0
213220
GitFileDiffs *diffs = [self gitDiffs];
214221
unsigned long line = [self lineNumberForPoint:p0];
215222

216-
if ( exists( diffs->deleted, line ) ||
217-
(exists( diffs->added, line ) && exists( diffs->modified, line )) ) {
223+
if ( diffs && (exists( diffs->deleted, line ) ||
224+
(exists( diffs->added, line ) && exists( diffs->modified, line ))) ) {
218225
CGRect a0, a1;
219226
unsigned long start = diffs->modified[line];
220227
[self getParagraphRect:&a0 firstLineRect:&a1 forLineNumber:start];

0 commit comments

Comments
 (0)