diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.h b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.h index 56e5d9ab4b0..af03eb7ef19 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.h +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.h @@ -3,7 +3,7 @@ NS_ASSUME_NONNULL_BEGIN @interface WKSourceEditorFormatterReference : WKSourceEditorFormatter - +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isReferenceInRange:(NSRange)range; @end NS_ASSUME_NONNULL_END diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.m index ea9e3996885..2233791b433 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterReference.m @@ -4,6 +4,7 @@ @interface WKSourceEditorFormatterReference () @property (nonatomic, strong) NSDictionary *refAttributes; @property (nonatomic, strong) NSDictionary *refEmptyAttributes; +@property (nonatomic, strong) NSDictionary *refContentAttributes; @property (nonatomic, strong) NSRegularExpression *refOpenAndCloseRegex; @property (nonatomic, strong) NSRegularExpression *refOpenRegex; @@ -12,6 +13,13 @@ @interface WKSourceEditorFormatterReference () @end @implementation WKSourceEditorFormatterReference + +#pragma mark - Custom Attributed String Keys + +NSString * const WKSourceEditorCustomKeyContentReference = @"WKSourceEditorCustomKeyContentReference"; + +#pragma mark - Overrides + - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEditorFonts *)fonts { self = [super initWithColors:colors fonts:fonts]; if (self) { @@ -25,6 +33,10 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi WKSourceEditorCustomKeyColorGreen: [NSNumber numberWithBool:YES] }; + _refContentAttributes = @{ + WKSourceEditorCustomKeyContentReference: [NSNumber numberWithBool:YES] + }; + _refOpenAndCloseRegex = [[NSRegularExpression alloc] initWithPattern:@"(]+?)?>)(.*?)(<\\/ref>)" options:0 error:nil]; _refOpenRegex = [[NSRegularExpression alloc] initWithPattern:@"]+?)?>" options:0 error:nil]; _refCloseRegex = [[NSRegularExpression alloc] initWithPattern:@"<\\/ref>" options:0 error:nil]; @@ -34,10 +46,11 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi return self; } -#pragma mark - Overrides - - (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedString *)attributedString inRange:(NSRange)range { + // Reset + [attributedString removeAttribute:WKSourceEditorCustomKeyContentReference range:range]; + [self.refOpenAndCloseRegex enumerateMatchesInString:attributedString.string options:0 range:range @@ -51,6 +64,10 @@ - (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedStri [attributedString addAttributes:self.refAttributes range:openingRange]; } + if (contentRange.location != NSNotFound) { + [attributedString addAttributes:self.refContentAttributes range:contentRange]; + } + if (closingRange.location != NSNotFound) { [attributedString addAttributes:self.refAttributes range:closingRange]; } @@ -114,4 +131,41 @@ - (void)updateColors:(WKSourceEditorColors *)colors inAttributedString:(NSMutabl - (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAttributedString *)attributedString inRange:(NSRange)range { // No special font handling needed for references } + +#pragma mark - Public + +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isReferenceInRange:(NSRange)range { + __block BOOL isContentKey = NO; + + if (range.length == 0) { + + if (attributedString.length > range.location) { + NSDictionary *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil]; + + if (attrs[WKSourceEditorCustomKeyContentReference] != nil) { + isContentKey = YES; + } else { + // Edge case, check previous character if we are up against closing string + if (attrs[WKSourceEditorCustomKeyColorGreen]) { + attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil]; + if (attrs[WKSourceEditorCustomKeyContentReference] != nil) { + isContentKey = YES; + } + } + } + } + + } else { + [attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) { + if ((attrs[WKSourceEditorCustomKeyContentReference] != nil) && + (loopRange.location == range.location && loopRange.length == range.length)) { + isContentKey = YES; + stop = YES; + } + }]; + } + + return isContentKey; +} + @end