diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index 69fef0a51..48db5df7a 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -864,12 +864,14 @@ extension ComposeViewController: TextEditorViewChangeObserver { private static func scanAutoCompleteInfo(textEditorView: TextEditorView) -> AutoCompleteInfo? { let text = textEditorView.text - let cursorLocation = textEditorView.selectedRange.location - let cursorIndex = text.index(text.startIndex, offsetBy: cursorLocation) - guard cursorLocation > 0, !text.isEmpty else { return nil } - - let _highlighStartIndex: String.Index? = { - var index = text.index(text.startIndex, offsetBy: cursorLocation - 1) + + guard textEditorView.selectedRange.location > 0, !text.isEmpty, + let selectedRange = Range(textEditorView.selectedRange, in: text) else { + return nil + } + let cursorIndex = selectedRange.upperBound + let _highlightStartIndex: String.Index? = { + var index = text.index(before: cursorIndex) while index > text.startIndex { let char = text[index] if char == "@" || char == "#" || char == ":" { @@ -886,18 +888,18 @@ extension ComposeViewController: TextEditorViewChangeObserver { } }() - guard let highlighStartIndex = _highlighStartIndex else { return nil } - let scanRange = NSRange(highlighStartIndex..= cursorIndex else { return nil } - let symbolRange = highlighStartIndex..= cursorIndex else { return nil } + let symbolRange = highlightStartIndex..