Prevent inserting extra whitespace when autocompleting
This commit is contained in:
parent
58c6d508ec
commit
0687c040a0
|
@ -75,6 +75,15 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
|
|||
let selectedRangeStartUTF16 = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
|
||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: selectedRangeStartUTF16)
|
||||
|
||||
let insertSpace: Bool
|
||||
if text.distance(from: characterBeforeCursorIndex, to: text.endIndex) > 0 {
|
||||
let charAfterCursor = text[text.index(after: characterBeforeCursorIndex)]
|
||||
insertSpace = charAfterCursor != " " && charAfterCursor != "\n"
|
||||
} else {
|
||||
insertSpace = true
|
||||
}
|
||||
let string = insertSpace ? string + " " : string
|
||||
|
||||
textField.text!.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||
didChange(textField)
|
||||
|
||||
|
|
|
@ -243,6 +243,15 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
|
|||
|
||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: textView.selectedRange.upperBound)
|
||||
|
||||
let insertSpace: Bool
|
||||
if text.distance(from: characterBeforeCursorIndex, to: text.endIndex) > 0 {
|
||||
let charAfterCursor = text[text.index(after: characterBeforeCursorIndex)]
|
||||
insertSpace = charAfterCursor != " " && charAfterCursor != "\n"
|
||||
} else {
|
||||
insertSpace = true
|
||||
}
|
||||
let string = insertSpace ? string + " " : string
|
||||
|
||||
textView.text.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||
self.textViewDidChange(textView)
|
||||
|
||||
|
|
Loading…
Reference in New Issue