import Foundation import Testing @testable import OrgCore /// The list that opens by itself (#139), fuzzy matching (#140), word completion (#141) and /// completion from the index (#143). struct CompletionListTests { func context(_ text: String, _ caret: Int? = nil, options: EditingOptions = .org) -> EditContext { let caret = caret ?? text.utf16.count return EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret.. (String, Int) in let caret = (marked as NSString).range(of: "|").location return (marked.replacingOccurrences(of: "|", with: ""), caret) } let results = try EmacsOracle.run(cases.map { EmacsOracle.Case(text: $0.0, point: EmacsOracle.point($0.1, in: $0.0) , form: form) }) for ((text, caret), result) in zip(cases, results) { var expansion = DabbrevExpansion(context(text, caret))! var ours: [String] = [] while true { let (edit, exhausted) = expansion.advance() if exhausted { ours.append(expansion.failure) break } ours.append(edit.replacement) } #expect(ours.joined(separator: "\n") == result.text, "\(text.debugDescription)") } } // MARK: - #143 @Test func idLinksCompleteHeadingsFromTheBufferThenTheIndex() { var options = fuzzy options.filePath = "/notes/here.org" let text = "* Alpha\n:PROPERTIES:\n:ID: a1\n:END:\n* Beta\nSee [[id:al]]" let completion = OrgCompletion.at(context(text, text.utf16.count - 2, options: options))! #expect(completion.kind == .idLink && completion.typed == "al" && completion.query == .linkTargets) #expect(completion.candidates == ["Alpha"]) #expect(completion.link == 46..<55) let other = CompletionItem(title: "All hands", id: nil, path: "/notes/other.org", start: 10, heading: true) let twin = CompletionItem(title: "Alpha", id: "a2", path: "/notes/other.org", start: 40, heading: true) let filled = OrgCompletion.filled(completion, with: CompletionResult(items: [other, twin]), fuzzy: true) #expect(Set(filled.candidates) == ["Alpha (here.org)", "Alpha (other.org)", "All hands"]) #expect(filled.item("All hands") == other && filled.label("All hands") == "All hands other.org") // A heading here without an ID gets one, and the link moves with the drawer. let beta = CompletionItem(title: "Beta", id: nil, path: "/notes/here.org", start: 35, heading: true) let edits = CompletionFlow.createID(beta, in: context(text, options: options), newID: "b1")! let link = CompletionFlow.linkEdit(completion, beta, id: "b1", after: edits) var result = text for edit in edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { result = edit.apply(to: result) } result = link.apply(to: result) #expect(result == "* Alpha\n:PROPERTIES:\n:ID: a1\n:END:\n* Beta\n:PROPERTIES:\n:ID: b1\n:END:\nSee [[id:b1][Beta]]") } @Test func denoteLinksAskTheIndex() { let completion = OrgCompletion.at(context("[[denote:"))! #expect(completion.kind == .denoteLink && completion.candidates.isEmpty && completion.query == .denoteNotes) let note = CompletionItem(title: "Reading list", id: "20260101T090000", path: "/n/20260101T090000--reading-list.org", start: 0, heading: false) let filled = OrgCompletion.filled(completion, with: CompletionResult(items: [note]), fuzzy: false) #expect(filled.candidates == ["Reading list"] && filled.link(note, id: note.id!) == "[[denote:20260101T090000][Reading list]]") } @Test func propertyValuesFromTheBufferAllowedFirst() { let text = "#+PROPERTY: Status_ALL todo \"in progress\" done\n* A\n:PROPERTIES:\n:Status: waiting\n:END:\n* B\n:PROPERTIES:\n:Status: i\n:END:\n" let caret = (text as NSString).range(of: ":Status: i").location + 10 let completion = OrgCompletion.at(context(text, caret))! #expect(completion.kind == .propertyValue && completion.query == .propertyValues("Status")) #expect(completion.candidates == ["in progress"]) let all = OrgCompletion.at(context(text, caret - 1))! #expect(all.candidates == ["todo", "in progress", "done", "waiting"]) let filled = OrgCompletion.filled(all, with: CompletionResult(strings: ["blocked", "done"]), fuzzy: false) #expect(filled.candidates == ["todo", "in progress", "done", "waiting", "blocked"]) // Names still complete as before. #expect(OrgCompletion.at(context("* H\n:PROPERTIES:\n:A: 1\n:CU\n:END:\n", 26))?.kind == .property) } }