Skip to content

Instantly share code, notes, and snippets.

@joericioppo
Created December 21, 2010 22:34
Show Gist options
  • Save joericioppo/750736 to your computer and use it in GitHub Desktop.
Save joericioppo/750736 to your computer and use it in GitHub Desktop.
- (void)reloadLineIndexes {
NSString *diffString = [[self textStorage] string];
if (diffString.length < 1) {
self.highestNewFileLineNumber = 0;
self.lineIndexes = nil;
return;
}
NSLayoutManager *layoutManager = [self layoutManager];
NSTextContainer *textContainer = [self textContainer];
NSRect textRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(0, NSUIntegerMax) inTextContainer:textContainer];
NSString *currentLine = nil;
NSUInteger currentIndex = 0;
NSUInteger newFileLineIndex = 0;
BOOL isContinuation = NO;
NSRect lineBoundingRect = self.frame;
lineBoundingRect.size.height = kTextBoundingRectHeight;
NSMutableArray *mutableLineIndexes = [NSMutableArray arrayWithCapacity:(textRect.size.height / kTextBoundingRectHeight)];
while (currentIndex * kTextBoundingRectHeight < NSMaxY(textRect)) {
NSRange glyphRange = [layoutManager glyphRangeForBoundingRect:lineBoundingRect inTextContainer:textContainer];
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
GHDiffLineIndexSet *lineIndexSet = [GHDiffLineIndexSet lineIndexSet];
lineIndexSet.textAttributes = self.lineNumberTextAttributes;
currentLine = [diffString substringWithRange:characterRange];
if ([currentLine isFileMarker]) {
lineIndexSet.lineType = GHDiffLineTypeFileMarker;
}
else if ([currentLine isChangeSetMarker]) {
newFileLineIndex = [currentLine newFileChangeSetMarkerIndex] - 1;
lineIndexSet.newFileLineIndex = newFileLineIndex;
lineIndexSet.lineType = GHDiffLineTypeChangeSetMarker;
}
else if ([currentLine isAddition]) {
newFileLineIndex ++;
lineIndexSet.newFileLineIndex = newFileLineIndex;
lineIndexSet.lineType = GHDiffLineTypeAddition;
}
else if ([currentLine isDeletion]) {
lineIndexSet.lineType = GHDiffLineTypeDeletion;
}
else if (isContinuation) {
lineIndexSet.lineType = GHDiffLineTypeContinuation;
}
else {
newFileLineIndex ++;
lineIndexSet.newFileLineIndex = newFileLineIndex;
lineIndexSet.lineType = GHDiffLineTypePreExistingLine;
}
if (newFileLineIndex != NSUIntegerMax && newFileLineIndex > self.highestNewFileLineNumber) {
self.highestNewFileLineNumber = newFileLineIndex;
}
currentIndex ++;
lineIndexSet.realLineIndex = currentIndex;
lineIndexSet.lineRect = lineBoundingRect;
isContinuation = [currentLine hasNewlineTerminator] ? NO : YES;
lineIndexSet.hasNewlineTerminator = isContinuation;
[mutableLineIndexes addObject:lineIndexSet];
lineBoundingRect.origin.y += kTextBoundingRectHeight;
}
self.lineIndexes = mutableLineIndexes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment