Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: support search in scopes #135

Closed
rebornix opened this issue Jul 30, 2022 · 1 comment · Fixed by #136
Closed

API: support search in scopes #135

rebornix opened this issue Jul 30, 2022 · 1 comment · Fixed by #136
Labels
enhancement New feature or request

Comments

@rebornix
Copy link
Contributor

rebornix commented Jul 30, 2022

Describe the solution you'd like

Supporting range: NSRange in the Search API like snippets below

public struct SearchQuery: Hashable, Equatable {
+    /// The range to search in
+    public let range: NSRange?

-    public init(text: String, matchMethod: MatchMethod = .contains, isCaseSensitive: Bool = false) {
+    public init(text: String, matchMethod: MatchMethod = .contains, isCaseSensitive: Bool = false, range: NSRange? = nil) {

    func matches(in string: NSString) -> [NSTextCheckingResult] {
        do {
            let regex = try NSRegularExpression(pattern: annotatedText, options: regularExpressionOptions)
-            return regex.matches(in: string as String, range: NSRange(location: 0, length: string.length)) 
+            return regex.matches(in: string as String, range: range ?? NSRange(location: 0, length: string.length))
        } catch {
            #if DEBUG
            print(error)
            #endif
            return []
        }
    }

Supporting search in specified ranges would allow embedders to control where the search should happen, making it possible to build features like

  • Search in current block
  • Search in Strings/Comments

Describe alternatives you've considered

We could search in all text and then do the filtering afterwards but that would be less performant.

I can send PRs if this makes sense to you.

@rebornix rebornix added the enhancement New feature or request label Jul 30, 2022
@simonbs
Copy link
Owner

simonbs commented Jul 31, 2022

This sounds like a great addition. I'll be happy to merge a PR that introduces this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants