fix: use downstream caret affinity after heading edits

This commit is contained in:
Valdemar Lorenzen
2026-10-21 16:32:24 +02:00
parent 489fa84b3c
commit 5065ba59bd
4 changed files with 30 additions and 2 deletions
@@ -106,3 +106,28 @@ func headingsFollowVisibleParagraphEditingAndRoundTrip() throws {
editor.insertText("# ", replacementRange: editor.selectedRange())
#expect(editor.markdownString == "```text\n## # Title\n```")
}
@Test @MainActor
func headingCommandsKeepCaretOnTheVisibleBodyLine() throws {
_ = NSApplication.shared
let editor = NotesTextView(frame: NSRect(x: 0, y: 0, width: 600, height: 500))
let window = NSWindow(contentRect: editor.frame, styleMask: .borderless, backing: .buffered, defer: false)
window.contentView = editor
window.makeFirstResponder(editor)
defer { window.orderOut(nil) }
for source in ["\n# Title", "Text\n\n## Title", "Text\n\n\n###### Title"] {
editor.setMarkdownContents(NSAttributedString(string: source))
editor.setSelectedRange(NSRange(location: (editor.string as NSString).range(of: "Title").location, length: 0))
for command in [#selector(NSResponder.deleteBackward(_:)), #selector(NSResponder.insertNewline(_:))] {
editor.doCommand(by: command)
let title = (editor.string as NSString).range(of: "Title")
#expect(editor.selectedRange() == NSRange(location: title.location, length: 0))
let caretRect = editor.firstRect(forCharacterRange: editor.selectedRange(), actualRange: nil)
let titleRect = editor.firstRect(forCharacterRange: NSRange(location: title.location, length: 1), actualRange: nil)
try #require(titleRect.height > 0)
#expect(caretRect.origin == titleRect.origin)
#expect(caretRect.height == titleRect.height)
}
#expect(editor.markdownString == source)
}
}