Commit a15a7c8375

a15a7c83754a4ddcc5ba21c6f5e2ab5977f728d7

parent: cfd1ff17f1

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-10 17:29 UTC

Mac agenda refreshes without redrawing on edits; Mac stall watchdog

- The Mac agenda's refresh is its own view watching files and edits, and
  waits out bursts, as on iOS.
- The main-thread watchdog runs in Debug builds and with ORGSTAR_PERF on
  the Mac too.
- Pretty-entity scanning skips comment lines without a regex per line.

Layout: unified · split

Sources/OrgPresentation/Display.swift +10 −1
@@ -131,7 +131,7 @@ extension Presentation {
131 location = NSMaxRange(line) 131 location = NSMaxRange(line)
132 if line.location == revealedLine || inBlock(line.location) { continue } 132 if line.location == revealedLine || inBlock(line.location) { continue }
133 let content = text.substring(with: line) 133 let content = text.substring(with: line)
134 if content.range(of: "^[ \\t]*#([ +]|$)", options: .regularExpression) != nil { continue } 134 if Self.isCommentOrKeyword(content) { continue }
135 for run in PrettyEntities.runs(in: content, subSuperscriptsNeedBraces: subSuperscriptsNeedBraces) { 135 for run in PrettyEntities.runs(in: content, subSuperscriptsNeedBraces: subSuperscriptsNeedBraces) {
136 if case .script(_, let body, _) = run { 136 if case .script(_, let body, _) = run {
137 let bodyStart = line.location + body.lowerBound 137 let bodyStart = line.location + body.lowerBound
@@ -143,6 +143,15 @@ extension Presentation {
143 return out 143 return out
144 } 144 }
145 145
146 /// `^[ \t]*#([ +]|$)`: a comment or keyword line, which shows no entities.
147 static func isCommentOrKeyword(_ line: String) -> Bool {
148 let body = line.drop { $0 == " " || $0 == "\t" }
149 guard body.first == "#" else { return false }
150 let next = body.dropFirst().first
151 // `$` only at the end: the pattern ran on the line with its newline.
152 return next == nil || next == " " || next == "+"
153 }
154
146 /// Roles whose text keeps sub/superscript markers as written. 155 /// Roles whose text keeps sub/superscript markers as written.
147 public static func excludesScripts(_ role: StyleRole) -> Bool { 156 public static func excludesScripts(_ role: StyleRole) -> Bool {
148 switch role { 157 switch role {
Sources/Orgstar/AgendaView.swift +45 −18
@@ -131,14 +131,56 @@ struct AgendaView: View {
131 var tick: Int 131 var tick: Int
132 } 132 }
133 133
134 /// The settings part of the refresh key; `Refresher` adds the files and edits, so that
135 /// they redraw only it.
134 private var refreshKey: RefreshKey { 136 private var refreshKey: RefreshKey {
135 RefreshKey( 137 RefreshKey(
136 mode: agenda.mode, start: agenda.start, span: span, startOffset: startOffset, includeSubfolders: includeSubfolders, prefix: [prefixAgenda, prefixTodo, prefixTags], logMode: logMode, 138 mode: agenda.mode, start: agenda.start, span: span, startOffset: startOffset, includeSubfolders: includeSubfolders, prefix: [prefixAgenda, prefixTodo, prefixTags], logMode: logMode,
137 calendars: calendarEventsOn ? [calendarNames] : [], files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion, 139 calendars: calendarEventsOn ? [calendarNames] : [], files: 0, changed: nil, edits: 0, path: nil, tick: tick
138 path: session.path, tick: tick
139 ) 140 )
140 } 141 }
141 142
143 private func refresh() async {
144 agenda.span = span
145 agenda.startOffset = startOffset
146 agenda.includeSubfolders = includeSubfolders
147 agenda.options.prefixFormat = ["agenda": prefixAgenda, "todo": prefixTodo, "tags": prefixTags]
148 agenda.options.logMode = logMode
149 agenda.options = OrgPreferences.agendaOptions(agenda.options)
150 agenda.calendarNames = OrgPreferences.calendarEventCalendars()
151 if calendarEventsOn, isAgenda {
152 agenda.calendarEvents = await CalendarAccess.events.requestAccess() ? CalendarAccess.events : nil
153 } else {
154 agenda.calendarEvents = nil
155 }
156 await agenda.refresh(open: session.openTexts)
157 }
158
159 /// Refreshes the agenda when the settings, files or edits change, once a burst of them ends.
160 private struct Refresher: View {
161 let base: RefreshKey
162 let session: DocumentSession
163 let workspace: WorkspaceModel
164 let refresh: () async -> Void
165
166 private var key: RefreshKey {
167 var key = base
168 key.files = workspace.files.count
169 key.changed = workspace.changedFile?.version
170 key.edits = session.editVersion
171 key.path = session.path
172 return key
173 }
174
175 var body: some View {
176 Color.clear.task(id: key) {
177 try? await Task.sleep(for: .milliseconds(150))
178 guard !Task.isCancelled else { return }
179 await refresh()
180 }
181 }
182 }
183
142 private var isAgenda: Bool { 184 private var isAgenda: Bool {
143 if case .agenda = agenda.mode { return true } 185 if case .agenda = agenda.mode { return true }
144 return false 186 return false
@@ -282,22 +324,7 @@ struct AgendaView: View {
282 .onChange(of: viewName) { applyView() } 324 .onChange(of: viewName) { applyView() }
283 .onChange(of: commands.agendaRequest?.start, initial: true) { showRequestedDays() } 325 .onChange(of: commands.agendaRequest?.start, initial: true) { showRequestedDays() }
284 .onChange(of: commands.agendaRequest?.days) { showRequestedDays() } 326 .onChange(of: commands.agendaRequest?.days) { showRequestedDays() }
285 .task(id: refreshKey) { 327 .background { Refresher(base: refreshKey, session: session, workspace: workspace, refresh: refresh) }
286 agenda.span = span
287 agenda.startOffset = startOffset
288 agenda.includeSubfolders = includeSubfolders
289 agenda.options.prefixFormat = ["agenda": prefixAgenda, "todo": prefixTodo, "tags": prefixTags]
290 agenda.options.logMode = logMode
291 agenda.options = OrgPreferences.agendaOptions(agenda.options)
292 agenda.calendarNames = OrgPreferences.calendarEventCalendars()
293 if calendarEventsOn, isAgenda {
294 agenda.calendarEvents = await CalendarAccess.events.requestAccess() ? CalendarAccess.events : nil
295 } else {
296 agenda.calendarEvents = nil
297 }
298 let open = session.openTexts
299 await agenda.refresh(open: open)
300 }
301 .task { 328 .task {
302 loadViews() 329 loadViews()
303 // The now line moves each minute; a new day refreshes the statuses. 330 // The now line moves each minute; a new day refreshes the statuses.
Sources/Orgstar/OrgstarApp.swift +9
@@ -292,6 +292,15 @@ struct OrgstarApp: App {
292 return session 292 return session
293 }() 293 }()
294 294
295 init() {
296 // Debug builds, and runs with ORGSTAR_PERF set: main-thread stalls in the console.
297 #if DEBUG
298 Perf.watchMainThread()
299 #else
300 if ProcessInfo.processInfo.environment["ORGSTAR_PERF"] != nil { Perf.watchMainThread() }
301 #endif
302 }
303
295 var body: some Scene { 304 var body: some Scene {
296 Window("Orgstar", id: "main") { 305 Window("Orgstar", id: "main") {
297 switch workspace { 306 switch workspace {
Tests/OrgPresentationTests/CommentLineTests.swift added +13
@@ -0,0 +1,13 @@
1import Foundation
2import Testing
3@testable import OrgPresentation
4
5struct CommentLineTests {
6 @Test func matchesThePattern() {
7 let lines = ["#", "# x", "#+TITLE: a", " #\n", "\t# \n", "#x", "a # b", "", "#\r\n", "#\r", "#\u{2028}", " #+\n", "##", " x#"]
8 for line in lines {
9 let expected = line.range(of: "^[ \\t]*#([ +]|$)", options: .regularExpression) != nil
10 #expect(Presentation.isCommentOrKeyword(line) == expected, "\(line.debugDescription)")
11 }
12 }
13}