diff --git a/VoiceInk/Services/TextInsertionFormatter.swift b/VoiceInk/Services/TextInsertionFormatter.swift index cf4c11e..9ab1389 100644 --- a/VoiceInk/Services/TextInsertionFormatter.swift +++ b/VoiceInk/Services/TextInsertionFormatter.swift @@ -38,14 +38,22 @@ class TextInsertionFormatter { } let cursorPosition = rangeValue.location - let beforeStart = max(0, cursorPosition - 100) - let textBefore = String(text[text.index(text.startIndex, offsetBy: beforeStart).. 0 ? text[text.index(text.startIndex, offsetBy: cursorPosition - 1)] : nil - let charAfter = cursorPosition < text.count ? text[text.index(text.startIndex, offsetBy: cursorPosition)] : nil + let searchRangeLength = 100 + + let beforeStartIndex = text.index(cursorStringIndex, offsetBy: -searchRangeLength, limitedBy: text.startIndex) ?? text.startIndex + let textBefore = String(text[beforeStartIndex.. text.startIndex ? text[text.index(before: cursorStringIndex)] : nil + let charAfter = cursorStringIndex < text.endIndex ? text[cursorStringIndex] : nil return InsertionContext( textBefore: textBefore, @@ -164,3 +172,4 @@ class TextInsertionFormatter { return false } } +