commit c8f18958a2d4d2ceab095ddcefaa78460cdd9945
parent 3b07a207c408f58b34d939dcb7c6b7b54596b627
Author: William Casarin <jb55@jb55.com>
Date: Wed, 28 Jun 2023 17:08:28 +0200
refactor: cleanup processFocusedWordForMention
Diffstat:
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/damus/Views/TextViewWrapper.swift b/damus/Views/TextViewWrapper.swift
@@ -70,17 +70,21 @@ struct TextViewWrapper: UIViewRepresentable {
}
private func processFocusedWordForMention(textView: UITextView) {
- if let selectedRange = textView.selectedTextRange {
- var val: (String?, NSRange?)
- if let wordRange = textView.tokenizer.rangeEnclosingPosition(selectedRange.start, with: .word, inDirection: .init(rawValue: UITextLayoutDirection.left.rawValue)) {
- if let startPosition = textView.position(from: wordRange.start, offset: -1),
- let cursorPosition = textView.position(from: selectedRange.start, offset: 0) {
- let word = textView.text(in: textView.textRange(from: startPosition, to: cursorPosition)!)
- val = (word, convertToNSRange(startPosition, cursorPosition, textView))
- }
- }
- getFocusWordForMention?(val.0, val.1)
+ var val: (String?, NSRange?) = (nil, nil)
+
+ guard let selectedRange = textView.selectedTextRange else { return }
+
+ let wordRange = textView.tokenizer.rangeEnclosingPosition(selectedRange.start, with: .word, inDirection: .init(rawValue: UITextLayoutDirection.left.rawValue))
+
+ if let wordRange,
+ let startPosition = textView.position(from: wordRange.start, offset: -1),
+ let cursorPosition = textView.position(from: selectedRange.start, offset: 0)
+ {
+ let word = textView.text(in: textView.textRange(from: startPosition, to: cursorPosition)!)
+ val = (word, convertToNSRange(startPosition, cursorPosition, textView))
}
+
+ getFocusWordForMention?(val.0, val.1)
}
private func convertToNSRange( _ startPosition: UITextPosition, _ endPosition: UITextPosition, _ textView: UITextView) -> NSRange? {