Skip to content

Commit 18496b8

Browse files
committed
code gardening
* Add a NSRange.empty for easy access * Rewrite preedit attributed string creation
1 parent e257034 commit 18496b8

4 files changed

+25
-27
lines changed

sources/BridgingFunctions.swift

+4
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ func ?=<T>(left: inout T?, right: T?) {
5858
left = right
5959
}
6060
}
61+
62+
extension NSRange {
63+
static let empty = NSRange(location: NSNotFound, length: 0)
64+
}

sources/SquirrelInputController.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class SquirrelInputController: IMKInputController {
1313
private var client: IMKTextInput?
1414
private let rimeAPI: RimeApi_stdbool = rime_get_api_stdbool().pointee
1515
private var preedit: String = ""
16-
private var selRange: NSRange = NSRange(location: NSNotFound, length: 0)
16+
private var selRange: NSRange = .empty
1717
private var caretPos: Int = 0
1818
private var lastModifiers: NSEvent.ModifierFlags = .init()
1919
private var session: RimeSessionId = 0
@@ -411,8 +411,8 @@ private extension SquirrelInputController {
411411
if rimeAPI.get_commit(session, &commitText) {
412412
if let text = commitText.text {
413413
commit(string: String(cString: text))
414-
_ = rimeAPI.free_commit(&commitText)
415414
}
415+
_ = rimeAPI.free_commit(&commitText)
416416
}
417417
}
418418

@@ -512,7 +512,7 @@ private extension SquirrelInputController {
512512
func commit(string: String) {
513513
guard let client = client else { return }
514514
// print("[DEBUG] commitString: \(string)")
515-
client.insertText(string, replacementRange: NSRange(location: NSNotFound, length: 0))
515+
client.insertText(string, replacementRange: .empty)
516516
preedit = ""
517517
hidePalettes()
518518
}
@@ -538,7 +538,7 @@ private extension SquirrelInputController {
538538
let remainingRange = NSRange(location: start, length: preedit.utf16.count - start)
539539
let attrs = mark(forStyle: kTSMHiliteSelectedRawText, at: remainingRange)! as! [NSAttributedString.Key: Any]
540540
attrString.setAttributes(attrs, range: remainingRange)
541-
client.setMarkedText(attrString, selectionRange: NSRange(location: caretPos, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0))
541+
client.setMarkedText(attrString, selectionRange: NSRange(location: caretPos, length: 0), replacementRange: .empty)
542542
}
543543

544544
// swiftlint:disable:next function_parameter_count

sources/SquirrelPanel.swift

+13-19
Original file line numberDiff line numberDiff line change
@@ -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()

sources/SquirrelView.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ final class SquirrelView: NSView {
2828
private let squirrelLayoutDelegate: SquirrelLayoutDelegate
2929
var candidateRanges: [NSRange] = []
3030
var hilightedIndex = 0
31-
var preeditRange = NSRange(location: NSNotFound, length: 0)
32-
var highlightedPreeditRange = NSRange(location: NSNotFound, length: 0)
31+
var preeditRange: NSRange = .empty
32+
var highlightedPreeditRange: NSRange = .empty
3333
var separatorWidth: CGFloat = 0
3434
var shape = CAShapeLayer()
3535

@@ -72,7 +72,7 @@ final class SquirrelView: NSView {
7272
}
7373

7474
func convert(range: NSRange) -> NSTextRange? {
75-
guard range.location != NSNotFound else { return nil }
75+
guard range != .empty else { return nil }
7676
guard let startLocation = textLayoutManager.location(textLayoutManager.documentRange.location, offsetBy: range.location) else { return nil }
7777
guard let endLocation = textLayoutManager.location(startLocation, offsetBy: range.length) else { return nil }
7878
return NSTextRange(location: startLocation, end: endLocation)
@@ -668,7 +668,7 @@ private extension SquirrelView {
668668
if highlightedRange.upperBound == (textView.string as NSString).length {
669669
highlightedRect.size.height += theme.edgeInset.height - halfLinespace
670670
}
671-
if highlightedRange.location - ((preeditRange.location == NSNotFound ? 0 : preeditRange.location) + preeditRange.length) <= 1 {
671+
if highlightedRange.location - (preeditRange == .empty ? 0 : preeditRange.upperBound) <= 1 {
672672
if preeditRange.length == 0 {
673673
highlightedRect.size.height += theme.edgeInset.height - halfLinespace
674674
highlightedRect.origin.y -= theme.edgeInset.height - halfLinespace

0 commit comments

Comments
 (0)