From 7576067d0b0429373c727cee29c1baf49c6e11e3 Mon Sep 17 00:00:00 2001 From: rebornix Date: Sun, 31 Jul 2022 10:07:51 -0700 Subject: [PATCH 1/2] Support search in a specific range in the text view --- .../TextView/SearchReplace/SearchQuery.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift b/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift index f170d0a01..c64cb36ac 100644 --- a/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift +++ b/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift @@ -22,10 +22,12 @@ public struct SearchQuery: Hashable, Equatable { /// The text to search for. public let text: String - /// Whether the text is a regular exprssion. + /// Whether the text is a regular expression. public let matchMethod: MatchMethod /// Whether to perform a case-sensitive search. public let isCaseSensitive: Bool + /// A range in the text view that the search should run against. Defaults to `nil` meaning full text view. When set to `nil` the search will run against the full text view. + public let range: NSRange? private var annotatedText: String { switch matchMethod { @@ -57,17 +59,18 @@ public struct SearchQuery: Hashable, Equatable { /// - text: The text to search for. May be a regular expression if `isRegularExpression` is `true`. /// - matchMethod: Strategy to use when matching the search text against the text in the text view. Defaults to `contains`. /// - isCaseSensitive: Whether to perform a case-sensitive search. - public init(text: String, matchMethod: MatchMethod = .contains, isCaseSensitive: Bool = false) { + /// - range: A range in the text view that the search should run against. Defaults to `nil` meaning full text view. + public init(text: String, matchMethod: MatchMethod = .contains, isCaseSensitive: Bool = false, range: NSRange? = nil) { self.text = text self.matchMethod = matchMethod self.isCaseSensitive = isCaseSensitive + self.range = range } func matches(in string: NSString) -> [NSTextCheckingResult] { do { let regex = try NSRegularExpression(pattern: annotatedText, options: regularExpressionOptions) - let range = NSRange(location: 0, length: string.length) - return regex.matches(in: string as String, range: range) + return regex.matches(in: string as String, range: range ?? NSRange(location: 0, length: string.length)) } catch { #if DEBUG print(error) From 079414fff6fd2e16b925cd3601c07ddad9dd6457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=B8vring?= Date: Mon, 1 Aug 2022 09:51:14 +0200 Subject: [PATCH 2/2] Tweaks documentation of --- Sources/Runestone/TextView/SearchReplace/SearchQuery.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift b/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift index c64cb36ac..86231e164 100644 --- a/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift +++ b/Sources/Runestone/TextView/SearchReplace/SearchQuery.swift @@ -26,7 +26,9 @@ public struct SearchQuery: Hashable, Equatable { public let matchMethod: MatchMethod /// Whether to perform a case-sensitive search. public let isCaseSensitive: Bool - /// A range in the text view that the search should run against. Defaults to `nil` meaning full text view. When set to `nil` the search will run against the full text view. + /// A range in the text view that the search should run against. + /// + /// When set to `nil` the search will run against the full text view. Defaults to `nil`. public let range: NSRange? private var annotatedText: String {