Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow reference formatter to detect if range contains custom content key
Browse files Browse the repository at this point in the history
tonisevener committed Dec 15, 2023
1 parent ac6bf63 commit eaac9a7
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
NS_ASSUME_NONNULL_BEGIN

@interface WKSourceEditorFormatterReference : WKSourceEditorFormatter

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isReferenceInRange:(NSRange)range;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -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(?:[^\\/>]+?)?>)(.*?)(<\\/ref>)" options:0 error:nil];
_refOpenRegex = [[NSRegularExpression alloc] initWithPattern:@"<ref(?:[^\\/>]+?)?>" 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<NSAttributedStringKey,id> *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<NSAttributedStringKey,id> * _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

0 comments on commit eaac9a7

Please sign in to comment.