In BSSourceView, keep the NSAttributedString creation on the background block.
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 15 Sep 2019 13:42:22 +0000 (09:42 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 15 Sep 2019 14:04:44 +0000 (10:04 -0400)
Only post the AppKit calls to the main thread.

Source/BSSourceView.mm

index 5b922ef993960bd401bbf50b2ae3d9b260a84dd3..337d7b6fb4ef9e38551bbdf209bf3371693032b0 100644 (file)
     [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 &nbsp; 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 &nbsp; 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];
         }