Sources/Orgstar/EditorView.swift

99566e2d13b805e89149c6338d59d0b6729549ec
orgstar/Sources/Orgstar/EditorView.swift history · blame · raw

77 lines · 3287 bytes

 1import AppKit
 2import OrgApp
 3import OrgCore
 4import OrgEditorAppKit
 5import OrgHighlight
 6import SwiftUI
 7
 8/// The TextKit 2 editor for the session's file. A new file gets a new editor, which becomes the
 9/// session's buffer; reveal requests go to the existing one.
10struct EditorView: NSViewRepresentable {
11    /// One highlighter for every editor: it caches compiled grammars and results.
12    static let highlighter = TreeSitterHighlighter()
13
14    let session: DocumentSession
15    let commands: AppCommands
16    let keymap: Keymap
17    let modal: Bool
18    let optionAsMeta: OptionAsMeta
19    let options: EditingOptions
20    let onAppCommand: (String) -> Void
21    let onExternal: (ExternalRequest) -> Void
22
23    final class Coordinator {
24        var editor: OrgEditor?
25        var path: String?
26        var revealID = 0
27    }
28
29    func makeCoordinator() -> Coordinator { Coordinator() }
30
31    func makeNSView(context: Context) -> NSView {
32        NSView()
33    }
34
35    func updateNSView(_ container: NSView, context: Context) {
36        let coordinator = context.coordinator
37        if coordinator.path != session.path {
38            coordinator.path = session.path
39            container.subviews.forEach { $0.removeFromSuperview() }
40            coordinator.editor = nil
41            if let document = session.document {
42                let mode: OrgEditor.Mode = session.isOrg ? .org : .plain(language: session.path.flatMap(FileTypes.language))
43                let editor = OrgEditor(document: document, frame: container.bounds, editable: document.isEditable, highlighter: Self.highlighter, mode: mode)
44                editor.onChange = { [weak session] in session?.bufferChanged() }
45                editor.onMessage = { [weak commands] in commands?.show($0) }
46                editor.onKeysPending = { [weak commands] keys, next in
47                    commands?.pendingKeys = keys
48                    commands?.keyHints = next.map { KeyHint(key: $0.key, title: $0.title) }
49                }
50                editor.onModeChange = { [weak commands] in commands?.editorState = $0 }
51                editor.onSelectionChange = { [weak commands] in commands?.selection = $0 }
52                editor.onAppCommand = onAppCommand
53                editor.onExternal = onExternal
54                editor.onPrompt = { [weak commands] prompt, reply in commands?.ask(prompt, reply: reply) }
55                commands.editor = editor
56                let scroll = editor.makeScrollView()
57                scroll.frame = container.bounds
58                scroll.autoresizingMask = [.width, .height]
59                container.addSubview(scroll)
60                container.window?.makeFirstResponder(editor.textView)
61                coordinator.editor = editor
62                session.attach(editor)
63            }
64        }
65        if let editor = coordinator.editor {
66            if editor.keymap != keymap { editor.keymap = keymap }
67            editor.modal = modal
68            editor.optionAsMeta = optionAsMeta
69            editor.options = options
70        }
71        container.window?.isDocumentEdited = session.isDirty
72        if let request = session.revealRequest, request.id != coordinator.revealID {
73            coordinator.revealID = request.id
74            coordinator.editor?.reveal(request.offset)
75        }
76    }
77}