fix: restore empty list exit and keyboard indentation

This commit is contained in:
Valdemar Lorenzen
2026-10-19 15:06:47 +02:00
parent 21619c5420
commit 1c3ecb232b
4 changed files with 221 additions and 21 deletions
+143 -1
View File
@@ -275,7 +275,9 @@ func markdownListReturnContinuesExitsAndUndoesAtArbitraryDepths() throws {
("- parent\n\t+ child\n\t\t3) ", "- parent\n\t+ child\n\t+ "),
("- first\n - child\n - ", "- first\n - child\n- "),
("- parent\n continuation\n - ", "- parent\n continuation\n- "),
("- parent\nparagraph\n - ", "- parent\nparagraph\n"),
("- parent\nparagraph\n - ", "- parent\nparagraph\n- "),
("* * *\n - ", "* * *\n- "),
("- - -\n 4) ", "- - -\n 4) "),
("- parent\n + ", "- parent\n- "),
("- parent\r\n - ", "- parent\r\n- "),
("prose", "prose\n"), ("* * *", "* * *\n"),
@@ -290,6 +292,31 @@ func markdownListReturnContinuesExitsAndUndoesAtArbitraryDepths() throws {
#expect(editor.markdownContents.string == after)
#expect(editor.selectedRange() == NSRange(location: editor.string.utf16.count, length: 0))
}
// Empty means whitespace-only body, even before or within the extra separators.
for marker in ["-", "12)", "- [X]", "3. []"] {
let source = "- parent\n " + marker + " \t "
let bodyStart = ("- parent\n " + marker + " ").utf16.count
for position in bodyStart...source.utf16.count {
editor.setMarkdownContents(NSAttributedString(string: source))
editor.setSelectedRange(editor.renderedRange(for: NSRange(location: position, length: 0)))
editor.keyDown(with: enter)
#expect(editor.markdownString == "- parent\n- ",
"Empty Return at \(position) in \(source.debugDescription)")
}
}
for (source, steps) in [
(" + \t ", [" + \t ", " + \t ", "+ \t ", ""]),
("\t\t12) ", ["\t12) ", "12) ", ""]),
(" - [X] ", [" - [X] ", "- [X] ", ""]),
] {
editor.setMarkdownContents(NSAttributedString(string: source))
editor.setSelectedRange(NSRange(location: editor.string.utf16.count, length: 0))
for expected in steps {
editor.keyDown(with: enter)
#expect(editor.markdownString == expected)
#expect(editor.selectedRange() == NSRange(location: editor.string.utf16.count, length: 0))
}
}
reset("- ab🦊cd")
editor.setSelectedRange((editor.string as NSString).range(of: "🦊"))
editor.keyDown(with: enter)
@@ -350,6 +377,121 @@ func markdownListReturnContinuesExitsAndUndoesAtArbitraryDepths() throws {
#expect(restored.notes.first { $0.id == id }?.contents.string == "7. saved\n8. ")
}
@Test @MainActor
func listTabAndShiftTabPreserveMarkersSourceSelectionsUndoAndPersistence() throws {
_ = NSApplication.shared
let suite = "NotesIndent.\(UUID().uuidString)"
let defaults = try #require(UserDefaults(suiteName: suite))
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(suite)
defer {
defaults.removePersistentDomain(forName: suite)
try? FileManager.default.removeItem(at: directory)
}
let controller = NotesController(defaults: defaults, directory: directory)
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 750, height: 390),
styleMask: .borderless, backing: .buffered, defer: false)
window.contentViewController = controller
window.setContentSize(NSSize(width: 750, height: 390))
defer { _ = controller.close(); window.orderOut(nil) }
func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) }
let editor = try #require(descendants(controller.view).compactMap { $0 as? NotesTextView }.first)
window.makeFirstResponder(editor)
func key(_ shift: Bool) throws -> NSEvent {
try #require(NSEvent.keyEvent(with: .keyDown, location: .zero,
modifierFlags: shift ? .shift : [], timestamp: 1, windowNumber: window.windowNumber,
context: nil, characters: shift ? "\u{19}" : "\t", charactersIgnoringModifiers: "\t",
isARepeat: false, keyCode: 48))
}
let tab = try key(false), backtab = try key(true)
func reset(_ source: String) {
editor.setMarkdownContents(NSAttributedString(string: source))
editor.breakUndoCoalescing()
editor.undoManager?.removeAllActions()
}
for marker in ["-", "*", "+", "009)", "12.", "- [X]", "2) []"] {
let source = " " + marker + " \t🦊 café\n - child"
reset(source)
let body = (editor.string as NSString).range(of: "🦊 café")
// Visible indentation, marker, body start/middle/end, and a Unicode selection.
let selections = [NSRange(location: 0, length: 0), NSRange(location: 1, length: 0),
NSRange(location: 3, length: 0), NSRange(location: body.location, length: 0),
NSRange(location: body.location + 2, length: 0),
NSRange(location: NSMaxRange(body), length: 0), body]
for selection in selections {
reset(source)
editor.setSelectedRange(selection)
let logical = editor.markdownInsertionRange(for: selection)
editor.keyDown(with: tab)
editor.breakUndoCoalescing()
#expect(editor.markdownString == " " + source)
#expect(editor.markdownContents.string == " " + source)
#expect(editor.selectedRange() == editor.renderedRange(for:
NSRange(location: logical.location + 2, length: logical.length)))
editor.undoManager?.undo()
#expect(editor.markdownString == source)
#expect(editor.selectedRange() == selection)
#expect(editor.undoManager?.canUndo == false)
editor.undoManager?.redo()
#expect(editor.markdownString == " " + source)
editor.keyDown(with: backtab)
#expect(editor.markdownString == source)
#expect(editor.selectedRange() == editor.renderedRange(for: logical))
}
}
for (indent, remaining) in [("\t ", " "), (" ", " "), (" ", ""), ("", "")] {
for marker in ["+", "12)", "- [ ]"] {
let source = indent + marker + " 🦊 café"
reset(source)
let body = (editor.string as NSString).range(of: "🦊 café")
editor.setSelectedRange(body)
editor.keyDown(with: backtab)
#expect(editor.markdownString == remaining + marker + " 🦊 café")
#expect((editor.string as NSString).substring(with: editor.selectedRange()) == "🦊 café")
if indent.isEmpty { #expect(editor.undoManager?.canUndo == false) }
}
}
for source in ["prose", "* * *", "```md\n- literal", "~~~\n 12) literal"] {
reset(source)
editor.setSelectedRange(NSRange(location: editor.string.utf16.count, length: 0))
#expect(!controller.textView(editor, doCommandBy: #selector(NSResponder.insertTab(_:))))
#expect(!controller.textView(editor, doCommandBy: #selector(NSResponder.insertBacktab(_:))))
#expect(editor.markdownString == source)
}
reset("- root")
editor.setSelectedRange(NSRange(location: 0, length: 0))
for _ in 0..<3 { editor.keyDown(with: backtab) }
#expect(editor.markdownString == "- root")
#expect(editor.undoManager?.canUndo == false)
// Tab completes a still-literal typed marker rather than indenting it.
for marker in ["-", "12)", "- [X]"] {
reset(" ")
editor.setSelectedRange(NSRange(location: 0, length: 0))
for character in marker {
editor.insertText(String(character), replacementRange: editor.selectedRange())
}
#expect(editor.string == marker + " ")
editor.keyDown(with: tab)
#expect(editor.markdownString == marker + "\t ")
#expect(editor.string == "\u{fffc}\t ")
editor.keyDown(with: tab)
#expect(editor.markdownString == " " + marker + "\t ")
}
reset("- 🦊 café")
editor.setSelectedRange((editor.string as NSString).range(of: "café"))
editor.keyDown(with: tab)
editor.breakUndoCoalescing()
#expect(editor.markdownString == " - 🦊 café")
#expect((editor.string as NSString).substring(with: editor.selectedRange()) == "café")
#expect(controller.save())
let id = try #require(defaults.string(forKey: "selectedNote").flatMap(UUID.init(uuidString:)))
#expect(try NotesStore(directory: directory).notes.first { $0.id == id }?.contents.string == " - 🦊 café")
editor.undoManager?.undo()
#expect(editor.markdownString == "- 🦊 café")
#expect(editor.undoManager?.canUndo == false)
editor.undoManager?.redo()
#expect(editor.markdownString == " - 🦊 café")
}
@Test @MainActor
func taskCheckboxesToggleMarkdownWithUndoPersistenceAndListContinuation() throws {
_ = NSApplication.shared