Skip to content

Commit

Permalink
code gardening
Browse files Browse the repository at this point in the history
* Add a NSRange.empty for easy access
* Rewrite preedit attributed string creation
  • Loading branch information
LEOYoon-Tsaw committed May 28, 2024
1 parent e257034 commit e976514
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
4 changes: 4 additions & 0 deletions sources/BridgingFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ func ?=<T>(left: inout T?, right: T?) {
left = right
}
}

extension NSRange {
static let empty = NSRange(location: NSNotFound, length: 0)
}
8 changes: 4 additions & 4 deletions sources/SquirrelInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand Down
32 changes: 13 additions & 19 deletions sources/SquirrelPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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[..<startIndex]), attributes: theme.preeditAttrs))
}
if selRange.length > 0 {
let highlightedPreeditStart = line.length
line.append(NSAttributedString(string: String(preedit[startIndex..<endIndex]), attributes: theme.preeditHighlightedAttrs))
highlightedPreeditRange = NSRange(location: highlightedPreeditStart, length: line.length - highlightedPreeditStart)
}
if selRange.upperBound < preedit.utf16.count {
line.append(NSAttributedString(string: String(preedit[endIndex...]), attributes: theme.preeditAttrs))
}
preeditRange = NSRange(location: 0, length: preedit.utf16.count)
highlightedPreeditRange = selRange

let line = NSMutableAttributedString(string: preedit)
line.addAttributes(theme.preeditAttrs, range: preeditRange)
line.addAttributes(theme.preeditHighlightedAttrs, range: selRange)
text.append(line)

text.addAttribute(.paragraphStyle, value: theme.preeditParagraphStyle, range: NSRange(location: 0, length: text.length))
preeditRange = NSRange(location: 0, length: text.length)
if !candidates.isEmpty {
text.append(NSAttributedString(string: "\n", attributes: theme.preeditAttrs))
}
} else {
preeditRange = .empty
highlightedPreeditRange = .empty
}

// candidates
Expand Down Expand Up @@ -441,7 +435,7 @@ private extension SquirrelPanel {
view.textContentStorage.attributedString = text
view.textView.setLayoutOrientation(vertical ? .vertical : .horizontal)
view.drawView(candidateRanges: [NSRange(location: 0, length: text.length)], hilightedIndex: -1,
preeditRange: NSRange(location: NSNotFound, length: 0), highlightedPreeditRange: NSRange(location: NSNotFound, length: 0))
preeditRange: .empty, highlightedPreeditRange: .empty)
show()

statusTimer?.invalidate()
Expand Down
8 changes: 4 additions & 4 deletions sources/SquirrelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ final class SquirrelView: NSView {
private let squirrelLayoutDelegate: SquirrelLayoutDelegate
var candidateRanges: [NSRange] = []
var hilightedIndex = 0
var preeditRange = NSRange(location: NSNotFound, length: 0)
var highlightedPreeditRange = NSRange(location: NSNotFound, length: 0)
var preeditRange: NSRange = .empty
var highlightedPreeditRange: NSRange = .empty
var separatorWidth: CGFloat = 0
var shape = CAShapeLayer()

Expand Down Expand Up @@ -72,7 +72,7 @@ final class SquirrelView: NSView {
}

func convert(range: NSRange) -> 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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e976514

Please sign in to comment.