Skip to content

Commit 172f9ff

Browse files
authored
Improve case insensitive search (#111)
* Improve case insensitive search * Extract methods to extension
1 parent bc14d96 commit 172f9ff

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

MarkEditCore/Sources/Extensions/String+Extension.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public extension String {
2020
func toData(encoding: String.Encoding = .utf8) -> Data? {
2121
data(using: encoding)
2222
}
23+
24+
func equalsIgnoreCase(_ another: String) -> Bool {
25+
caseInsensitiveCompare(another) == .orderedSame
26+
}
27+
28+
func hasPrefixIgnoreCase(_ prefix: String) -> Bool {
29+
range(of: prefix, options: [.anchored, .caseInsensitive]) != nil
30+
}
2331
}
2432

2533
extension String.Encoding {

MarkEditKit/Sources/Bridge/Native/Modules/EditorModuleCompletion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class EditorModuleCompletion: NativeModuleCompletion {
4444
// Figure out the partial word range with one tokenization pass
4545
let from = range.lowerBound.utf16Offset(in: anchor.text)
4646
let to = range.upperBound.utf16Offset(in: anchor.text)
47-
let prefix = anchor.text[range].trimmingCharacters(in: .whitespaces).lowercased()
47+
let prefix = anchor.text[range].trimmingCharacters(in: .whitespaces)
4848

4949
// Figure out all words in the document with more tokenization passes
5050
if let fullText, delegate?.editorCompletionTokenizeWholeDocument(self) == true {
@@ -57,7 +57,7 @@ public final class EditorModuleCompletion: NativeModuleCompletion {
5757
anchor: anchor,
5858
partialRange: NSRange(location: from, length: to - from),
5959
tokenizedWords: (cachedTokens + tokens(in: anchor.text)).filter {
60-
$0.lowercased().hasPrefix(prefix)
60+
$0.hasPrefixIgnoreCase(prefix)
6161
}
6262
)
6363
}

MarkEditMac/Sources/Editor/Controllers/EditorViewController+Completion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ extension EditorViewController {
2727

2828
if AppPreferences.Assistant.wordsInDocument {
2929
// Remove tokens if they "cannot be completed", usually means they are not a word
30-
let isWord = completions.contains { $0.lowercased() == prefix }
31-
completions.append(contentsOf: tokenizedWords.filter { isWord || $0.lowercased() != prefix })
30+
let isWord = completions.contains { $0.equalsIgnoreCase(prefix) }
31+
completions.append(contentsOf: tokenizedWords.filter { isWord || $0 != prefix })
3232
}
3333

3434
if AppPreferences.Assistant.standardWords {

0 commit comments

Comments
 (0)