Skip to content

Commit

Permalink
Allow formatter to detect if range contains custom content key
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisevener committed Dec 15, 2023
1 parent 9ea3aca commit abd42fb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface WKSourceEditorFormatterStrikethrough : WKSourceEditorFormatter

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isStrikethroughInRange:(NSRange)range;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
@interface WKSourceEditorFormatterStrikethrough ()

@property (nonatomic, strong) NSDictionary *strikethroughAttributes;
@property (nonatomic, strong) NSDictionary *strikethroughContentAttributes;
@property (nonatomic, strong) NSRegularExpression *strikethroughRegex;

@end

@implementation WKSourceEditorFormatterStrikethrough

#pragma mark - Custom Attributed String Keys

NSString * const WKSourceEditorCustomKeyContentStrikethrough = @"WKSourceEditorCustomKeyContentStrikethrough";

- (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEditorFonts *)fonts {
self = [super initWithColors:colors fonts:fonts];
if (self) {
Expand All @@ -18,13 +23,21 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi
WKSourceEditorCustomKeyColorGreen: [NSNumber numberWithBool:YES]
};

_strikethroughContentAttributes = @{
WKSourceEditorCustomKeyContentStrikethrough: [NSNumber numberWithBool:YES]
};

_strikethroughRegex = [[NSRegularExpression alloc] initWithPattern:@"(<s>)(\\s*.*?)(<\\/s>)" options:0 error:nil];
}

return self;
}

- (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedString *)attributedString inRange:(NSRange)range {

// Reset
[attributedString removeAttribute:WKSourceEditorCustomKeyContentStrikethrough range:range];

[self.strikethroughRegex enumerateMatchesInString:attributedString.string
options:0
range:range
Expand All @@ -37,6 +50,10 @@ - (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedStri
if (openingRange.location != NSNotFound) {
[attributedString addAttributes:self.strikethroughAttributes range:openingRange];
}

if (contentRange.location != NSNotFound) {
[attributedString addAttributes:self.strikethroughContentAttributes range:contentRange];
}

if (closingRange.location != NSNotFound) {
[attributedString addAttributes:self.strikethroughAttributes range:closingRange];
Expand Down Expand Up @@ -67,4 +84,48 @@ - (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAt
// No special font handling needed for references
}

#pragma mark - Public

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isStrikethroughInRange:(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[WKSourceEditorCustomKeyContentStrikethrough] != 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[WKSourceEditorCustomKeyContentStrikethrough] != nil) {
isContentKey = YES;
}
}
}
}

} else {
__block NSRange unionRange = NSMakeRange(NSNotFound, 0);
[attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) {
if (attrs[WKSourceEditorCustomKeyContentStrikethrough] != nil) {
if (unionRange.location == NSNotFound) {
unionRange = loopRange;
} else {
unionRange = NSUnionRange(unionRange, loopRange);
}
stop = YES;
}
}];

if (NSEqualRanges(unionRange, range)) {
isContentKey = YES;
}
}

return isContentKey;
}

@end

0 comments on commit abd42fb

Please sign in to comment.