fix: input emoji make auto complete component crash issue

This commit is contained in:
CMK 2021-06-02 14:59:01 +08:00
parent a44d09d13d
commit 634aef0de3
1 changed files with 16 additions and 14 deletions

View File

@ -864,12 +864,14 @@ extension ComposeViewController: TextEditorViewChangeObserver {
private static func scanAutoCompleteInfo(textEditorView: TextEditorView) -> AutoCompleteInfo? { private static func scanAutoCompleteInfo(textEditorView: TextEditorView) -> AutoCompleteInfo? {
let text = textEditorView.text 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? = { guard textEditorView.selectedRange.location > 0, !text.isEmpty,
var index = text.index(text.startIndex, offsetBy: cursorLocation - 1) 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 { while index > text.startIndex {
let char = text[index] let char = text[index]
if char == "@" || char == "#" || char == ":" { if char == "@" || char == "#" || char == ":" {
@ -886,18 +888,18 @@ extension ComposeViewController: TextEditorViewChangeObserver {
} }
}() }()
guard let highlighStartIndex = _highlighStartIndex else { return nil } guard let highlightStartIndex = _highlightStartIndex else { return nil }
let scanRange = NSRange(highlighStartIndex..<text.endIndex, in: text) let scanRange = NSRange(highlightStartIndex..<text.endIndex, in: text)
guard let match = text.firstMatch(pattern: MastodonRegex.autoCompletePattern, options: [], range: scanRange) else { return nil } guard let match = text.firstMatch(pattern: MastodonRegex.autoCompletePattern, options: [], range: scanRange) else { return nil }
let matchRange = match.range(at: 0) guard let matchRange = Range(match.range(at: 0), in: text) else { return nil }
let matchStartIndex = text.index(text.startIndex, offsetBy: matchRange.location) let matchStartIndex = matchRange.lowerBound
let matchEndIndex = text.index(matchStartIndex, offsetBy: matchRange.length) let matchEndIndex = matchRange.upperBound
guard matchStartIndex == highlighStartIndex, matchEndIndex >= cursorIndex else { return nil } guard matchStartIndex == highlightStartIndex, matchEndIndex >= cursorIndex else { return nil }
let symbolRange = highlighStartIndex..<text.index(after: highlighStartIndex) let symbolRange = highlightStartIndex..<text.index(after: highlightStartIndex)
let symbolString = text[symbolRange] let symbolString = text[symbolRange]
let toCursorRange = highlighStartIndex..<cursorIndex let toCursorRange = highlightStartIndex..<cursorIndex
let toCursorString = text[toCursorRange] let toCursorString = text[toCursorRange]
let toHighlightEndRange = matchStartIndex..<matchEndIndex let toHighlightEndRange = matchStartIndex..<matchEndIndex
let toHighlightEndString = text[toHighlightEndRange] let toHighlightEndString = text[toHighlightEndRange]