feat: continue nested markdown lists on return
This commit is contained in:
@@ -230,3 +230,95 @@ func notesLauncherRoutesShortcutsAndTearsDownPickersWithoutTakingEditorNavigatio
|
||||
#expect(!descendants(content).compactMap { $0 as? NSSearchField }.contains { $0.accessibilityLabel() == "Search notes" })
|
||||
#expect(launcherSearch.isHidden == false)
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func markdownListReturnContinuesExitsAndUndoesAtArbitraryDepths() throws {
|
||||
_ = NSApplication.shared
|
||||
let suite = "NotesLists.\(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
|
||||
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? NSTextView }.first)
|
||||
window.makeFirstResponder(editor)
|
||||
let enter = try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [],
|
||||
timestamp: 1, windowNumber: window.windowNumber, context: nil, characters: "\r",
|
||||
charactersIgnoringModifiers: "\r", isARepeat: false, keyCode: 36))
|
||||
func reset(_ text: String) {
|
||||
editor.insertText(text, replacementRange: NSRange(location: 0, length: editor.string.utf16.count))
|
||||
editor.breakUndoCoalescing()
|
||||
editor.undoManager?.removeAllActions()
|
||||
}
|
||||
let examples = [
|
||||
("- fox 🦊", "- fox 🦊\n- "),
|
||||
(" * nested", " * nested\n * "),
|
||||
("\t+ nested", "\t+ nested\n\t+ "),
|
||||
("9. ninth", "9. ninth\n10. "),
|
||||
(" 12) nested", " 12) nested\n 13) "),
|
||||
("- [x] finished", "- [x] finished\n- [ ] "),
|
||||
("- ", ""), ("+ \t ", ""), ("4. ", ""), ("- [ ] ", ""),
|
||||
("- parent\n - ", "- parent\n- "),
|
||||
("1. parent\n * ", "1. parent\n2. "),
|
||||
("- 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\n + ", "- parent\n- "),
|
||||
("- parent\r\n - ", "- parent\r\n- "),
|
||||
("prose", "prose\n"), ("* * *", "* * *\n"),
|
||||
("```md\n- literal", "```md\n- literal\n"),
|
||||
("~~~\n 1. literal", "~~~\n 1. literal\n"),
|
||||
("```\n- literal\n```\n- real", "```\n- literal\n```\n- real\n- "),
|
||||
]
|
||||
for (before, after) in examples {
|
||||
reset(before)
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == after, "Return in \(before.debugDescription)")
|
||||
#expect(editor.selectedRange() == NSRange(location: after.utf16.count, length: 0))
|
||||
}
|
||||
reset("- ab🦊cd")
|
||||
editor.setSelectedRange((editor.string as NSString).range(of: "🦊"))
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == "- ab\n- cd")
|
||||
#expect(editor.selectedRange() == NSRange(location: 7, length: 0))
|
||||
reset("- parent\n - child\n - leaf")
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == "- parent\n - child\n - leaf\n - ")
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == "- parent\n - child\n - leaf\n - ")
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == "- parent\n - child\n - leaf\n- ")
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == "- parent\n - child\n - leaf\n")
|
||||
// Nesting follows actual ancestor indentation, not a fixed depth or indent size.
|
||||
let ancestors = (0..<80).map { String(repeating: " ", count: $0 * 2) + "- level \($0)" }.joined(separator: "\n")
|
||||
let before = ancestors + "\n" + String(repeating: " ", count: 160) + "- leaf"
|
||||
reset(before)
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == before + "\n" + String(repeating: " ", count: 160) + "- ")
|
||||
for depth in stride(from: 79, through: 0, by: -1) {
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == before + "\n" + String(repeating: " ", count: depth * 2) + "- ")
|
||||
}
|
||||
editor.keyDown(with: enter)
|
||||
#expect(editor.string == before + "\n")
|
||||
reset("7. saved")
|
||||
editor.keyDown(with: enter)
|
||||
editor.breakUndoCoalescing()
|
||||
#expect(editor.string == "7. saved\n8. ")
|
||||
editor.undoManager?.undo()
|
||||
#expect(editor.string == "7. saved")
|
||||
editor.undoManager?.redo()
|
||||
#expect(editor.string == "7. saved\n8. ")
|
||||
let restored = try NotesStore(directory: directory)
|
||||
let id = try #require(defaults.string(forKey: "selectedNote").flatMap(UUID.init(uuidString:)))
|
||||
#expect(restored.notes.first { $0.id == id }?.contents.string == "7. saved\n8. ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user