From f3c4bcd226f316b9de9c854ff6294190e304d73f Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Fri, 15 Dec 2023 14:23:41 -0600 Subject: [PATCH] Add syntax highlight logic in formatter --- .../WKSourceEditorFormatterStrikethrough.m | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterStrikethrough.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterStrikethrough.m index b5157f83ade..c2349c9b581 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterStrikethrough.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterStrikethrough.m @@ -1,5 +1,70 @@ #import "WKSourceEditorFormatterStrikethrough.h" +#import "WKSourceEditorColors.h" + +@interface WKSourceEditorFormatterStrikethrough () + +@property (nonatomic, strong) NSDictionary *strikethroughAttributes; +@property (nonatomic, strong) NSRegularExpression *strikethroughRegex; + +@end @implementation WKSourceEditorFormatterStrikethrough +- (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEditorFonts *)fonts { + self = [super initWithColors:colors fonts:fonts]; + if (self) { + _strikethroughAttributes = @{ + NSForegroundColorAttributeName: colors.greenForegroundColor, + WKSourceEditorCustomKeyColorGreen: [NSNumber numberWithBool:YES] + }; + + _strikethroughRegex = [[NSRegularExpression alloc] initWithPattern:@"()(\\s*.*?)(<\\/s>)" options:0 error:nil]; + } + + return self; +} + +- (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedString *)attributedString inRange:(NSRange)range { + [self.strikethroughRegex enumerateMatchesInString:attributedString.string + options:0 + range:range + usingBlock:^(NSTextCheckingResult *_Nullable result, NSMatchingFlags flags, BOOL *_Nonnull stop) { + NSRange fullMatch = [result rangeAtIndex:0]; + NSRange openingRange = [result rangeAtIndex:1]; + NSRange contentRange = [result rangeAtIndex:2]; + NSRange closingRange = [result rangeAtIndex:3]; + + if (openingRange.location != NSNotFound) { + [attributedString addAttributes:self.strikethroughAttributes range:openingRange]; + } + + if (closingRange.location != NSNotFound) { + [attributedString addAttributes:self.strikethroughAttributes range:closingRange]; + } + }]; +} + +- (void)updateColors:(WKSourceEditorColors *)colors inAttributedString:(NSMutableAttributedString *)attributedString inRange:(NSRange)range { + + NSMutableDictionary *mutAttributes = [[NSMutableDictionary alloc] initWithDictionary:self.strikethroughAttributes]; + [mutAttributes setObject:colors.greenForegroundColor forKey:NSForegroundColorAttributeName]; + self.strikethroughAttributes = [[NSDictionary alloc] initWithDictionary:mutAttributes]; + + [attributedString enumerateAttribute:WKSourceEditorCustomKeyColorGreen + inRange:range + options:nil + usingBlock:^(id value, NSRange localRange, BOOL *stop) { + if ([value isKindOfClass: [NSNumber class]]) { + NSNumber *numValue = (NSNumber *)value; + if ([numValue boolValue] == YES) { + [attributedString addAttributes:self.strikethroughAttributes range:localRange]; + } + } + }]; +} + +- (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAttributedString *)attributedString inRange:(NSRange)range { + // No special font handling needed for references +} + @end