oficina / sheets Public

Mac: a key that types nothing never clears a cell

Review #72 delta, finding 5. Every empty key-down counted as a dead key, and
replace mode emptied the draft before the keys were replayed, so a key that
turned out to type nothing (JIS 英数/かな, say) opened the cell empty and a
click away committed "" over it.

The input-mode keys (key codes 102 and 104) no longer start typing. And
replace mode no longer empties anything: the held keys are replayed over
the field's contents, selected, so the cell's text is replaced only when
real or marked text arrives; a key that makes nothing leaves the draft as
it was and a click away writes nothing.

Test: testAKeyThatTypesNothingLeavesTheCellAlone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 186d7346dc83bc1fc7375bcab2b72ea3b7002803 parent f649376
3 files changed, +37 −16
  • native/apple/Sources/SheetsKit/SheetGrid.swift +1 −1
  • native/apple/Sources/SheetsKit/SheetTypeAhead.swift +10 −14
  • native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift +26 −1

CI passed — run details

CI log
amadan: .amadan/timeout: this job's budget is 15m0s (this runner allows up to 30m0s)
=== vet ===
CGO_ENABLED=0 go vet ./...
CGO_ENABLED=0 go vet -tags browser ./internal/sheets/
=== spacing ===
go tool fiesta spacing -skip 'vendor/*.css' -skip 'tokens.css' internal/sheets/static
=== fmt ===
=== build ===
CGO_ENABLED=0 go build ./...
=== migrations ===
go tool rastrillo migration check
rastrillo migration check: models and migrations agree
=== go-test ===
CGO_ENABLED=0 go test -count=1 ./...
?   	amadan.net/oficina/sheets/cmd/sheets	[no test files]
?   	amadan.net/oficina/sheets/cmd/sheets-contract	[no test files]
ok  	amadan.net/oficina/sheets/internal/guardstest	0.165s
ok  	amadan.net/oficina/sheets/internal/sheets	16.701s
ok  	amadan.net/oficina/sheets/internal/sheets/calc	0.013s
ok  	amadan.net/oficina/sheets/internal/sheets/format	0.005s
?   	amadan.net/oficina/sheets/internal/sheets/keymailtest	[no test files]
ok  	amadan.net/oficina/sheets/internal/sheets/live	0.056s
ok  	amadan.net/oficina/sheets/internal/sheets/seal	0.003s
ok  	amadan.net/oficina/sheets/internal/sheets/sealtest	0.002s
ok  	amadan.net/oficina/sheets/internal/sheets/store	6.591s
ok  	amadan.net/oficina/sheets/internal/sheets/teamstate	0.166s
ok  	amadan.net/oficina/sheets/internal/sheets/xlsx	5.117s
?   	amadan.net/oficina/sheets/tools/gencorpus	[no test files]
=== race ===
go test -race -count=1 ./internal/sheets/store/... ./internal/sheets/live/...
ok  	amadan.net/oficina/sheets/internal/sheets/store	74.070s
ok  	amadan.net/oficina/sheets/internal/sheets/live	1.067s
diff --git a/native/apple/Sources/SheetsKit/SheetGrid.swift b/native/apple/Sources/SheetsKit/SheetGrid.swift
index 98d37ad..c694df9 100644
--- a/native/apple/Sources/SheetsKit/SheetGrid.swift
+++ b/native/apple/Sources/SheetsKit/SheetGrid.swift
@@ -105,7 +105,7 @@ struct SheetGrid: View {
// cell may be scrolled out of view: bring it into view too.
let point = model.selection.start
scroller.reveal(point, at: metrics.frame(point))
- typeAhead.begin(with: event, replace: { [model] in model.draft = "" }, expired: fieldNeverCame)
+ typeAhead.begin(with: event, expired: fieldNeverCame)
edit(point)
if editState.cell == nil { typeAhead.cancel(); return false }
return true
diff --git a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
index b561415..79596c4 100644
--- a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
+++ b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
@@ -18,17 +18,16 @@ import SwiftUI
/// True from the keystroke that opened the field until it has them all.
private(set) var awaitingField = false
private var deadline: TimeInterval = 0
- /// Empties the cell's draft. Run only once the field exists and is about to
- /// receive the keys: a draft emptied for a field that never came would be
- /// committed by the next click as an empty cell.
- private var replace: (() -> Void)?
+ /// Keys that change the input mode, not the text (JIS 英数 and かな): they
+ /// arrive as key-downs with no characters, like a dead key, and are not one.
+ static let modeKeys: Set<UInt16> = [102, 104]
/// Whether a key, pressed on a selected cell, should start replacing it.
/// Anything that is not text (arrows, Return, Tab, Escape, Delete, function
/// keys) or that is a shortcut (Command, Control) is left to the grid.
static func startsTyping(_ event: NSEvent) -> Bool {
guard event.type == .keyDown, event.modifierFlags.intersection([.command, .control]).isEmpty,
- event.specialKey == nil
+ event.specialKey == nil, !modeKeys.contains(event.keyCode)
else { return false }
// A dead key has no characters yet; what it will compose is still text.
// With Option (US ´ is Option-E) the unmodified key still names a
@@ -43,8 +42,7 @@ import SwiftUI
private var generation = 0
/// `expired` runs if the field has not come a second later; the grid then
/// hands what was typed to the formula bar rather than drop it.
- func begin(with event: NSEvent, replace: @escaping () -> Void, expired: @escaping () -> Void) {
- self.replace = replace
+ func begin(with event: NSEvent, expired: @escaping () -> Void) {
held = [event]
awaitingField = true
deadline = ProcessInfo.processInfo.systemUptime + 1
@@ -66,7 +64,6 @@ import SwiftUI
func cancel() {
held = []
awaitingField = false
- replace = nil
}
/// Called once the cell's field is first responder. The replay waits one
@@ -83,12 +80,11 @@ import SwiftUI
let events = held
held = []
awaitingField = false
- if let replace {
- self.replace = nil
- replace()
- field.stringValue = ""
- field.currentEditor()?.string = ""
- }
+ // Typing replaces the cell, but only once text (or an input method's
+ // marked text) actually arrives: the keys go in over the whole contents,
+ // selected, and a key that turns out to type nothing leaves the cell as
+ // it was, so a click away commits nothing.
+ field.currentEditor()?.selectAll(nil)
for (index, event) in events.enumerated() {
guard let editor = field.currentEditor() else {
// Return or Tab ended this edit; what followed starts the next one.
diff --git a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
index 15b4f07..e088163 100644
--- a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
+++ b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
@@ -326,6 +326,31 @@ final class FirstKeystrokeTests: XCTestCase {
XCTAssertTrue(editors(host).isEmpty, "a stray editor of the old sheet stayed open")
}
+ /// Review #72 delta, finding 5: an empty key-down that types nothing (or
+ /// an input-mode key) must not clear the cell.
+ @MainActor func testAKeyThatTypesNothingLeavesTheCellAlone() async throws {
+ let delegate = FieldEditorDelegate()
+ let server = SheetServerFixture()
+ let model = fixtureModel(server)
+ model.cells[Coordinate(r: 1, c: 0)] = Cell(r: 1, c: 0, raw: "keep", display: "keep")
+ let (window, host) = try await host(model, delegate: delegate)
+ defer { window.orderOut(nil) }
+ XCTAssertEqual(model.draft, "keep")
+ // JIS かな: an input-mode key, not text.
+ XCTAssertFalse(SheetTypeAhead.startsTyping(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, windowNumber: 0, context: nil, characters: "", charactersIgnoringModifiers: "", isARepeat: false, keyCode: 104)!))
+ // An empty key-down the system then makes nothing of.
+ delegate.editor.inputMethod = true
+ key("", code: 10, window: window)
+ try await Task.sleep(for: .milliseconds(200))
+ XCTAssertFalse(editors(host).isEmpty, "a dead key opens the cell")
+ XCTAssertEqual(model.draft, "keep", "nothing typed, nothing replaced")
+ click(NSPoint(x: 260, y: 320), window: window)
+ try await Task.sleep(for: .milliseconds(100))
+ await model.settle()
+ XCTAssertTrue(server.batches.isEmpty, "a click away wrote the cell")
+ XCTAssertEqual(model.displayCell(Coordinate(r: 1, c: 0))?.display, "keep")
+ }
+
/// Held keys are never dropped: if the field does not come, what was typed
/// is handed on (to the formula bar) rather than discarded.
@MainActor func testHeldKeysAreHandedOnIfTheFieldNeverComes() async throws {
@@ -334,7 +359,7 @@ final class FirstKeystrokeTests: XCTestCase {
}
let typeAhead = SheetTypeAhead()
var handedOn: String?
- typeAhead.begin(with: event("x", 7), replace: {}, expired: { handedOn = typeAhead.heldText })
+ typeAhead.begin(with: event("x", 7), expired: { handedOn = typeAhead.heldText })
typeAhead.hold(event("y", 16))
typeAhead.hold(event("\r", 36))
try await Task.sleep(for: .milliseconds(1300))