Coder Social home page Coder Social logo

Comments (5)

jameshays avatar jameshays commented on July 26, 2024

This is my solution at the moment.

internal extension UITextView {
    
    func find(prefixes: Set<String>, with delimiterSet: CharacterSet) -> (prefix: String, word: String, range: NSRange)? {
        guard prefixes.count > 0
            else { return nil }
        
        return prefixes.compactMap({
            guard let prefix = $0.first else { return nil }
            return find(prefix: prefix, with: delimiterSet)
        }).last
    }
    
    func find(prefix: Character, with delimiterSet: CharacterSet) -> (prefix: String, word: String, range: NSRange)? {
        guard let caretRange = self.caretRange,
            let cursorRange = Range(caretRange, in: text) else { return nil }
        
        var substring = text[..<cursorRange.upperBound]
        guard let prefixIndex = substring.lastIndex(of: prefix) else { return nil }
        
        let wordRange: Range = prefixIndex..<cursorRange.upperBound
        substring = substring[wordRange]
        
        let location = wordRange.lowerBound.encodedOffset
        let length = wordRange.upperBound.encodedOffset - location
        let range = NSRange(location: location, length: length)
        
        return (String(prefix), String(substring), range)
    }
    
    func wordAtCaret(with delimiterSet: CharacterSet) -> (word: String, range: NSRange)? {
        guard let caretRange = self.caretRange,
            let result = text.word(at: caretRange, with: delimiterSet)
            else { return nil }
        
        let location = result.range.lowerBound.encodedOffset
        let range = NSRange(location: location, length: result.range.upperBound.encodedOffset - location)
        
        return (result.word, range)
    }
    
    var caretRange: NSRange? {
        guard let selectedRange = self.selectedTextRange else { return nil }
        return NSRange(
            location: offset(from: beginningOfDocument, to: selectedRange.start),
            length: offset(from: selectedRange.start, to: selectedRange.end)
        )
    }
    
}

After more testing, I may create a PR for it.

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

@jameshays Thanks for helping out with this! Tbh I was in a crunch when I needed this for a project of mine and will be the first to admit it was not robust enough for all cases, but it worked for what I needed

I would accept PRs

from inputbaraccessoryview.

jameshays avatar jameshays commented on July 26, 2024

In the autocomleteManager, I have this as well.

    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        // Ensure that the text to be inserted is not using previous attributes
        preserveTypingAttributes()
        
        if let session = currentSession {
            let textToReplace = (textView.text as NSString).substring(with: range)
            let deleteSpaceCount = textToReplace.filter { $0 == .space }.count
            let insertSpaceCount = text.filter { $0 == .space }.count
            let spaceCountDiff = insertSpaceCount - deleteSpaceCount
            session.spaceCounter = spaceCountDiff
        }
...

session.spaceCount = spaceCountDiff. it doesn't seem to always be accurate when deleting spaces and I haven't worked through that yet. It seems to be working for now though. More testing is probably warranted.

from inputbaraccessoryview.

jameshays avatar jameshays commented on July 26, 2024

I created a PR here. #50

Give it a go and let me know what you find.

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

Fixed in 4.2.1

from inputbaraccessoryview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.