Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions GhosttyTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
D0B10008A1B2C3D4E5F60001 /* PortalTabDragRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B10009A1B2C3D4E5F60001 /* PortalTabDragRoutingTests.swift */; };
D0B1000AA1B2C3D4E5F60001 /* GhosttyTerminalViewSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B1000BA1B2C3D4E5F60001 /* GhosttyTerminalViewSupport.swift */; };
D0B10012A1B2C3D4E5F60001 /* GhosttyApp+SurfaceConfigurationReload.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B10013A1B2C3D4E5F60001 /* GhosttyApp+SurfaceConfigurationReload.swift */; };
D3571000A1B2C3D4E5F60718 /* GhosttyNSView+IMEComposition.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3571001A1B2C3D4E5F60718 /* GhosttyNSView+IMEComposition.swift */; };
D0B1000CA1B2C3D4E5F60001 /* CmuxWebViewDragRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B1000DA1B2C3D4E5F60001 /* CmuxWebViewDragRoutingTests.swift */; };
D0B1000EA1B2C3D4E5F60001 /* BrowserPaneDropRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B1000FA1B2C3D4E5F60001 /* BrowserPaneDropRoutingTests.swift */; };
D0B10010A1B2C3D4E5F60001 /* BonsplitTabBarDebug.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B10011A1B2C3D4E5F60001 /* BonsplitTabBarDebug.swift */; };
Expand Down Expand Up @@ -328,6 +329,7 @@
F2000000A1B2C3D4E5F60718 /* UpdatePillReleaseVisibilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2000001A1B2C3D4E5F60718 /* UpdatePillReleaseVisibilityTests.swift */; };
F20F85FC5900550685FA33AD /* StackAuth in Frameworks */ = {isa = PBXBuildFile; productRef = A8BD195031FC4B82B4354297 /* StackAuth */; };
F3000000A1B2C3D4E5F60718 /* CJKIMEInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3000001A1B2C3D4E5F60718 /* CJKIMEInputTests.swift */; };
D3571002A1B2C3D4E5F60718 /* CJKIMEMarkedSelectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3571003A1B2C3D4E5F60718 /* CJKIMEMarkedSelectionTests.swift */; };
1718C0DE1718C0DE17180002 /* GhosttyCommandShiftForwardingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1718C0DE1718C0DE17180001 /* GhosttyCommandShiftForwardingTests.swift */; };
D3284001A1B2C3D4E5F60718 /* TraditionalChineseIMENumpadRegressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3284002A1B2C3D4E5F60718 /* TraditionalChineseIMENumpadRegressionTests.swift */; };
F4000000A1B2C3D4E5F60718 /* GhosttyConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4000001A1B2C3D4E5F60718 /* GhosttyConfigTests.swift */; };
Expand Down Expand Up @@ -564,6 +566,7 @@
D7AB00000000000000000006 /* GhosttyNSView+MoveTabToNewWorkspace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GhosttyNSView+MoveTabToNewWorkspace.swift"; sourceTree = "<group>"; };
D0B1000BA1B2C3D4E5F60001 /* GhosttyTerminalViewSupport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GhosttyTerminalViewSupport.swift; sourceTree = "<group>"; };
D0B10013A1B2C3D4E5F60001 /* GhosttyApp+SurfaceConfigurationReload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GhosttyApp+SurfaceConfigurationReload.swift"; sourceTree = "<group>"; };
D3571001A1B2C3D4E5F60718 /* GhosttyNSView+IMEComposition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GhosttyNSView+IMEComposition.swift"; sourceTree = "<group>"; };
D0B10001A1B2C3D4E5F60001 /* TerminalPaneDropTargetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalPaneDropTargetView.swift; sourceTree = "<group>"; };
A5001016 /* GhosttyKit.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = GhosttyKit.xcframework; sourceTree = "<group>"; };
A5001017 /* ghostty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ghostty.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -756,6 +759,7 @@
F1C1AA20B7E84D10A1C10001 /* InactivePaneFirstClickFocusTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InactivePaneFirstClickFocusTests.swift; sourceTree = "<group>"; };
F2000001A1B2C3D4E5F60718 /* UpdatePillReleaseVisibilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatePillReleaseVisibilityTests.swift; sourceTree = "<group>"; };
F3000001A1B2C3D4E5F60718 /* CJKIMEInputTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CJKIMEInputTests.swift; sourceTree = "<group>"; };
D3571003A1B2C3D4E5F60718 /* CJKIMEMarkedSelectionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CJKIMEMarkedSelectionTests.swift; sourceTree = "<group>"; };
1718C0DE1718C0DE17180001 /* GhosttyCommandShiftForwardingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GhosttyCommandShiftForwardingTests.swift; sourceTree = "<group>"; };
D3284002A1B2C3D4E5F60718 /* TraditionalChineseIMENumpadRegressionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TraditionalChineseIMENumpadRegressionTests.swift; sourceTree = "<group>"; };
F4000001A1B2C3D4E5F60718 /* GhosttyConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GhosttyConfigTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1038,6 +1042,7 @@
D7AB00000000000000000006 /* GhosttyNSView+MoveTabToNewWorkspace.swift */,
D0B1000BA1B2C3D4E5F60001 /* GhosttyTerminalViewSupport.swift */,
D0B10013A1B2C3D4E5F60001 /* GhosttyApp+SurfaceConfigurationReload.swift */,
D3571001A1B2C3D4E5F60718 /* GhosttyNSView+IMEComposition.swift */,
D0B10001A1B2C3D4E5F60001 /* TerminalPaneDropTargetView.swift */,
A5001531 /* TerminalWindowPortal.swift */,
D0B10007A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift */,
Expand Down Expand Up @@ -1188,6 +1193,7 @@
children = (
F2000001A1B2C3D4E5F60718 /* UpdatePillReleaseVisibilityTests.swift */,
F3000001A1B2C3D4E5F60718 /* CJKIMEInputTests.swift */,
D3571003A1B2C3D4E5F60718 /* CJKIMEMarkedSelectionTests.swift */,
1718C0DE1718C0DE17180001 /* GhosttyCommandShiftForwardingTests.swift */,
D3284002A1B2C3D4E5F60718 /* TraditionalChineseIMENumpadRegressionTests.swift */,
F4000001A1B2C3D4E5F60718 /* GhosttyConfigTests.swift */,
Expand Down Expand Up @@ -1612,6 +1618,7 @@
D7AB00000000000000000005 /* GhosttyNSView+MoveTabToNewWorkspace.swift in Sources */,
D0B1000AA1B2C3D4E5F60001 /* GhosttyTerminalViewSupport.swift in Sources */,
D0B10012A1B2C3D4E5F60001 /* GhosttyApp+SurfaceConfigurationReload.swift in Sources */,
D3571000A1B2C3D4E5F60718 /* GhosttyNSView+IMEComposition.swift in Sources */,
D0B10000A1B2C3D4E5F60001 /* TerminalPaneDropTargetView.swift in Sources */,
A5001532 /* TerminalWindowPortal.swift in Sources */,
D0B10006A1B2C3D4E5F60001 /* TerminalWindowPortalDebug.swift in Sources */,
Expand Down Expand Up @@ -1801,6 +1808,7 @@
files = (
F2000000A1B2C3D4E5F60718 /* UpdatePillReleaseVisibilityTests.swift in Sources */,
F3000000A1B2C3D4E5F60718 /* CJKIMEInputTests.swift in Sources */,
D3571002A1B2C3D4E5F60718 /* CJKIMEMarkedSelectionTests.swift in Sources */,
1718C0DE1718C0DE17180002 /* GhosttyCommandShiftForwardingTests.swift in Sources */,
D3284001A1B2C3D4E5F60718 /* TraditionalChineseIMENumpadRegressionTests.swift in Sources */,
F4000000A1B2C3D4E5F60718 /* GhosttyConfigTests.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions Sources/GhosttyNSView+IMEComposition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import AppKit

extension GhosttyNSView {
func normalizedMarkedSelectionRange(_ range: NSRange, markedLength: Int) -> NSRange {
guard markedLength > 0 else {
return NSRange(location: NSNotFound, length: 0)
}
guard range.location != NSNotFound else {
return NSRange(location: markedLength, length: 0)
}

let clampedLocation = min(max(range.location, 0), markedLength)
let clampedLength = min(max(range.length, 0), markedLength - clampedLocation)
return NSRange(location: clampedLocation, length: clampedLength)
}

func clampedMarkedTextRange(_ range: NSRange, markedLength: Int) -> NSRange? {
guard range.length > 0, range.location != NSNotFound else { return nil }
guard markedLength > 0 else { return nil }

let location = min(max(range.location, 0), markedLength)
let maxLength = markedLength - location
guard maxLength > 0 else { return nil }

let length = min(max(range.length, 0), maxLength)
guard length > 0 else { return nil }
return NSRange(location: location, length: length)
}
}
25 changes: 23 additions & 2 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7043,6 +7043,7 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
// For NSTextInputClient - accumulates text during key events
private(set) var keyTextAccumulator: [String]? = nil
private var markedText = NSMutableAttributedString()
private var markedSelectedRange = NSRange(location: NSNotFound, length: 0)
private var lastPerformKeyEvent: TimeInterval?
private(set) var externalCommittedTextDepth = 0
var numpadIMECommitDeduplicator = NumpadIMECommitDeduplicator()
Expand Down Expand Up @@ -12716,7 +12717,16 @@ extension GhosttyNSView: NSTextInputClient {
}

func selectedRange() -> NSRange {
readSelectionSnapshot()?.range ?? NSRange(location: 0, length: 0)
if markedText.length > 0 {
#if DEBUG
assert(
markedSelectedRange.location != NSNotFound,
"markedSelectedRange must be valid when markedText is non-empty"
)
#endif
return markedSelectedRange
}
return readSelectionSnapshot()?.range ?? NSRange(location: 0, length: 0)
}
Comment thread
austinywang marked this conversation as resolved.
Resolved

func setMarkedText(_ string: Any, selectedRange: NSRange, replacementRange: NSRange) {
Expand All @@ -12736,8 +12746,12 @@ extension GhosttyNSView: NSTextInputClient {
case let v as String:
markedText = NSMutableAttributedString(string: v)
default:
break
return
}
markedSelectedRange = normalizedMarkedSelectionRange(
selectedRange,
markedLength: markedText.length
)

// If we're not in a keyDown event, sync preedit immediately.
// This can happen due to external events like changing keyboard layouts
Expand All @@ -12762,6 +12776,7 @@ extension GhosttyNSView: NSTextInputClient {
#endif
if markedText.length > 0 {
markedText.mutableString.setString("")
markedSelectedRange = NSRange(location: NSNotFound, length: 0)
syncPreedit()
invalidateTextInputCoordinates(selectionChanged: true)
}
Expand Down Expand Up @@ -12804,6 +12819,12 @@ extension GhosttyNSView: NSTextInputClient {
}

func attributedSubstring(forProposedRange range: NSRange, actualRange: NSRangePointer?) -> NSAttributedString? {
if markedText.length > 0 {
guard let substringRange = clampedMarkedTextRange(range, markedLength: markedText.length) else { return nil }
actualRange?.pointee = substringRange
return markedText.attributedSubstring(from: substringRange)
}

guard range.length > 0,
let snapshot = readSelectionSnapshot() else { return nil }
actualRange?.pointee = snapshot.range
Expand Down
8 changes: 1 addition & 7 deletions cmuxTests/CJKIMEInputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,7 @@ final class CJKIMEMarkedTextTests: XCTestCase {
XCTAssertFalse(view.hasMarkedText())
}

// MARK: - selectedRange / validAttributesForMarkedText

func testSelectedRangeReturnsEmptyRangeWithoutSelection() {
let view = GhosttyNSView(frame: .zero)
let range = view.selectedRange()
XCTAssertEqual(range, NSRange(location: 0, length: 0))
}
// MARK: - validAttributesForMarkedText

func testValidAttributesForMarkedTextReturnsEmpty() {
let view = GhosttyNSView(frame: .zero)
Expand Down
83 changes: 83 additions & 0 deletions cmuxTests/CJKIMEMarkedSelectionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import XCTest
import AppKit

#if canImport(cmux_DEV)
@testable import cmux_DEV
#elseif canImport(cmux)
@testable import cmux
#endif

final class CJKIMEMarkedSelectionTests: XCTestCase {
Comment thread
austinywang marked this conversation as resolved.
Resolved
func testSelectedRangeReturnsEmptyRangeWithoutSelectionOrMarkedText() {
let view = GhosttyNSView(frame: .zero)
let range = view.selectedRange()
XCTAssertEqual(range, NSRange(location: 0, length: 0))
}

func testSelectedRangeTracksMarkedTextSelection() {
let view = GhosttyNSView(frame: .zero)

view.setMarkedText(
"にほんご",
selectedRange: NSRange(location: 2, length: 1),
replacementRange: NSRange(location: NSNotFound, length: 0)
)

XCTAssertEqual(
view.selectedRange(),
NSRange(location: 2, length: 1),
"selectedRange should mirror the IME caret/selection inside marked text"
)
}

func testSelectedRangeReturnsEmptyRangeAfterCompositionEnds() {
let view = GhosttyNSView(frame: .zero)

view.setMarkedText(
"東京",
selectedRange: NSRange(location: 1, length: 0),
replacementRange: NSRange(location: NSNotFound, length: 0)
)
view.unmarkText()

XCTAssertEqual(view.selectedRange(), NSRange(location: 0, length: 0))
}

func testAttributedSubstringReturnsMarkedTextSegment() {
let view = GhosttyNSView(frame: .zero)
view.setMarkedText(
"とうきょう",
selectedRange: NSRange(location: 3, length: 0),
replacementRange: NSRange(location: NSNotFound, length: 0)
)

var actualRange = NSRange(location: NSNotFound, length: 0)
let substring = view.attributedSubstring(
forProposedRange: NSRange(location: 2, length: 2),
actualRange: &actualRange
)

XCTAssertEqual(actualRange, NSRange(location: 2, length: 2))
XCTAssertEqual(substring?.string, "きょ")
}

func testTraditionalChineseZhuyinMarkedTextSelectionAndSubstring() {
let view = GhosttyNSView(frame: .zero)
view.setMarkedText(
"ㄓㄨ",
selectedRange: NSRange(location: 2, length: 0),
replacementRange: NSRange(location: NSNotFound, length: 0)
)

XCTAssertEqual(view.selectedRange(), NSRange(location: 2, length: 0))

var actualRange = NSRange(location: NSNotFound, length: 0)
let substring = view.attributedSubstring(
forProposedRange: NSRange(location: 0, length: 2),
actualRange: &actualRange
)

XCTAssertEqual(actualRange, NSRange(location: 0, length: 2))
XCTAssertEqual(substring?.string, "ㄓㄨ")
}
}
Loading