From caf6ecfbfd12ba349667afd1dde4911abaa2abef Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 15 Sep 2019 09:42:22 -0400 Subject: [PATCH] In BSSourceView, keep the NSAttributedString creation on the background block. Only post the AppKit calls to the main thread. --- Source/BSSourceView.mm | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Source/BSSourceView.mm b/Source/BSSourceView.mm index 5b922ef..337d7b6 100644 --- a/Source/BSSourceView.mm +++ b/Source/BSSourceView.mm @@ -192,20 +192,27 @@ [task setStandardOutput:outPipe]; [task setStandardError:errPipe]; [task setTerminationHandler:^(NSTask*) { + NSMutableAttributedString* source; + + if (task.terminationStatus == 0) { + NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile]; + source = + [[NSMutableAttributedString alloc] initWithHTML:data + options:@{ NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) } + documentAttributes:nil]; + + // PHP uses   in the highlighted output, which should be converted + // back to normal spaces. + NSMutableString* stringData = [source mutableString]; + [stringData replaceOccurrencesOfString:@"\u00A0" withString:@" " options:0 range:NSMakeRange(0, stringData.length)]; + } else { + NSLog(@"Failed to highlight PHP file %@: %@", filePath, [[errPipe fileHandleForReading] readDataToEndOfFile]); + } + dispatch_async(dispatch_get_main_queue(), ^{ - if (task.terminationStatus == 0) { - NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile]; - NSMutableAttributedString* source = - [[NSMutableAttributedString alloc] initWithHTML:data - options:@{ NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) } - documentAttributes:nil]; - NSMutableString* stringData = [source mutableString]; - // PHP uses   in the highlighted output, which should be converted - // back to normal spaces. - [stringData replaceOccurrencesOfString:@"\u00A0" withString:@" " options:0 range:NSMakeRange(0, stringData.length)]; + if (source) { [[self->textView_ textStorage] setAttributedString:source]; } else { - NSLog(@"Failed to highlight PHP file %@: %@", filePath, [[errPipe fileHandleForReading] readDataToEndOfFile]); [self setPlainTextStringFromFile:filePath]; } -- 2.43.5