From defe94cd5eff33491fb2f00bcda8c2fc1608c5eb Mon Sep 17 00:00:00 2001 From: Daniele Cattaneo Date: Sun, 1 Mar 2015 22:30:30 +0100 Subject: [PATCH] When over-colouring an already coloured block, remove the old colouring entirely (not only the over-coloured range) if the two colouring types can't be nested. Fixes #22. --- SMLSyntaxColouring.m | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/SMLSyntaxColouring.m b/SMLSyntaxColouring.m index 7a5b42c6..e5747217 100644 --- a/SMLSyntaxColouring.m +++ b/SMLSyntaxColouring.m @@ -1142,10 +1142,26 @@ - (void)colourSecondStrings2InRange:(NSRange)rangeToRecolour withRangeScanner:(N /* * - setColour:range: - */ - (void)setColour:(NSDictionary *)colourDictionary range:(NSRange)range { + NSRange effectiveRange = NSMakeRange(0,0); + NSRange bounds = NSMakeRange(0, [[layoutManager textStorage] length]); + NSUInteger i = range.location; + NSString *attr; + NSSet *overlapSet = [NSSet setWithObjects:SMLSyntaxGroupCommand, nil]; + + while (NSLocationInRange(i, range)) { + attr = [layoutManager temporaryAttribute:SMLSyntaxGroup atCharacterIndex:i + longestEffectiveRange:&effectiveRange inRange:bounds]; + if (![overlapSet containsObject:attr]) { + [layoutManager removeTemporaryAttribute:NSForegroundColorAttributeName + forCharacterRange:effectiveRange]; + [layoutManager removeTemporaryAttribute:SMLSyntaxGroup + forCharacterRange:effectiveRange]; + } + i = NSMaxRange(effectiveRange); + } [layoutManager addTemporaryAttributes:colourDictionary forCharacterRange:range]; } @@ -1153,25 +1169,30 @@ - (void)setColour:(NSDictionary *)colourDictionary range:(NSRange)range /* * - applyColourDefaults */ + +#define NSCOLOR_FROM_USER_DEFAULTS(name) [NSUnarchiver \ + unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] \ + objectForKey:name]] + - (void)applyColourDefaults { - commandsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsCommandsColourWell]], NSForegroundColorAttributeName, nil]; + commandsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsCommandsColourWell), SMLSyntaxGroup: SMLSyntaxGroupCommand}; - commentsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsCommentsColourWell]], NSForegroundColorAttributeName, nil]; + commentsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsCommentsColourWell), SMLSyntaxGroup: @"comments"}; - instructionsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsInstructionsColourWell]], NSForegroundColorAttributeName, nil]; + instructionsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsInstructionsColourWell), SMLSyntaxGroup: SMLSyntaxGroupInstruction}; - keywordsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsKeywordsColourWell]], NSForegroundColorAttributeName, nil]; + keywordsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsKeywordsColourWell), SMLSyntaxGroup: SMLSyntaxGroupKeyword}; - autocompleteWordsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsAutocompleteColourWell]], NSForegroundColorAttributeName, nil]; + autocompleteWordsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsAutocompleteColourWell), SMLSyntaxGroup: SMLSyntaxGroupAutoComplete}; - stringsColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsStringsColourWell]], NSForegroundColorAttributeName, nil]; + stringsColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsStringsColourWell), SMLSyntaxGroup: @"strings"}; - variablesColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsVariablesColourWell]], NSForegroundColorAttributeName, nil]; + variablesColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsVariablesColourWell), SMLSyntaxGroup: SMLSyntaxGroupVariable}; - attributesColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsAttributesColourWell]], NSForegroundColorAttributeName, nil]; + attributesColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsAttributesColourWell), SMLSyntaxGroup: SMLSyntaxGroupAttribute}; - numbersColour = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[SMLDefaults valueForKey:MGSFragariaPrefsNumbersColourWell]], NSForegroundColorAttributeName, nil]; + numbersColour = @{NSForegroundColorAttributeName : NSCOLOR_FROM_USER_DEFAULTS(MGSFragariaPrefsNumbersColourWell), SMLSyntaxGroup: SMLSyntaxGroupNumber}; [self removeAllColours]; }