krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Commit d926f7c432

d926f7c4323f71a0f652cf7cd4d35c4813d62c19

parent: 5d3019a3c9

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-05 03:30 UTC

Add the command model, heading commands and the Emacs oracle

Layout: unified · split

Sources/OrgCore/Commands/Command.swift added +95
@@ -0,0 +1,95 @@
1import Foundation
2
3/// Everything a command may read. Commands never read the clock or the file system, so the
4/// same context always gives the same result.
5public struct EditContext: Sendable {
6 /// The revision `text` and `tree` belong to; results are applied only at this revision.
7 public let revision: Int
8 public let text: String
9 public let tree: OrgTree
10 /// Selections in UTF-16 offsets. The first is the main caret.
11 public let selection: [Range<Int>]
12 public let now: Date
13 /// Includes the time zone.
14 public let calendar: Calendar
15 /// Replies to earlier prompts, by prompt key.
16 public let answers: [String: String]
17
18 public init(
19 revision: Int, text: String, tree: OrgTree, selection: [Range<Int>],
20 now: Date = Date(), calendar: Calendar = .current, answers: [String: String] = [:]
21 ) {
22 self.revision = revision
23 self.text = text
24 self.tree = tree
25 self.selection = selection
26 self.now = now
27 self.calendar = calendar
28 self.answers = answers
29 }
30
31 public var caret: Int { selection.first?.lowerBound ?? 0 }
32}
33
34public struct Prompt: Sendable, Equatable {
35 /// The key the answer comes back under in `EditContext.answers`.
36 public let key: String
37 public let message: String
38
39 public init(key: String, message: String) {
40 self.key = key
41 self.message = message
42 }
43}
44
45/// Things a command asks for besides text changes; the platform layer carries them out.
46public enum Effect: Sendable, Equatable {
47 case message(String)
48}
49
50public struct EditResult: Sendable, Equatable {
51 public let baseRevision: Int
52 /// Non-overlapping, in `baseRevision` coordinates.
53 public let edits: [TextEdit]
54 /// Selection after the edits, in new coordinates; nil maps the old selection through.
55 public let selection: [Range<Int>]?
56 public let effects: [Effect]
57
58 public init(baseRevision: Int, edits: [TextEdit], selection: [Range<Int>]? = nil, effects: [Effect] = []) {
59 self.baseRevision = baseRevision
60 self.edits = edits
61 self.selection = selection
62 self.effects = effects
63 }
64}
65
66public enum CommandStep: Sendable, Equatable {
67 case commit(EditResult)
68 /// Ask, then run again with the answer in `EditContext.answers`.
69 case prompt(Prompt)
70 /// The command can't run here; nothing changes. The message is for the user, as org's
71 /// `user-error`.
72 case failed(String)
73}
74
75/// A named operation. Keys, menus, the palette and touch controls all run commands.
76public protocol OrgCommand: Sendable {
77 /// Stable identifier, used by keymaps: `org.todo.cycle`.
78 var id: String { get }
79 /// Shown in the command palette.
80 var title: String { get }
81 /// Whether the command means something at the caret; context dispatch (one key, several
82 /// commands) runs the first that applies.
83 func applies(in context: EditContext) -> Bool
84 func run(in context: EditContext) -> CommandStep
85}
86
87public enum Commands {
88 public static let all: [any OrgCommand] = [
89 TodoCycle(), PriorityUp(), PriorityDown(), PromoteHeading(), DemoteHeading(),
90 ]
91
92 public static func command(_ id: String) -> (any OrgCommand)? {
93 all.first { $0.id == id }
94 }
95}
Sources/OrgCore/Commands/HeadingCommands.swift added +376
@@ -0,0 +1,376 @@
1import Foundation
2
3// Heading-line commands, matched to Emacs 31.1 / Org 9.8.7 by the oracle tests.
4
5/// The heading whose line holds `offset`, for commands that act only on heading lines (org's
6/// M-left, M-right, S-up and S-down do something else elsewhere).
7func headingOnLine(at offset: Int, in tree: OrgTree) -> SyntaxNode? {
8 guard let heading = entryHeading(at: offset, in: tree) else { return nil }
9 let line = HeadingLine(heading)
10 return offset >= line.start && offset <= line.contentEnd ? heading : nil
11}
12
13/// The heading of the entry holding `offset`, as `org-back-to-heading`; nil before the first
14/// heading.
15func entryHeading(at offset: Int, in tree: OrgTree) -> SyntaxNode? {
16 let root = tree.root
17 let position = offset >= root.range.upperBound ? max(0, root.range.upperBound - 1) : offset
18 var node = root
19 var heading: SyntaxNode?
20 while let child = node.child(containing: position), child.kind == .section {
21 heading = child.firstChild(.heading)
22 node = child
23 }
24 return heading
25}
26
27/// The parts of a heading line, in offsets of the whole text.
28struct HeadingLine {
29 let start: Int
30 /// End of the line's content, before its line break.
31 let contentEnd: Int
32 let stars: Range<Int>
33 let todo: SyntaxToken?
34 let priority: SyntaxToken?
35 let tags: SyntaxToken?
36 /// Where the title begins: past the stars, keyword, cookie and the blanks after them.
37 let titleStart: Int
38
39 init(_ heading: SyntaxNode) {
40 let tokens = heading.tokens
41 start = heading.range.lowerBound
42 contentEnd = tokens.first { $0.kind == .newline }?.range.lowerBound ?? heading.range.upperBound
43 stars = tokens.first { $0.kind == .stars }?.range ?? start..<start
44 todo = tokens.first { $0.kind == .todoKeyword }
45 priority = tokens.first { $0.kind == .priority }
46 tags = tokens.first { $0.kind == .tags }
47 var at = start
48 for child in heading.green.children {
49 guard case .token(let token) = child, [.stars, .whitespace, .todoKeyword, .priority].contains(token.kind) else { break }
50 at += token.length
51 }
52 titleStart = min(at, contentEnd)
53 }
54
55 var level: Int { stars.count }
56}
57
58/// One line of text being edited, with a caret that moves the way Emacs moves point and
59/// markers for each kind of edit, so commands land the caret where Emacs does.
60struct LineBuffer {
61 var text: NSMutableString
62 /// Relative to the line start; nil when the caret is elsewhere in the document.
63 var caret: Int?
64
65 init(_ line: String, caret: Int?) {
66 text = NSMutableString(string: line)
67 self.caret = caret
68 }
69
70 var string: String { text as String }
71 var length: Int { text.length }
72
73 private mutating func edit(_ range: Range<Int>, _ replacement: String) -> Int {
74 text.replaceCharacters(in: NSRange(range), with: replacement)
75 return (replacement as NSString).length - range.count
76 }
77
78 /// `replace-match`: a caret strictly inside moves to the start; at or after the end it
79 /// shifts with the text.
80 mutating func replace(_ range: Range<Int>, with replacement: String) {
81 let delta = edit(range, replacement)
82 guard let position = caret else { return }
83 if position >= range.upperBound, position > range.lowerBound {
84 caret = position + delta
85 } else if position > range.lowerBound {
86 caret = range.lowerBound
87 }
88 }
89
90 /// Deleting `range`, then `insert-before-markers`: a caret anywhere from the start to the
91 /// end of the range ends up after the new text.
92 mutating func replaceBeforeMarkers(_ range: Range<Int>, with replacement: String) {
93 let delta = edit(range, replacement)
94 guard let position = caret else { return }
95 if position > range.upperBound {
96 caret = position + delta
97 } else if position >= range.lowerBound {
98 caret = range.lowerBound + (replacement as NSString).length
99 }
100 }
101
102 /// `insert` under `save-excursion`: a caret at the insertion point stays before the text.
103 mutating func insert(_ string: String, at position: Int) {
104 let delta = edit(position..<position, string)
105 if let caret, caret > position { self.caret = caret + delta }
106 }
107
108 /// `insert` at point: the caret moves past the text.
109 mutating func insertAtCaret(_ string: String) {
110 guard let position = caret else { return }
111 let delta = edit(position..<position, string)
112 caret = position + delta
113 }
114
115 /// Offset of display column `target` in the line, as `move-to-column`.
116 func offset(ofColumn target: Int) -> Int {
117 var column = 0
118 var offset = 0
119 for character in string {
120 if column >= target { break }
121 column = character == "\t" ? (column / 8 + 1) * 8 : column + displayWidth(of: character)
122 offset += String(character).utf16.count
123 }
124 return offset
125 }
126}
127
128extension HeadingLine {
129 /// Applies `edit` to the heading line in `context` and maps the caret: on the line it's
130 /// placed by `edit`; after the line it shifts by the change in length.
131 func commit(_ context: EditContext, _ edit: (inout LineBuffer) -> Void) -> CommandStep {
132 let old = (context.text as NSString).substring(with: NSRange(start..<contentEnd))
133 let caretOnLine = context.caret >= start && context.caret <= contentEnd
134 var buffer = LineBuffer(old, caret: caretOnLine ? context.caret - start : nil)
135 edit(&buffer)
136 let new = buffer.string
137 let delta = (new as NSString).length - (old as NSString).length
138 let caret = buffer.caret.map { $0 + start } ?? (context.caret > contentEnd ? context.caret + delta : context.caret)
139 guard new != old else { return .commit(EditResult(baseRevision: context.revision, edits: [], selection: [caret..<caret])) }
140 return .commit(EditResult(
141 baseRevision: context.revision,
142 edits: [TextEdit(range: start..<contentEnd, replacement: new)],
143 selection: [caret..<caret]
144 ))
145 }
146
147 /// Offsets relative to the line start.
148 func local(_ range: Range<Int>) -> Range<Int> { (range.lowerBound - start)..<(range.upperBound - start) }
149}
150
151// MARK: - Tags
152
153/// `org--align-tags-here` with `org-tags-column` -77: tags end at column 77, or sit one blank
154/// after the title when it is too long. Nothing changes when they are already there.
155///
156/// Commands align under `save-excursion`, where the caret is a marker: in the blanks before
157/// the tags it ends up where they start. Aligning tags directly (`preservingColumn`) keeps the
158/// caret's column instead, as org does for point.
159func alignTags(_ buffer: inout LineBuffer, tagsColumn: Int = -77, preservingColumn: Bool = false) {
160 let line = buffer.string
161 guard let match = line.range(of: "[ \\t]+(:[[:alnum:]_@#%]+)+:[ \\t]*$", options: .regularExpression) else { return }
162 let matched = String(line[match])
163 let tags = matched.trimmingCharacters(in: .whitespaces)
164 let blankStart = (String(line[..<match.lowerBound]) as NSString).length
165 let tagsStart = blankStart + (matched as NSString).range(of: tags).location
166 let prefixColumn = column(of: String(line[..<match.lowerBound]))
167 let currentColumn = column(of: (line as NSString).substring(to: tagsStart))
168 let target = tagsColumn >= 0 ? tagsColumn : abs(tagsColumn) - displayWidth(tags)
169 let newColumn = max(target, prefixColumn + 1)
170 guard newColumn != currentColumn else { return }
171 let inBlanks = buffer.caret.map { $0 > blankStart && $0 <= tagsStart } ?? false
172 let caretColumn: Int? = inBlanks && preservingColumn ? column(of: (line as NSString).substring(to: buffer.caret!)) : nil
173 let old = buffer.caret
174 buffer.text.replaceCharacters(in: NSRange(blankStart..<tagsStart), with: String(repeating: " ", count: newColumn - prefixColumn))
175 let delta = (newColumn - prefixColumn) - (tagsStart - blankStart)
176 if let caretColumn {
177 buffer.caret = buffer.offset(ofColumn: caretColumn)
178 } else if inBlanks {
179 buffer.caret = blankStart
180 } else if let old, old > tagsStart {
181 buffer.caret = old + delta
182 }
183}
184
185/// Display column at the end of `text`, starting from column 0.
186func column(of text: String) -> Int {
187 var column = 0
188 for character in text {
189 column = character == "\t" ? (column / 8 + 1) * 8 : column + displayWidth(of: character)
190 }
191 return column
192}
193
194// MARK: - TODO
195
196public struct TodoCycle: OrgCommand {
197 public init() {}
198 public var id: String { "org.todo.cycle" }
199 public var title: String { "Cycle TODO State" }
200
201 public func applies(in context: EditContext) -> Bool {
202 entryHeading(at: context.caret, in: context.tree) != nil
203 }
204
205 /// No keyword, then each keyword of its sequence in order, then no keyword again.
206 static func next(after current: String?, in settings: OrgSettings) -> String? {
207 guard let current else {
208 guard let first = settings.todoSequences.first else { return nil }
209 return (first.active + first.done).first?.name
210 }
211 for sequence in settings.todoSequences {
212 let names = (sequence.active + sequence.done).map(\.name)
213 if let index = names.firstIndex(of: current) {
214 return index + 1 < names.count ? names[index + 1] : nil
215 }
216 }
217 return nil
218 }
219
220 /// As `org-todo`: the blanks after the stars, the keyword and the blanks after it are
221 /// replaced by " NEXT " (or " " for no keyword) with `insert-before-markers`, then tags
222 /// are aligned.
223 public func run(in context: EditContext) -> CommandStep {
224 guard let heading = entryHeading(at: context.caret, in: context.tree) else {
225 return .failed("Before first headline")
226 }
227 let line = HeadingLine(heading)
228 let next = Self.next(after: line.todo?.text, in: context.tree.settings)
229 return line.commit(context) { buffer in
230 let text = buffer.string as NSString
231 let regionStart = line.stars.upperBound - line.start
232 var regionEnd = regionStart
233 while regionEnd < text.length, text.character(at: regionEnd) == 0x20 { regionEnd += 1 }
234 if let todo = line.todo {
235 regionEnd = line.local(todo.range).upperBound
236 var blanks = regionEnd
237 while blanks < text.length, text.character(at: blanks) == 0x20 { blanks += 1 }
238 if blanks > regionEnd {
239 regionEnd = blanks
240 } else if text.substring(from: regionEnd).allSatisfy({ $0 == " " || $0 == "\t" }) {
241 regionEnd = text.length
242 }
243 }
244 buffer.replaceBeforeMarkers(regionStart..<regionEnd, with: next.map { " \($0) " } ?? " ")
245 alignTags(&buffer)
246 }
247 }
248}
249
250// MARK: - Priority
251
252struct PriorityChange {
253 let up: Bool
254
255 func run(in context: EditContext) -> CommandStep {
256 guard let heading = headingOnLine(at: context.caret, in: context.tree) else {
257 return .failed("Not on a heading")
258 }
259 let line = HeadingLine(heading)
260 let priorities = context.tree.settings.priorities
261 guard let highest = Self.value(priorities.highest), let lowest = Self.value(priorities.lowest) else {
262 return .failed("Unsupported priority range")
263 }
264 let numeric = Int(priorities.highest) != nil
265 let current = line.priority.flatMap { Self.value(String($0.text.dropFirst(2).dropLast())) }
266 // From no cookie, the first press sets the default, as `org-priority-start-cycle-with-default`.
267 var new: Int?
268 if let current {
269 new = up ? current - 1 : current + 1
270 if let value = new, value < highest || value > lowest { new = nil }
271 } else {
272 new = Self.value(priorities.default)
273 }
274 let cookie = new.map { "[#\(Self.label($0, numeric: numeric))]" }
275 return line.commit(context) { buffer in
276 if let existing = line.priority {
277 let range = line.local(existing.range)
278 if let cookie {
279 buffer.replace((range.lowerBound + 2)..<(range.upperBound - 1), with: String(cookie.dropFirst(2).dropLast()))
280 } else {
281 // The cookie and one following space, as `org-priority-regexp` group 1.
282 let text = buffer.string as NSString
283 let space = range.upperBound < text.length && text.character(at: range.upperBound) == 0x20 ? 1 : 0
284 buffer.replace(range.lowerBound..<(range.upperBound + space), with: "")
285 }
286 } else if let cookie {
287 if let todo = line.todo {
288 buffer.insert(" " + cookie, at: line.local(todo.range).upperBound)
289 } else {
290 buffer.insert(cookie + " ", at: line.titleStart - line.start)
291 }
292 }
293 alignTags(&buffer)
294 }
295 }
296
297 /// A priority as a number: a letter's code point, or the number itself.
298 static func value(_ text: String) -> Int? {
299 if let number = Int(text) { return number }
300 guard text.unicodeScalars.count == 1, let scalar = text.unicodeScalars.first else { return nil }
301 return Int(scalar.value)
302 }
303
304 static func label(_ value: Int, numeric: Bool) -> String {
305 numeric ? String(value) : String(Character(Unicode.Scalar(UInt32(value))!))
306 }
307}
308
309public struct PriorityUp: OrgCommand {
310 public init() {}
311 public var id: String { "org.priority.up" }
312 public var title: String { "Raise Priority" }
313 public func applies(in context: EditContext) -> Bool { headingOnLine(at: context.caret, in: context.tree) != nil }
314 public func run(in context: EditContext) -> CommandStep { PriorityChange(up: true).run(in: context) }
315}
316
317public struct PriorityDown: OrgCommand {
318 public init() {}
319 public var id: String { "org.priority.down" }
320 public var title: String { "Lower Priority" }
321 public func applies(in context: EditContext) -> Bool { headingOnLine(at: context.caret, in: context.tree) != nil }
322 public func run(in context: EditContext) -> CommandStep { PriorityChange(up: false).run(in: context) }
323}
324
325// MARK: - Promote and demote
326
327public struct PromoteHeading: OrgCommand {
328 public init() {}
329 public var id: String { "org.heading.promote" }
330 public var title: String { "Promote Heading" }
331 public func applies(in context: EditContext) -> Bool { headingOnLine(at: context.caret, in: context.tree) != nil }
332
333 public func run(in context: EditContext) -> CommandStep {
334 guard let heading = headingOnLine(at: context.caret, in: context.tree) else { return .failed("Not on a heading") }
335 let line = HeadingLine(heading)
336 guard line.level > 1 else { return .failed("Cannot promote to level 0") }
337 return line.commit(context) { buffer in
338 buffer.replace(0..<(line.level + 1), with: String(repeating: "*", count: line.level - 1) + " ")
339 alignTags(&buffer)
340 fixPositionAfterPromote(&buffer, settings: context.tree.settings)
341 }
342 }
343}
344
345public struct DemoteHeading: OrgCommand {
346 public init() {}
347 public var id: String { "org.heading.demote" }
348 public var title: String { "Demote Heading" }
349 public func applies(in context: EditContext) -> Bool { headingOnLine(at: context.caret, in: context.tree) != nil }
350
351 public func run(in context: EditContext) -> CommandStep {
352 guard let heading = headingOnLine(at: context.caret, in: context.tree) else { return .failed("Not on a heading") }
353 let line = HeadingLine(heading)
354 return line.commit(context) { buffer in
355 buffer.replace(0..<(line.level + 1), with: String(repeating: "*", count: line.level + 1) + " ")
356 alignTags(&buffer)
357 fixPositionAfterPromote(&buffer, settings: context.tree.settings)
358 }
359 }
360}
361
362/// `org-fix-position-after-promote`: a caret right after the stars or the keyword steps over
363/// the following space, or adds one at the end of the line.
364func fixPositionAfterPromote(_ buffer: inout LineBuffer, settings: OrgSettings) {
365 guard let caret = buffer.caret else { return }
366 let tree = OrgParser.parse(buffer.string + "\n", defaults: settings)
367 guard let heading = tree.root.firstChild(.section)?.firstChild(.heading) else { return }
368 let line = HeadingLine(heading)
369 let anchors = [line.stars.upperBound, line.todo?.range.upperBound].compactMap { $0 }
370 guard anchors.contains(caret) else { return }
371 if caret == buffer.length {
372 buffer.insertAtCaret(" ")
373 } else if buffer.text.character(at: caret) == 0x20 {
374 buffer.caret = caret + 1
375 }
376}
Sources/OrgCore/Parser/Incremental.swift +2 −2
@@ -40,7 +40,7 @@ extension OrgParser {
4040 _ old: OrgTree, oldText: String, edit: TextEdit, defaults: OrgSettings = .default
4141 ) -> (tree: OrgTree, strategy: ReparseStrategy) {
4242 let newText = edit.apply(to: oldText)
43 let context = EditContext(oldText: oldText, newText: newText, edit: edit)
43 let context = ReparseContext(oldText: oldText, newText: newText, edit: edit)
4444 if context.touchesSettings() { return (parse(newText, defaults: defaults), .full) }
4545 if let tree = context.reparseElement(old) { return (tree, .element) }
4646 if let tree = context.reparseRegion(old) { return (tree, .region) }
@@ -49,7 +49,7 @@ extension OrgParser {
4949 }
5050}
5151
52private struct EditContext {
52private struct ReparseContext {
5353 let oldText: String
5454 let newText: String
5555 let edit: TextEdit
Tests/OrgCoreTests/EmacsOracle.swift added +132
@@ -0,0 +1,132 @@
1import Foundation
2@testable import OrgCore
3
4/// Runs cases through `emacs -Q --batch` with Org's settings pinned, so commands can be
5/// compared with Emacs byte for byte. One Emacs process runs a whole batch.
6///
7/// Pinned versions: Emacs 31.1, Org 9.8.7 (design, "Testing"). `ORGSTAR_EMACS` overrides the
8/// executable.
9enum EmacsOracle {
10 struct Case: Codable {
11 var text: String
12 /// Emacs point: 1 plus the number of characters (code points) before it.
13 var point: Int
14 /// An Emacs Lisp form to run with point there.
15 var form: String
16 }
17
18 struct Result: Codable, Equatable {
19 var text: String
20 var point: Int
21 /// The error message, or empty.
22 var error: String
23 }
24
25 static let script = #"""
26 ;;; -*- lexical-binding: t -*-
27 (require 'org)
28 (require 'json)
29 (setq org-todo-keywords '((sequence "TODO" "DONE"))
30 org-log-done nil
31 org-log-repeat nil
32 org-adapt-indentation nil
33 org-tags-column -77
34 org-priority-highest ?A
35 org-priority-lowest ?C
36 org-priority-default ?B
37 indent-tabs-mode nil)
38 (let* ((input (with-temp-buffer
39 (let ((coding-system-for-read 'utf-8-unix))
40 (insert-file-contents (getenv "ORACLE_INPUT")))
41 (json-parse-buffer :object-type 'alist :array-type 'list)))
42 (results nil))
43 (dolist (c input)
44 (with-temp-buffer
45 (insert (alist-get 'text c))
46 (org-mode)
47 (goto-char (alist-get 'point c))
48 ;; As an interactive call, not a repeat: some commands cycle differently on repeats.
49 (setq this-command 'orgstar-oracle last-command nil)
50 (let ((err (condition-case e
51 (progn (eval (car (read-from-string (alist-get 'form c))) t) "")
52 (error (error-message-string e)))))
53 (push `((text . ,(buffer-substring-no-properties (point-min) (point-max)))
54 (point . ,(point))
55 (error . ,err))
56 results))))
57 (let ((coding-system-for-write 'utf-8-unix))
58 (with-temp-file (getenv "ORACLE_OUTPUT")
59 (insert (json-encode (nreverse results))))))
60 """#
61
62 static var executable: String { ProcessInfo.processInfo.environment["ORGSTAR_EMACS"] ?? "emacs" }
63
64 static var isAvailable: Bool {
65 (try? run(["--version"]))?.contains("GNU Emacs") ?? false
66 }
67
68 static func run(_ cases: [Case]) throws -> [Result] {
69 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-oracle-\(UUID().uuidString)")
70 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
71 defer { try? FileManager.default.removeItem(at: folder) }
72 let scriptURL = folder.appendingPathComponent("oracle.el")
73 let input = folder.appendingPathComponent("input.json")
74 let output = folder.appendingPathComponent("output.json")
75 try script.write(to: scriptURL, atomically: true, encoding: .utf8)
76 try JSONEncoder().encode(cases).write(to: input)
77 _ = try run(["-Q", "--batch", "-l", scriptURL.path], environment: ["ORACLE_INPUT": input.path, "ORACLE_OUTPUT": output.path])
78 return try JSONDecoder().decode([Result].self, from: Data(contentsOf: output))
79 }
80
81 @discardableResult
82 static func run(_ arguments: [String], environment: [String: String] = [:]) throws -> String {
83 let process = Process()
84 process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
85 process.arguments = [executable] + arguments
86 process.environment = ProcessInfo.processInfo.environment.merging(environment) { $1 }
87 let pipe = Pipe()
88 process.standardOutput = pipe
89 process.standardError = pipe
90 try process.run()
91 let data = pipe.fileHandleForReading.readDataToEndOfFile()
92 process.waitUntilExit()
93 return String(decoding: data, as: UTF8.self)
94 }
95
96 /// UTF-16 offset to Emacs point.
97 static func point(_ offset: Int, in text: String) -> Int {
98 let index = String.Index(utf16Offset: offset, in: text)
99 return text.unicodeScalars[..<index].count + 1
100 }
101
102 /// Emacs point to UTF-16 offset.
103 static func offset(_ point: Int, in text: String) -> Int {
104 text.unicodeScalars.prefix(point - 1).reduce(0) { $0 + $1.utf16.count }
105 }
106
107 /// UTF-16 offsets of every unicode scalar boundary, including the end.
108 static func positions(_ text: String) -> [Int] {
109 var positions = [0]
110 var offset = 0
111 for scalar in text.unicodeScalars {
112 offset += scalar.utf16.count
113 positions.append(offset)
114 }
115 return positions
116 }
117}
118
119/// Runs a command the way an editor would: build a context, run, apply the edits.
120func runCommand(_ command: any OrgCommand, _ text: String, caret: Int) -> (text: String, caret: Int, failure: String?) {
121 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret])
122 switch command.run(in: context) {
123 case .commit(let result):
124 var new = text
125 for edit in result.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { new = edit.apply(to: new) }
126 return (new, result.selection?.first?.lowerBound ?? caret, nil)
127 case .failed(let message):
128 return (text, caret, message)
129 case .prompt(let prompt):
130 return (text, caret, "prompt: \(prompt.key)")
131 }
132}
Tests/OrgCoreTests/HeadingCommandTests.swift added +125
@@ -0,0 +1,125 @@
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct HeadingCommandTests {
6 @Test func todoCycle() {
7 let cycle = TodoCycle()
8 #expect(runCommand(cycle, "* a\n", caret: 2).text == "* TODO a\n")
9 #expect(runCommand(cycle, "* TODO a\n", caret: 2).text == "* DONE a\n")
10 #expect(runCommand(cycle, "* DONE a\n", caret: 2).text == "* a\n")
11 #expect(runCommand(cycle, "* TODO\n", caret: 2).text == "* DONE \n")
12 #expect(runCommand(cycle, "#+TODO: A B | C\n* B x\n", caret: 18).text == "#+TODO: A B | C\n* C x\n")
13 #expect(runCommand(cycle, "text\n", caret: 1).failure != nil)
14 }
15
16 @Test func caretMovesToTheTitleWhenBeforeIt() {
17 #expect(runCommand(TodoCycle(), "* a\n", caret: 2).caret == 7)
18 #expect(runCommand(TodoCycle(), "* TODO abc\nbody\n", caret: 13).caret == 13)
19 }
20
21 @Test func priorities() {
22 #expect(runCommand(PriorityUp(), "* a\n", caret: 2).text == "* [#B] a\n")
23 #expect(runCommand(PriorityUp(), "* [#A] a\n", caret: 2).text == "* a\n")
24 #expect(runCommand(PriorityDown(), "* TODO a\n", caret: 2).text == "* TODO [#B] a\n")
25 #expect(runCommand(PriorityDown(), "* [#C] a\n", caret: 2).text == "* a\n")
26 }
27
28 @Test func promoteAndDemote() {
29 #expect(runCommand(DemoteHeading(), "* a\nbody\n", caret: 2).text == "** a\nbody\n")
30 #expect(runCommand(PromoteHeading(), "** a\n", caret: 3).text == "* a\n")
31 #expect(runCommand(PromoteHeading(), "* a\n", caret: 0).failure != nil)
32 #expect(runCommand(DemoteHeading(), "* a\nbody\n", caret: 6).failure != nil)
33 }
34
35 @Test func tagsAlignToColumn77() {
36 let result = runCommand(PriorityUp(), "* TODO a :t:\n", caret: 2).text
37 #expect(result == "* TODO [#B] a" + String(repeating: " ", count: 77 - 13 - 3) + ":t:\n")
38 }
39}
40
41/// Every heading variant, every caret position, every heading command, against Emacs.
42/// Fails when Emacs is missing unless `ORGSTAR_SKIP_ORACLE` is set.
43struct HeadingOracleTests {
44 static let variants = [
45 "* a\nbody\n", "* TODO a\n", "* DONE a\n", "** [#B] two words :tag:\n", "* TODO [#A] x :a:b:\nbody\n",
46 "* \n", "* TODO\n", "*** title with 日本 :t:\n", "* a😀 b\n", "* a :t:\n", "text\n* a\n", "* a\n",
47 "* TODO a\n", "* a\r\n",
48 "#+TODO: NEXT WAIT | DONE CANCELED\n* WAIT a :t:\n",
49 "* " + String(repeating: "long ", count: 16) + "title :t:\n",
50 "* TODO\ttab\n",
51 ]
52
53 /// Commands, their Emacs forms, and whether they only act on heading lines.
54 static let commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)] = [
55 (TodoCycle(), "(org-todo)", false),
56 (PriorityUp(), "(org-priority-up)", true),
57 (PriorityDown(), "(org-priority-down)", true),
58 (PromoteHeading(), "(org-do-promote)", true),
59 (DemoteHeading(), "(org-do-demote)", true),
60 ]
61
62 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
63 func headingCommandsMatchEmacs() throws {
64 try compare(Self.variants.map { ($0, EmacsOracle.positions($0)) })
65 }
66
67 /// Real headings: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every command on up to 400 heading
68 /// lines from the folder's files, with the caret at the line start, the title start and
69 /// the line end.
70 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
71 func headingCommandsMatchEmacsOnACorpus() throws {
72 let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
73 var samples: [(String, [Int])] = []
74 let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
75 .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
76 for file in files where samples.count < 400 {
77 guard let text = try? String(contentsOf: file, encoding: .utf8) else { continue }
78 var settings = ""
79 for line in text.components(separatedBy: "\n") where line.uppercased().hasPrefix("#+TODO:") || line.uppercased().hasPrefix("#+SEQ_TODO:") {
80 settings += line + "\n"
81 }
82 for line in text.components(separatedBy: "\n") where line.hasPrefix("*") && samples.count < 400 {
83 let sample = settings + line + "\n"
84 guard let heading = entryHeading(at: (sample as NSString).length - 1, in: OrgParser.parse(sample)) else { continue }
85 let parts = HeadingLine(heading)
86 samples.append((sample, [parts.start, parts.titleStart, parts.contentEnd]))
87 }
88 }
89 try compare(samples)
90 }
91
92 func compare(_ inputs: [(text: String, carets: [Int])]) throws {
93 try #require(EmacsOracle.isAvailable, "Emacs is required for the oracle tests; set ORGSTAR_SKIP_ORACLE to skip")
94 var cases: [EmacsOracle.Case] = []
95 var ours: [(label: String, text: String, caret: Int, failed: Bool)] = []
96 for (text, carets) in inputs {
97 for offset in carets {
98 let onHeadingLine = headingOnLine(at: offset, in: OrgParser.parse(text)) != nil
99 for (command, form, headingLineOnly) in Self.commands where onHeadingLine || !headingLineOnly {
100 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(offset, in: text), form: form))
101 let result = runCommand(command, text, caret: offset)
102 ours.append(("\(command.id) at \(offset) in \(text.debugDescription)", result.text, result.caret, result.failure != nil))
103 }
104 }
105 }
106 let theirs = try EmacsOracle.run(cases)
107 var mismatches = 0
108 for (mine, emacs) in zip(ours, theirs) {
109 let emacsFailed = !emacs.error.isEmpty
110 let emacsCaret = EmacsOracle.offset(emacs.point, in: emacs.text)
111 let same = mine.failed == emacsFailed && (emacsFailed || (mine.text == emacs.text && mine.caret == emacsCaret))
112 if !same {
113 mismatches += 1
114 if mismatches <= 15 {
115 Issue.record("""
116 \(mine.label)
117 ours: \(mine.failed ? "failed" : "\(mine.text.debugDescription) @\(mine.caret)")
118 emacs: \(emacsFailed ? "failed: \(emacs.error)" : "\(emacs.text.debugDescription) @\(emacsCaret)")
119 """)
120 }
121 }
122 }
123 #expect(mismatches == 0, "\(mismatches) of \(ours.count) cases differ from Emacs")
124 }
125}