From 0687c040a05505acfe1e7babeec416116bfbfe3e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 12 Oct 2020 22:03:50 -0400 Subject: [PATCH] Prevent inserting extra whitespace when autocompleting --- Tusker/Screens/Compose/ComposeAutocompleteView.swift | 6 +++--- .../Screens/Compose/ComposeContentWarningTextField.swift | 9 +++++++++ Tusker/Screens/Compose/MainComposeTextView.swift | 9 +++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeAutocompleteView.swift b/Tusker/Screens/Compose/ComposeAutocompleteView.swift index 7cf05b004e..000be70362 100644 --- a/Tusker/Screens/Compose/ComposeAutocompleteView.swift +++ b/Tusker/Screens/Compose/ComposeAutocompleteView.swift @@ -73,7 +73,7 @@ struct ComposeAutocompleteMentionsView: View { HStack(spacing: 8) { ForEach(accounts, id: \.id) { (account) in Button { - uiState.autocompleteHandler?.autocomplete(with: "@\(account.acct) ") + uiState.autocompleteHandler?.autocomplete(with: "@\(account.acct)") } label: { HStack(spacing: 4) { ComposeAvatarImageView(url: account.avatar) @@ -210,7 +210,7 @@ struct ComposeAutocompleteEmojisView: View { HStack(spacing: 8) { ForEach(emojis, id: \.shortcode) { (emoji) in Button { - uiState.autocompleteHandler?.autocomplete(with: ":\(emoji.shortcode): ") + uiState.autocompleteHandler?.autocomplete(with: ":\(emoji.shortcode):") } label: { HStack(spacing: 4) { CustomEmojiImageView(emoji: emoji) @@ -266,7 +266,7 @@ struct ComposeAutocompleteHashtagsView: View { HStack(spacing: 8) { ForEach(hashtags, id: \.name) { (hashtag) in Button { - uiState.autocompleteHandler?.autocomplete(with: "#\(hashtag.name) ") + uiState.autocompleteHandler?.autocomplete(with: "#\(hashtag.name)") } label: { Text(verbatim: "#\(hashtag.name)") .foregroundColor(Color(UIColor.label)) diff --git a/Tusker/Screens/Compose/ComposeContentWarningTextField.swift b/Tusker/Screens/Compose/ComposeContentWarningTextField.swift index 72cf26aa12..05ddb15f69 100644 --- a/Tusker/Screens/Compose/ComposeContentWarningTextField.swift +++ b/Tusker/Screens/Compose/ComposeContentWarningTextField.swift @@ -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.. 0 { + let charAfterCursor = text[text.index(after: characterBeforeCursorIndex)] + insertSpace = charAfterCursor != " " && charAfterCursor != "\n" + } else { + insertSpace = true + } + let string = insertSpace ? string + " " : string + textView.text.replaceSubrange(lastWordStartIndex..