@@ -20,7 +20,7 @@ final class SquirrelPanel: NSPanel {
20
20
private var statusTimer : Timer ?
21
21
22
22
private var preedit : String = " "
23
- private var selRange : NSRange = . init ( location : NSNotFound , length : 0 )
23
+ private var selRange : NSRange = . empty
24
24
private var caretPos : Int = 0
25
25
private var candidates : [ String ] = . init( )
26
26
private var comments : [ String ] = . init( )
@@ -171,32 +171,26 @@ final class SquirrelPanel: NSPanel {
171
171
currentScreen ( )
172
172
173
173
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
176
176
177
177
// preedit
178
178
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)
193
185
text. append ( line)
194
186
195
187
text. addAttribute ( . paragraphStyle, value: theme. preeditParagraphStyle, range: NSRange ( location: 0 , length: text. length) )
196
- preeditRange = NSRange ( location: 0 , length: text. length)
197
188
if !candidates. isEmpty {
198
189
text. append ( NSAttributedString ( string: " \n " , attributes: theme. preeditAttrs) )
199
190
}
191
+ } else {
192
+ preeditRange = . empty
193
+ highlightedPreeditRange = . empty
200
194
}
201
195
202
196
// candidates
@@ -441,7 +435,7 @@ private extension SquirrelPanel {
441
435
view. textContentStorage. attributedString = text
442
436
view. textView. setLayoutOrientation ( vertical ? . vertical : . horizontal)
443
437
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 )
445
439
show ( )
446
440
447
441
statusTimer? . invalidate ( )
0 commit comments