From b49f3a75f1971bc106b37ad3fd2bc79c242a95d5 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Wed, 6 Dec 2023 12:00:58 -0600 Subject: [PATCH 1/5] Fix vertical template syntax highlight issue --- .../WKSourceEditorFormatterTemplate.m | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m index 5d6272f4a8c..93d5e35edd2 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m @@ -34,7 +34,7 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi }; _horizontalTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"\\{{2}[^\\{\\}\\n]*\\}{2}" options:0 error:nil]; - _verticalStartTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^\\{{2}[^\\{\\}\\n]*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; + _verticalStartTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^(?:.*)(\\{{2}[^\\{\\}\n]*)$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalParameterTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^\\s*\\|.*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalEndTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^([^\\{\\}\n]*\\}{2})(?:.)*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; } @@ -64,11 +64,12 @@ - (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedStri options:0 range:range usingBlock:^(NSTextCheckingResult *_Nullable result, NSMatchingFlags flags, BOOL *_Nonnull stop) { - NSRange matchRange = [result rangeAtIndex:0]; + NSRange fullMatch = [result rangeAtIndex:0]; + NSRange openingTemplateRange = [result rangeAtIndex:1]; - if (matchRange.location != NSNotFound) { - [attributedString addAttributes:self.verticalTemplateAttributes range:matchRange]; - } + if (fullMatch.location != NSNotFound && openingTemplateRange.location != NSNotFound) { + [attributedString addAttributes:self.verticalTemplateAttributes range:openingTemplateRange]; + } }]; [self.verticalParameterTemplateRegex enumerateMatchesInString:attributedString.string @@ -86,12 +87,14 @@ - (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedStri options:0 range:range usingBlock:^(NSTextCheckingResult *_Nullable result, NSMatchingFlags flags, BOOL *_Nonnull stop) { + NSRange fullMatch = [result rangeAtIndex:0]; NSRange closingTemplateRange = [result rangeAtIndex:1]; - if (fullMatch.location != NSNotFound && closingTemplateRange.location != NSNotFound) { - [attributedString addAttributes:self.verticalTemplateAttributes range:closingTemplateRange]; - } + if (fullMatch.location != NSNotFound && closingTemplateRange.location != NSNotFound) { + [attributedString addAttributes:self.verticalTemplateAttributes range:closingTemplateRange]; + } + }]; } From 2e7cc02be407ce93598e2ad3cb80c34e7a15dafa Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Wed, 6 Dec 2023 12:06:20 -0600 Subject: [PATCH 2/5] Add tests --- .../WKSourceEditorFormatterTests.swift | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift index 6330fe0ba7c..9e8aad7edab 100644 --- a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift +++ b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift @@ -630,7 +630,7 @@ final class WKSourceEditorFormatterTests: XCTestCase { XCTAssertEqual(refClosingAttributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect ref formatting") } - func testVerticalStartTemplate() { + func testVerticalStartTemplate1() { let string = "{{Infobox officeholder" let mutAttributedString = NSMutableAttributedString(string: string) @@ -648,6 +648,33 @@ final class WKSourceEditorFormatterTests: XCTestCase { XCTAssertEqual(templateAttributes[.foregroundColor] as! UIColor, colors.purpleForegroundColor, "Incorrect template formatting") } + func testVerticalStartTemplate2() { + let string = "ending of previous sentence. {{cite web" + let mutAttributedString = NSMutableAttributedString(string: string) + + for formatter in formatters { + formatter.addSyntaxHighlighting(to: mutAttributedString, in: NSRange(location: 0, length: string.count)) + } + + var baseRange = NSRange(location: 0, length: 0) + let baseAttributes = mutAttributedString.attributes(at: 0, effectiveRange: &baseRange) + + var templateRange = NSRange(location: 0, length: 0) + let templateAttributes = mutAttributedString.attributes(at: 29, effectiveRange: &templateRange) + + // "ending of previous sentence. " + XCTAssertEqual(baseRange.location, 0, "Incorrect base formatting") + XCTAssertEqual(baseRange.length, 29, "Incorrect base formatting") + XCTAssertEqual(baseAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(baseAttributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + + // "ending of previous sentence. " + XCTAssertEqual(templateRange.location, 29, "Incorrect template formatting") + XCTAssertEqual(templateRange.length, 10, "Incorrect template formatting") + XCTAssertEqual(templateAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect template formatting") + XCTAssertEqual(templateAttributes[.foregroundColor] as! UIColor, colors.purpleForegroundColor, "Incorrect base formatting") + } + func testVerticalParameterTemplate() { let string = "| genus = Felis" let mutAttributedString = NSMutableAttributedString(string: string) From 16ee5ba2d29497e19b2a385c5fceaee19de80e44 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Thu, 7 Dec 2023 15:31:15 -0600 Subject: [PATCH 3/5] Undo line break character regression --- .../Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m index 93d5e35edd2..9083a4c4473 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m @@ -34,7 +34,7 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi }; _horizontalTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"\\{{2}[^\\{\\}\\n]*\\}{2}" options:0 error:nil]; - _verticalStartTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^(?:.*)(\\{{2}[^\\{\\}\n]*)$" options:NSRegularExpressionAnchorsMatchLines error:nil]; + _verticalStartTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^(?:.*)(\\{{2}[^\\{\\}\\n]*)$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalParameterTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^\\s*\\|.*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalEndTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^([^\\{\\}\n]*\\}{2})(?:.)*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; } From 3553ec8f5f3c57d72d8549fc495406bd1d303ef0 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Thu, 7 Dec 2023 15:58:19 -0600 Subject: [PATCH 4/5] Syntax highlight nested templates --- .../Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m index 9083a4c4473..b103dcd931e 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterTemplate.m @@ -33,7 +33,7 @@ - (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEdi WKSourceEditorCustomKeyVerticalTemplate: [NSNumber numberWithBool:YES] }; - _horizontalTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"\\{{2}[^\\{\\}\\n]*\\}{2}" options:0 error:nil]; + _horizontalTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"\\{{2}[^\\{\\}\\n]*(?:\\{{2}[^\\{\\}\\n]*\\}{2})*[^\\{\\}\\n]*\\}{2}" options:0 error:nil]; _verticalStartTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^(?:.*)(\\{{2}[^\\{\\}\\n]*)$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalParameterTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^\\s*\\|.*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; _verticalEndTemplateRegex = [[NSRegularExpression alloc] initWithPattern:@"^([^\\{\\}\n]*\\}{2})(?:.)*$" options:NSRegularExpressionAnchorsMatchLines error:nil]; From 91eac86d9179c5af42d9937e45d5a91f0713e353 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Thu, 7 Dec 2023 16:03:34 -0600 Subject: [PATCH 5/5] Add tests --- .../WKSourceEditorFormatterTests.swift | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift index 9e8aad7edab..a112c5a527c 100644 --- a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift +++ b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift @@ -630,6 +630,42 @@ final class WKSourceEditorFormatterTests: XCTestCase { XCTAssertEqual(refClosingAttributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect ref formatting") } + func testHorizontalNestedTemplate() { + let string = "Ford Island ({{lang-haw|Poka {{okina}}Ailana}}) is an" + let mutAttributedString = NSMutableAttributedString(string: string) + + for formatter in formatters { + formatter.addSyntaxHighlighting(to: mutAttributedString, in: NSRange(location: 0, length: string.count)) + } + + var base1Range = NSRange(location: 0, length: 0) + let base1Attributes = mutAttributedString.attributes(at: 0, effectiveRange: &base1Range) + + var templateRange = NSRange(location: 0, length: 0) + let templateAttributes = mutAttributedString.attributes(at: 13, effectiveRange: &templateRange) + + var base2Range = NSRange(location: 0, length: 0) + let base2Attributes = mutAttributedString.attributes(at: 46, effectiveRange: &base2Range) + + // "Ford Island (" + XCTAssertEqual(base1Range.location, 0, "Incorrect base formatting") + XCTAssertEqual(base1Range.length, 13, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + + // "{{lang-haw|Poka {{okina}}Ailana}}" + XCTAssertEqual(templateRange.location, 13, "Incorrect template formatting") + XCTAssertEqual(templateRange.length, 33, "Incorrect template formatting") + XCTAssertEqual(templateAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect template formatting") + XCTAssertEqual(templateAttributes[.foregroundColor] as! UIColor, colors.purpleForegroundColor, "Incorrect template formatting") + + // ") is an" + XCTAssertEqual(base2Range.location, 46, "Incorrect base formatting") + XCTAssertEqual(base2Range.length, 7, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + } + func testVerticalStartTemplate1() { let string = "{{Infobox officeholder" let mutAttributedString = NSMutableAttributedString(string: string)