Use standard macOS colors for PHP syntax highlighting.
authorRobert Sesek <rsesek@bluestatic.org>
Fri, 3 Apr 2020 22:41:47 +0000 (18:41 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Fri, 3 Apr 2020 22:41:47 +0000 (18:41 -0400)
This overrides the PHP standard colors so that they work better under
dark mode.

CHANGES
Source/BSSourceView.mm

diff --git a/CHANGES b/CHANGES
index c6370770fc0d0acf9b9789786782958dfedf8453..64a91f68db5864fb3747e051a43e987b1c1b28f3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,8 @@ MacGDBp                                                               CHANGE LOG
 #####################
 - Change: Draw the source view gutter in a way that is dark-mode compatible.
 - Change: Make the Remote Paths preferences pane larger.
+- Fix: #260  Use macOS standard colors for syntax highlighting for better dark-
+  mode compatibility.
 
 2.0 Beta 1
 #####################
index 591480369316b3afa6e0109673724f080ded18d7..7899dfc1ad341dcea8461880dbfa95841c011675 100644 (file)
   [self registerForDraggedTypes:types];
 }
 
+NSString* ColorHEXStringINIDirective(NSString* directive, NSColor* color) {
+  CGFloat red, green, blue, alpha;
+  color = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
+  [color getRed:&red green:&green blue:&blue alpha:&alpha];
+  return [NSString stringWithFormat:@"%@=\"#%02x%02x%02x\"", directive, (UInt8)(red * 255), (UInt8)(green * 255), (UInt8)(blue * 255)];
+}
+
 /**
  * Reads the contents of |filePath| and sets it as the displayed text, after
  * attempting to highlight it using the PHP binary.
     NSTask* task = [[NSTask alloc] init];
 
     [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
-    [task setArguments:@[ @"-s", filePath ]];
+    [task setArguments:@[
+      @"--syntax-highlight",
+      @"--define", ColorHEXStringINIDirective(@"highlight.string", [NSColor systemRedColor]),
+      @"--define", ColorHEXStringINIDirective(@"highlight.comment", [NSColor systemOrangeColor]),
+      @"--define", ColorHEXStringINIDirective(@"highlight.default", [NSColor systemBlueColor]),
+      @"--define", ColorHEXStringINIDirective(@"highlight.html", [NSColor systemGrayColor]),
+      filePath
+    ]];
     [task setStandardOutput:outPipe];
     [task setStandardError:errPipe];
     [task setTerminationHandler:^(NSTask*) {
     [task launch];
   } @catch (NSException* exception) {
     // If the PHP executable is not available then the NSTask will throw an exception
+    NSLog(@"Failed to highlight file: %@", exception);
     [self setPlainTextStringFromFile:filePath];
   }
 }