Improve loads of fancy things in relation to the mouse pointer

This commit is contained in:
2026-10-07 13:02:52 +02:00
parent ed5a227c9d
commit a0b3ef9975
25 changed files with 334 additions and 89 deletions
@@ -0,0 +1,57 @@
import AppKit
import HelderCore
@testable import HelderUI
import Testing
@MainActor
@Suite struct CodeEditorShowDiffTests {
/// Line 2 is a blank added line, line 3 a changed one; lines 1 and 4 are unchanged.
private func open(offersShowDiff: Bool = true) -> (CodeEditorController, NSWindow) {
let controller = CodeEditorController()
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 700, height: 400), styleMask: [.titled],
backing: .buffered, defer: false)
window.contentView = controller.scrollView
let marks = [2: CodeLineMark(tint: true, tone: .added), 3: CodeLineMark(tint: true, tone: .added)]
controller.apply(CodeEditorConfiguration(documentID: "a.php", path: "a.php", text: "one\n\nthree\nfour", revision: 1,
language: nil, isEditable: true, wrap: false, tabSize: 4, marksKey: "m", makeMarks: { marks },
offersShowDiff: offersShowDiff, knownSymbols: [], symbolsVersion: 0, flash: nil, focusRequest: 0,
selectAllRequest: 0, fontScale: 1, isShown: true))
window.displayIfNeeded()
return (controller, window)
}
/// A window point inside the first row of `line`, `x` points from the text view's left edge.
private func point(line: Int, x: CGFloat, in controller: CodeEditorController) -> NSPoint {
var rect: NSRect?
controller.forEachRow(in: controller.textView.bounds) { row in
if row.line == line, rect == nil { rect = row.rect }
}
let row = rect ?? .zero
return controller.textView.convert(NSPoint(x: x, y: row.midY), to: nil)
}
@Test func aBlankAddedLineOffersShowDiff() {
let (controller, window) = open()
#expect(controller.showDiffLine(atWindowPoint: point(line: 2, x: 40, in: controller)) == 2)
withExtendedLifetime(window) {}
}
@Test func theSpaceAfterTheLastGlyphCounts() {
let (controller, window) = open()
#expect(controller.showDiffLine(atWindowPoint: point(line: 3, x: 500, in: controller)) == 3)
withExtendedLifetime(window) {}
}
@Test func anUnchangedLineOffersNothing() {
let (controller, window) = open()
#expect(controller.showDiffLine(atWindowPoint: point(line: 1, x: 10, in: controller)) == nil)
#expect(controller.showDiffLine(atWindowPoint: point(line: 4, x: 500, in: controller)) == nil)
withExtendedLifetime(window) {}
}
@Test func aFileWithoutShowDiffOffersNothingOnAMarkedLine() {
let (controller, window) = open(offersShowDiff: false)
#expect(controller.showDiffLine(atWindowPoint: point(line: 3, x: 10, in: controller)) == nil)
withExtendedLifetime(window) {}
}
}
@@ -16,7 +16,17 @@ import Testing
let view = EditorPresentation(path: "a.php", requested: nil, diff: makeDiff(original: "a", updated: "b"))
#expect(view.mode == .actual)
#expect(view.barButtons == [.diff])
#expect(view.opensDiffOnClick)
#expect(view.offersShowDiff)
}
@Test func anUnchangedFileOffersNoShowDiff() {
#expect(!EditorPresentation(path: "a.php", requested: nil, diff: nil).offersShowDiff)
}
@Test func showDiffNeedsActualInThePane() {
let view = EditorPresentation(path: "README.md", requested: .preview, diff: makeDiff(original: "a", updated: "b"))
#expect(view.paneMode == .preview)
#expect(!view.offersShowDiff)
}
@Test func unchangedMarkdownOffersOnlyPreview() {
@@ -55,11 +65,11 @@ import Testing
#expect(view.paneMode == .actual)
}
@Test func anAddedFileHasNoClickToDiff() {
@Test func anAddedFileOffersNoShowDiff() {
let diff = makeDiff(original: "", updated: "b", isAddedFile: true)
let actual = EditorPresentation(path: "a.php", requested: .actual, diff: diff)
#expect(actual.placeholder == nil)
#expect(!actual.opensDiffOnClick)
#expect(!actual.offersShowDiff)
#expect(actual.status == .added)
}
@@ -18,7 +18,7 @@ import Testing
window.contentView = controller.scrollView
controller.apply(CodeEditorConfiguration(documentID: "notes.md", path: "notes.md", text: Self.text, revision: 1,
language: nil, isEditable: true, wrap: wrap, tabSize: 4, marksKey: "", makeMarks: { [:] },
opensDiffOnClick: false, knownSymbols: [], symbolsVersion: 0, flash: nil, focusRequest: 0,
offersShowDiff: false, knownSymbols: [], symbolsVersion: 0, flash: nil, focusRequest: 0,
selectAllRequest: 0, fontScale: 1, isShown: true))
return (controller, window)
}
@@ -458,7 +458,7 @@ import Testing
#expect(h.store.mode(for: "README.md") == .preview)
}
@Test func aClickOnAMarkedLineOpensTheDiffOnHeadVersusDiskAtThatLine() {
@Test func showDiffOnAMarkedLineOpensTheDiffOnHeadVersusDiskAtThatLine() {
let h = EditorHarness(files: ["a.php": "new"])
h.git.change("a.php", makeDiff(original: "old", updated: "new"))
h.store.open("a.php", OpenOptions(side: .unstaged))
@@ -553,16 +553,29 @@ import Testing
#expect(h.store.passOnSelectionRequest(anchor: .zero) == nil)
}
@Test func theRightClickMenuIsPassOnThenCopyReference() {
@Test func theRightClickMenuIsPassOnThenCopyReferenceWithoutAHeader() {
let h = EditorHarness(files: ["src/a.php": "x"])
let menu = h.store.contextMenuRequest(reference: AgentReference(path: "src/a.php", startLine: 42), code: nil, anchor: .zero)
#expect(menu.header == "src/a.php:42")
#expect(menu.header == "")
#expect(menu.entries == [
.action(.passOnReference("src/a.php:42", code: nil), label: "Pass on reference"),
.action(.copyReference("src/a.php:42"), label: "Copy reference"),
])
}
@Test func aMarkedLineAddsShowDiffBelowASeparator() {
let h = EditorHarness(files: ["src/a.php": "x"])
let menu = h.store.contextMenuRequest(reference: AgentReference(path: "src/a.php", startLine: 42), code: nil,
showDiffLine: 42, anchor: .zero)
#expect(menu.header == "")
#expect(menu.entries == [
.action(.passOnReference("src/a.php:42", code: nil), label: "Pass on reference"),
.action(.copyReference("src/a.php:42"), label: "Copy reference"),
.separator,
.action(.showDiff("src/a.php", line: 42), label: "Show diff"),
])
}
@Test func anEmptyNotePassesTheBareReference() {
#expect(PassOn.compose(note: " ", reference: "src/a.php:42") == "src/a.php:42")
}