Guest User

Untitled

a guest
Jul 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. - (void)reloadLineIndexes {
  2.  
  3. NSString *diffString = [[self textStorage] string];
  4.  
  5. if (diffString.length < 1) {
  6. self.highestNewFileLineNumber = 0;
  7. self.lineIndexes = nil;
  8. return;
  9. }
  10.  
  11. NSLayoutManager *layoutManager = [self layoutManager];
  12. NSTextContainer *textContainer = [self textContainer];
  13. NSRect textRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(0, NSUIntegerMax) inTextContainer:textContainer];
  14.  
  15. NSString *currentLine = nil;
  16. NSUInteger currentIndex = 0;
  17. NSUInteger newFileLineIndex = 0;
  18. BOOL isContinuation = NO;
  19.  
  20. NSRect lineBoundingRect = self.frame;
  21. lineBoundingRect.size.height = kTextBoundingRectHeight;
  22.  
  23. NSMutableArray *mutableLineIndexes = [NSMutableArray arrayWithCapacity:(textRect.size.height / kTextBoundingRectHeight)];
  24.  
  25. while (currentIndex * kTextBoundingRectHeight < NSMaxY(textRect)) {
  26.  
  27. NSRange glyphRange = [layoutManager glyphRangeForBoundingRect:lineBoundingRect inTextContainer:textContainer];
  28. NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
  29.  
  30. GHDiffLineIndexSet *lineIndexSet = [GHDiffLineIndexSet lineIndexSet];
  31. lineIndexSet.textAttributes = self.lineNumberTextAttributes;
  32.  
  33. currentLine = [diffString substringWithRange:characterRange];
  34.  
  35. if ([currentLine isFileMarker]) {
  36. lineIndexSet.lineType = GHDiffLineTypeFileMarker;
  37. }
  38.  
  39. else if ([currentLine isChangeSetMarker]) {
  40. newFileLineIndex = [currentLine newFileChangeSetMarkerIndex] - 1;
  41. lineIndexSet.newFileLineIndex = newFileLineIndex;
  42. lineIndexSet.lineType = GHDiffLineTypeChangeSetMarker;
  43. }
  44.  
  45. else if ([currentLine isAddition]) {
  46. newFileLineIndex ++;
  47. lineIndexSet.newFileLineIndex = newFileLineIndex;
  48. lineIndexSet.lineType = GHDiffLineTypeAddition;
  49. }
  50.  
  51. else if ([currentLine isDeletion]) {
  52. lineIndexSet.lineType = GHDiffLineTypeDeletion;
  53. }
  54.  
  55. else if (isContinuation) {
  56. lineIndexSet.lineType = GHDiffLineTypeContinuation;
  57. }
  58.  
  59. else {
  60. newFileLineIndex ++;
  61. lineIndexSet.newFileLineIndex = newFileLineIndex;
  62. lineIndexSet.lineType = GHDiffLineTypePreExistingLine;
  63. }
  64.  
  65. if (newFileLineIndex != NSUIntegerMax && newFileLineIndex > self.highestNewFileLineNumber) {
  66.  
  67. self.highestNewFileLineNumber = newFileLineIndex;
  68. }
  69.  
  70. currentIndex ++;
  71. lineIndexSet.realLineIndex = currentIndex;
  72. lineIndexSet.lineRect = lineBoundingRect;
  73. isContinuation = [currentLine hasNewlineTerminator] ? NO : YES;
  74. lineIndexSet.hasNewlineTerminator = isContinuation;
  75.  
  76. [mutableLineIndexes addObject:lineIndexSet];
  77. lineBoundingRect.origin.y += kTextBoundingRectHeight;
  78. }
  79.  
  80. self.lineIndexes = mutableLineIndexes;
  81. }
Add Comment
Please, Sign In to add comment