Commit ab3da01d50

ab3da01d504ff1f7e1830a99eeceaf79c7313e48

parent: 915dbdd25a

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-11 08:04 UTC

iOS: highlight src blocks in the block-edit sheet

The C-c ' sheet edits in a text view restyled after each change with the editor's tree-sitter highlighter and theme colors.

Closes #40

Layout: unified · split

Sources/OrgstarMobile/EditorScreen.swift +73 −17
@@ -106,7 +106,7 @@ struct EditorScreen: View {
106 case .syncCopies: SyncConflictSheet(session: session) { message = $0 } 106 case .syncCopies: SyncConflictSheet(session: session) { message = $0 }
107 case .recovery: RecoverySheet(session: session) { message = $0 } 107 case .recovery: RecoverySheet(session: session) { message = $0 }
108 case .block(let edit): 108 case .block(let edit):
109 BlockEditSheet(edit: edit) { content in 109 BlockEditSheet(edit: edit, theme: MobileTheme(model.theme)) { content in
110 self.sheet = nil 110 self.sheet = nil
111 if let content, let editor { _ = editor.perform(ReplaceBlockLines(edit: edit, content: content)) } 111 if let content, let editor { _ = editor.perform(ReplaceBlockLines(edit: edit, content: content)) }
112 } 112 }
@@ -387,30 +387,86 @@ struct PromptSheet: View {
387} 387}
388 388
389/// `org-edit-special` and `org-table-edit-field`: the text edited apart, put back with Save. 389/// `org-edit-special` and `org-table-edit-field`: the text edited apart, put back with Save.
390/// A src block's code is highlighted as its language.
390struct BlockEditSheet: View { 391struct BlockEditSheet: View {
391 let edit: BlockEdit 392 let edit: BlockEdit
393 let theme: MobileTheme
392 let finish: (String?) -> Void 394 let finish: (String?) -> Void
393 @State private var text = "" 395 @State private var text: String?
394 @FocusState private var focused: Bool
395 396
396 var body: some View { 397 var body: some View {
397 NavigationStack { 398 NavigationStack {
398 TextEditor(text: $text) 399 Group {
399 .font(.body.monospaced()) 400 if let text {
400 .autocorrectionDisabled() 401 CodeTextView(text: text, language: edit.language, theme: theme) { self.text = $0 }
401 .textInputAutocapitalization(.never)
402 .focused($focused)
403 .padding(.horizontal, 8)
404 .navigationTitle(edit.title.replacingOccurrences(of: "Editing ", with: "").capitalized(with: nil))
405 .navigationBarTitleDisplayMode(.inline)
406 .toolbar {
407 ToolbarItem(placement: .cancellationAction) { Button("Cancel") { finish(nil) } }
408 ToolbarItem(placement: .confirmationAction) { Button("Save") { finish(text) } }
409 } 402 }
403 }
404 .padding(.horizontal, 8)
405 .navigationTitle(edit.title.replacingOccurrences(of: "Editing ", with: "").capitalized(with: nil))
406 .navigationBarTitleDisplayMode(.inline)
407 .toolbar {
408 ToolbarItem(placement: .cancellationAction) { Button("Cancel") { finish(nil) } }
409 ToolbarItem(placement: .confirmationAction) { Button("Save") { finish(text ?? edit.content) } }
410 }
410 } 411 }
411 .onAppear { 412 .onAppear { text = edit.content }
412 text = edit.content 413 }
413 focused = true 414}
415
416/// A text view for code, restyled with the editor's highlighter after each change.
417struct CodeTextView: UIViewRepresentable {
418 let text: String
419 let language: String?
420 let theme: MobileTheme
421 let changed: (String) -> Void
422
423 func makeUIView(context: Context) -> UITextView {
424 let view = UITextView()
425 view.autocorrectionType = .no
426 view.autocapitalizationType = .none
427 view.smartQuotesType = .no
428 view.smartDashesType = .no
429 view.spellCheckingType = .no
430 view.keyboardDismissMode = .interactive
431 view.backgroundColor = .clear
432 view.text = text
433 view.delegate = context.coordinator
434 context.coordinator.style(view)
435 DispatchQueue.main.async { view.becomeFirstResponder() }
436 return view
437 }
438
439 func updateUIView(_ view: UITextView, context: Context) {
440 context.coordinator.parent = self
441 }
442
443 func makeCoordinator() -> Coordinator { Coordinator(self) }
444
445 @MainActor
446 final class Coordinator: NSObject, UITextViewDelegate {
447 var parent: CodeTextView
448 init(_ parent: CodeTextView) { self.parent = parent }
449
450 func textViewDidChange(_ view: UITextView) {
451 parent.changed(view.text)
452 style(view)
453 }
454
455 /// The theme's font and color, then the language's token colors.
456 func style(_ view: UITextView) {
457 let storage = view.textStorage
458 let whole = NSRange(location: 0, length: storage.length)
459 let base: [NSAttributedString.Key: Any] = [.font: parent.theme.font, .foregroundColor: parent.theme.foreground]
460 storage.beginEditing()
461 storage.setAttributes(base, range: whole)
462 if let language = parent.language {
463 for run in MobileEditor.highlighter.highlights(language: language, code: storage.string) {
464 guard let key = run.category.themeKey, let color = parent.theme.color(key), run.range.upperBound <= storage.length else { continue }
465 storage.addAttribute(.foregroundColor, value: color, range: NSRange(location: run.range.lowerBound, length: run.range.count))
466 }
467 }
468 storage.endEditing()
469 view.typingAttributes = base
414 } 470 }
415 } 471 }
416} 472}
docs/manual/guide/14-ios.org +1 −1
@@ -130,7 +130,7 @@ When a command asks a question, a sheet opens:
130 130
131Cancel answers nothing, as =C-g= does. 131Cancel answers nothing, as =C-g= does.
132 132
133=C-c '= on a block, and =C-c `= on a table field, open the text in a sheet of its own; Save puts it back. 133=C-c '= on a block, and =C-c `= on a table field, open the text in a sheet of its own; Save puts it back. A src block's code is colored as its language, as in the editor.
134 134
135** Hardware keyboard 135** Hardware keyboard
136 136