@@ -20,7 +20,7 @@ final class SquirrelPanel: NSPanel {
2020 private var statusTimer : Timer ?
2121
2222 private var preedit : String = " "
23- private var selRange : NSRange = . init ( location : NSNotFound , length : 0 )
23+ private var selRange : NSRange = . empty
2424 private var caretPos : Int = 0
2525 private var candidates : [ String ] = . init( )
2626 private var comments : [ String ] = . init( )
@@ -171,32 +171,26 @@ final class SquirrelPanel: NSPanel {
171171 currentScreen ( )
172172
173173 let text = NSMutableAttributedString ( )
174- var preeditRange = NSRange ( location : NSNotFound , length : 0 )
175- var highlightedPreeditRange = NSRange ( location : NSNotFound , length : 0 )
174+ let preeditRange : NSRange
175+ let highlightedPreeditRange : NSRange
176176
177177 // preedit
178178 if !preedit. isEmpty {
179- let line = NSMutableAttributedString ( )
180- let startIndex = String . Index ( utf16Offset: selRange. location, in: preedit)
181- let endIndex = String . Index ( utf16Offset: selRange. upperBound, in: preedit)
182- if selRange. location > 0 {
183- line. append ( NSAttributedString ( string: String ( preedit [ ..< startIndex] ) , attributes: theme. preeditAttrs) )
184- }
185- if selRange. length > 0 {
186- let highlightedPreeditStart = line. length
187- line. append ( NSAttributedString ( string: String ( preedit [ startIndex..< endIndex] ) , attributes: theme. preeditHighlightedAttrs) )
188- highlightedPreeditRange = NSRange ( location: highlightedPreeditStart, length: line. length - highlightedPreeditStart)
189- }
190- if selRange. upperBound < preedit. utf16. count {
191- line. append ( NSAttributedString ( string: String ( preedit [ endIndex... ] ) , attributes: theme. preeditAttrs) )
192- }
179+ preeditRange = NSRange ( location: 0 , length: preedit. utf16. count)
180+ highlightedPreeditRange = selRange
181+
182+ let line = NSMutableAttributedString ( string: preedit)
183+ line. addAttributes ( theme. preeditAttrs, range: preeditRange)
184+ line. addAttributes ( theme. preeditHighlightedAttrs, range: selRange)
193185 text. append ( line)
194186
195187 text. addAttribute ( . paragraphStyle, value: theme. preeditParagraphStyle, range: NSRange ( location: 0 , length: text. length) )
196- preeditRange = NSRange ( location: 0 , length: text. length)
197188 if !candidates. isEmpty {
198189 text. append ( NSAttributedString ( string: " \n " , attributes: theme. preeditAttrs) )
199190 }
191+ } else {
192+ preeditRange = . empty
193+ highlightedPreeditRange = . empty
200194 }
201195
202196 // candidates
@@ -441,7 +435,7 @@ private extension SquirrelPanel {
441435 view. textContentStorage. attributedString = text
442436 view. textView. setLayoutOrientation ( vertical ? . vertical : . horizontal)
443437 view. drawView ( candidateRanges: [ NSRange ( location: 0 , length: text. length) ] , hilightedIndex: - 1 ,
444- preeditRange: NSRange ( location : NSNotFound , length : 0 ) , highlightedPreeditRange: NSRange ( location : NSNotFound , length : 0 ) )
438+ preeditRange: . empty , highlightedPreeditRange: . empty )
445439 show ( )
446440
447441 statusTimer? . invalidate ( )
0 commit comments