From e976514b608565a023bdb0098cf50e5f74d5bd2f Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Tue, 28 May 2024 18:07:13 -0400 Subject: [PATCH] code gardening * Add a NSRange.empty for easy access * Rewrite preedit attributed string creation --- sources/BridgingFunctions.swift | 4 ++++ sources/SquirrelInputController.swift | 8 +++---- sources/SquirrelPanel.swift | 32 +++++++++++---------------- sources/SquirrelView.swift | 8 +++---- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/sources/BridgingFunctions.swift b/sources/BridgingFunctions.swift index ed537b3e7..39fdcdffd 100644 --- a/sources/BridgingFunctions.swift +++ b/sources/BridgingFunctions.swift @@ -58,3 +58,7 @@ func ?=(left: inout T?, right: T?) { left = right } } + +extension NSRange { + static let empty = NSRange(location: NSNotFound, length: 0) +} diff --git a/sources/SquirrelInputController.swift b/sources/SquirrelInputController.swift index df7aba759..cfe09ed4d 100644 --- a/sources/SquirrelInputController.swift +++ b/sources/SquirrelInputController.swift @@ -13,7 +13,7 @@ final class SquirrelInputController: IMKInputController { private var client: IMKTextInput? private let rimeAPI: RimeApi_stdbool = rime_get_api_stdbool().pointee private var preedit: String = "" - private var selRange: NSRange = NSRange(location: NSNotFound, length: 0) + private var selRange: NSRange = .empty private var caretPos: Int = 0 private var lastModifiers: NSEvent.ModifierFlags = .init() private var session: RimeSessionId = 0 @@ -411,8 +411,8 @@ private extension SquirrelInputController { if rimeAPI.get_commit(session, &commitText) { if let text = commitText.text { commit(string: String(cString: text)) - _ = rimeAPI.free_commit(&commitText) } + _ = rimeAPI.free_commit(&commitText) } } @@ -512,7 +512,7 @@ private extension SquirrelInputController { func commit(string: String) { guard let client = client else { return } // print("[DEBUG] commitString: \(string)") - client.insertText(string, replacementRange: NSRange(location: NSNotFound, length: 0)) + client.insertText(string, replacementRange: .empty) preedit = "" hidePalettes() } @@ -538,7 +538,7 @@ private extension SquirrelInputController { let remainingRange = NSRange(location: start, length: preedit.utf16.count - start) let attrs = mark(forStyle: kTSMHiliteSelectedRawText, at: remainingRange)! as! [NSAttributedString.Key: Any] attrString.setAttributes(attrs, range: remainingRange) - client.setMarkedText(attrString, selectionRange: NSRange(location: caretPos, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) + client.setMarkedText(attrString, selectionRange: NSRange(location: caretPos, length: 0), replacementRange: .empty) } // swiftlint:disable:next function_parameter_count diff --git a/sources/SquirrelPanel.swift b/sources/SquirrelPanel.swift index 52a00d0d2..6250491e1 100644 --- a/sources/SquirrelPanel.swift +++ b/sources/SquirrelPanel.swift @@ -20,7 +20,7 @@ final class SquirrelPanel: NSPanel { private var statusTimer: Timer? private var preedit: String = "" - private var selRange: NSRange = .init(location: NSNotFound, length: 0) + private var selRange: NSRange = .empty private var caretPos: Int = 0 private var candidates: [String] = .init() private var comments: [String] = .init() @@ -171,32 +171,26 @@ final class SquirrelPanel: NSPanel { currentScreen() let text = NSMutableAttributedString() - var preeditRange = NSRange(location: NSNotFound, length: 0) - var highlightedPreeditRange = NSRange(location: NSNotFound, length: 0) + let preeditRange: NSRange + let highlightedPreeditRange: NSRange // preedit if !preedit.isEmpty { - let line = NSMutableAttributedString() - let startIndex = String.Index(utf16Offset: selRange.location, in: preedit) - let endIndex = String.Index(utf16Offset: selRange.upperBound, in: preedit) - if selRange.location > 0 { - line.append(NSAttributedString(string: String(preedit[.. 0 { - let highlightedPreeditStart = line.length - line.append(NSAttributedString(string: String(preedit[startIndex.. NSTextRange? { - guard range.location != NSNotFound else { return nil } + guard range != .empty else { return nil } guard let startLocation = textLayoutManager.location(textLayoutManager.documentRange.location, offsetBy: range.location) else { return nil } guard let endLocation = textLayoutManager.location(startLocation, offsetBy: range.length) else { return nil } return NSTextRange(location: startLocation, end: endLocation) @@ -668,7 +668,7 @@ private extension SquirrelView { if highlightedRange.upperBound == (textView.string as NSString).length { highlightedRect.size.height += theme.edgeInset.height - halfLinespace } - if highlightedRange.location - ((preeditRange.location == NSNotFound ? 0 : preeditRange.location) + preeditRange.length) <= 1 { + if highlightedRange.location - (preeditRange == .empty ? 0 : preeditRange.upperBound) <= 1 { if preeditRange.length == 0 { highlightedRect.size.height += theme.edgeInset.height - halfLinespace highlightedRect.origin.y -= theme.edgeInset.height - halfLinespace