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