@@ -18,6 +18,7 @@ @interface SCMiniMapView () <NSLayoutManagerDelegate>
1818
1919@property (nonatomic , strong ) NSColor *backgroundColor;
2020@property (nonatomic , strong ) NSFont *font;
21+ @property (nonatomic , readwrite ) NSInteger lastCalculatedLines;
2122
2223@end
2324
@@ -185,29 +186,53 @@ - (void)updateTextView
185186 return ;
186187 }
187188
188- NSMutableAttributedString *mutableAttributedString = [self .editorTextView.textStorage mutableCopy ];
189+ typeof (self) __weak weakSelf = self;
190+ NSOperationQueue *background = [[NSOperationQueue alloc ] init ];
189191
190- if (mutableAttributedString.length == 0 ) {
191- return ;
192- }
193-
194- __block NSMutableParagraphStyle *style;
195-
196- [mutableAttributedString enumerateAttribute: NSParagraphStyleAttributeName
197- inRange: NSMakeRange (0 , mutableAttributedString.length)
198- options: 0
199- usingBlock: ^(id value, NSRange range, BOOL *stop) {
200- style = [value mutableCopy ];
201- *stop = YES ;
202- }];
203-
204-
205- [style setTabStops: @[]];
206- [style setDefaultTabInterval: style.defaultTabInterval * kDefaultZoomLevel ];
207-
208- [mutableAttributedString setAttributes: @{NSFontAttributeName : self.font , NSParagraphStyleAttributeName : style} range: NSMakeRange (0 , mutableAttributedString.length)];
192+ // Run the attributed strings operation on a background thread
193+ [background addOperationWithBlock: ^{
194+ NSMutableAttributedString *mutableAttributedString = [self .editorTextView.textStorage mutableCopy ];
195+
196+ if (mutableAttributedString.length == 0 ) {
197+ // Nothing to do here.
198+ weakSelf.lastCalculatedLines = 0 ;
199+ return ;
200+ }
201+
202+ __block NSMutableParagraphStyle *style;
203+
204+ [mutableAttributedString enumerateAttribute: NSParagraphStyleAttributeName
205+ inRange: NSMakeRange (0 , mutableAttributedString.length)
206+ options: 0
207+ usingBlock: ^(id value, NSRange range, BOOL *stop) {
208+ style = [value mutableCopy ];
209+ *stop = YES ;
210+ }];
211+
212+
213+ [style setTabStops: @[]];
214+ [style setDefaultTabInterval: style.defaultTabInterval * kDefaultZoomLevel ];
215+
216+ [mutableAttributedString setAttributes: @{NSFontAttributeName : weakSelf.font , NSParagraphStyleAttributeName : style} range: NSMakeRange (0 , mutableAttributedString.length)];
217+
218+ // Send the text storage update off to the main thread
219+ [[NSOperationQueue mainQueue ] addOperationWithBlock: ^{
220+ [weakSelf.textView.textStorage setAttributedString: mutableAttributedString];
221+
222+ }];
223+
224+ // Now that we've sent that off, let's see how many lines we have while
225+ // we're in here:
226+ [weakSelf calculateLinesFromString: [mutableAttributedString string ]];
227+ }];
228+ }
229+
230+ - (void )calculateLinesFromString : (NSString *)string
231+ {
232+ NSArray *lines = [string componentsSeparatedByString: @" \n " ];
209233
210- [self .textView.textStorage setAttributedString: mutableAttributedString];
234+ // Cache the last calculated lines so we can figure out how long we should take to render this minimap.
235+ self.lastCalculatedLines = lines.count ;
211236}
212237
213238- (void )resizeWithOldSuperviewSize : (NSSize )oldSize
0 commit comments