diff --git a/sources/SquirrelPanel.swift b/sources/SquirrelPanel.swift index b4379f7cf..1323a2472 100644 --- a/sources/SquirrelPanel.swift +++ b/sources/SquirrelPanel.swift @@ -222,8 +222,12 @@ final class SquirrelPanel: NSPanel { let comment = comments[i].precomposedStringWithCanonicalMapping let line = NSMutableAttributedString(string: theme.candidateFormat, attributes: labelAttrs) - line.addAttributes(attrs, range: (line.string as NSString).range(of: "[candidate]")) - line.addAttributes(commentAttrs, range: (line.string as NSString).range(of: "[comment]")) + for range in line.string.ranges(of: /\[candidate\]/) { + line.addAttributes(attrs, range: convert(range: range, in: line.string)) + } + for range in line.string.ranges(of: /\[comment\]/) { + line.addAttributes(commentAttrs, range: convert(range: range, in: line.string)) + } line.mutableString.replaceOccurrences(of: "[label]", with: label, range: NSMakeRange(0, line.length)) let labeledLine = line.copy() as! NSAttributedString line.mutableString.replaceOccurrences(of: "[candidate]", with: candidate, range: NSMakeRange(0, line.length)) @@ -428,4 +432,10 @@ private extension SquirrelPanel { self.hide() } } + + func convert(range: Range, in string: String) -> NSRange { + let startPos = range.lowerBound.utf16Offset(in: string) + let endPos = range.upperBound.utf16Offset(in: string) + return NSRange(location: startPos, length: endPos - startPos) + } }