From 90481db92ab2dd801b233bcd88ffb39274adf0de Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 3 Apr 2020 18:41:47 -0400 Subject: [PATCH] Use standard macOS colors for PHP syntax highlighting. This overrides the PHP standard colors so that they work better under dark mode. --- CHANGES | 2 ++ Source/BSSourceView.mm | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index c637077..64a91f6 100644 --- 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 ##################### diff --git a/Source/BSSourceView.mm b/Source/BSSourceView.mm index 5914803..7899dfc 100644 --- a/Source/BSSourceView.mm +++ b/Source/BSSourceView.mm @@ -189,6 +189,13 @@ [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. @@ -202,7 +209,14 @@ 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*) { @@ -243,6 +257,7 @@ [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]; } } -- 2.43.5