@@ -237,25 +237,24 @@ public final class MessageAutocompleteController: MessageTextViewListener {
237
237
238
238
public func willChangeRange( textView: MessageTextView , to range: NSRange ) {
239
239
240
- // range.length > 0: Backspace/removing text
240
+ // range.length == 1: Remove single character
241
241
// range.lowerBound < textView.selectedRange.lowerBound: Ignore trying to delete
242
242
// the substring if the user is already doing so
243
- if range. length > 0 , range. lowerBound < textView. selectedRange. lowerBound {
243
+ if range. length == 1 , range. lowerBound < textView. selectedRange. lowerBound {
244
244
245
245
// Backspace/removing text
246
246
let attribute = textView. attributedText
247
247
. attributes ( at: range. lowerBound, longestEffectiveRange: nil , in: range)
248
248
. filter { return $0. key == NSAttributedAutocompleteKey }
249
-
250
- if ( attribute [ NSAttributedAutocompleteKey] as? Bool ?? false ) == true {
251
-
249
+
250
+ if let isAutocomplete = attribute [ NSAttributedAutocompleteKey] as? Bool , isAutocomplete {
252
251
// Remove the autocompleted substring
253
252
let lowerRange = NSRange ( location: 0 , length: range. location + 1 )
254
253
textView. attributedText. enumerateAttribute ( NSAttributedAutocompleteKey, in: lowerRange, options: . reverse, using: { ( _, range, stop) in
255
-
254
+
256
255
// Only delete the first found range
257
256
defer { stop. pointee = true }
258
-
257
+
259
258
let emptyString = NSAttributedString ( string: " " , attributes: typingTextAttributes)
260
259
textView. attributedText = textView. attributedText. replacingCharacters ( in: range, with: emptyString)
261
260
textView. selectedRange = NSRange ( location: range. location, length: 0 )
0 commit comments