|
| 1 | +// |
| 2 | +// CodeEditor.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Marco Carnevali on 19/03/22. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import AppKit |
| 10 | +import SwiftUI |
| 11 | +import Highlightr |
| 12 | +import Combine |
| 13 | + |
| 14 | +struct CodeEditor: NSViewRepresentable { |
| 15 | + @ObservedObject private var codeFile: CodeFileDocument |
| 16 | + private var lineGutter: LineGutter? |
| 17 | + |
| 18 | + private let highlightr = Highlightr() |
| 19 | + private var view = NSView() |
| 20 | + private let theme: CodeFileView.Theme |
| 21 | + |
| 22 | + private var textView: CodeEditorTextView? { |
| 23 | + let scrollView = view.subviews.first as? NSScrollView |
| 24 | + return scrollView?.documentView as? CodeEditorTextView |
| 25 | + } |
| 26 | + |
| 27 | + private lazy var scrollView: NSScrollView = { |
| 28 | + let scrollView = NSScrollView() |
| 29 | + scrollView.drawsBackground = true |
| 30 | + scrollView.borderType = .noBorder |
| 31 | + scrollView.hasVerticalScroller = true |
| 32 | + scrollView.hasHorizontalRuler = false |
| 33 | + scrollView.autoresizingMask = [.width, .height] |
| 34 | + scrollView.translatesAutoresizingMaskIntoConstraints = false |
| 35 | + return scrollView |
| 36 | + }() |
| 37 | + |
| 38 | + init( |
| 39 | + codeFile: CodeFileDocument, |
| 40 | + theme: CodeFileView.Theme |
| 41 | + ) { |
| 42 | + self.codeFile = codeFile |
| 43 | + self.theme = theme |
| 44 | + |
| 45 | + let contentSize = scrollView.contentSize |
| 46 | + let textStorage = NSTextStorage() |
| 47 | + let layoutManager = NSLayoutManager() |
| 48 | + textStorage.addLayoutManager(layoutManager) |
| 49 | + let textContainer = NSTextContainer(containerSize: scrollView.frame.size) |
| 50 | + textContainer.widthTracksTextView = true |
| 51 | + textContainer.containerSize = NSSize( |
| 52 | + width: contentSize.width, |
| 53 | + height: .greatestFiniteMagnitude |
| 54 | + ) |
| 55 | + layoutManager.addTextContainer(textContainer) |
| 56 | + let textView = CodeEditorTextView(frame: .zero, textContainer: textContainer) |
| 57 | + textView.autoresizingMask = .width |
| 58 | + textView.drawsBackground = true |
| 59 | + textView.isEditable = true |
| 60 | + textView.isHorizontallyResizable = false |
| 61 | + textView.isVerticallyResizable = true |
| 62 | + textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) |
| 63 | + textView.minSize = NSSize(width: 0, height: contentSize.height) |
| 64 | + textView.allowsUndo = true |
| 65 | + |
| 66 | + scrollView.documentView = textView |
| 67 | + scrollView.translatesAutoresizingMaskIntoConstraints = false |
| 68 | + view.addSubview(scrollView) |
| 69 | + NSLayoutConstraint.activate([ |
| 70 | + scrollView.topAnchor.constraint(equalTo: view.topAnchor), |
| 71 | + scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 72 | + scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor), |
| 73 | + scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor) |
| 74 | + ]) |
| 75 | + let lineGutter = LineGutter( |
| 76 | + scrollView: scrollView, |
| 77 | + width: 20, |
| 78 | + font: .systemFont(ofSize: 10), |
| 79 | + textColor: .labelColor, |
| 80 | + backgroundColor: .windowBackgroundColor |
| 81 | + ) |
| 82 | + scrollView.verticalRulerView = lineGutter |
| 83 | + self.lineGutter = lineGutter |
| 84 | + scrollView.rulersVisible = true |
| 85 | + highlightr?.setTheme(to: theme.rawValue) |
| 86 | + } |
| 87 | + |
| 88 | + var themeBackgroundColor: NSColor? { |
| 89 | + highlightr?.theme.themeBackgroundColor as? NSColor |
| 90 | + } |
| 91 | + |
| 92 | + func makeNSView(context: Context) -> some NSView { |
| 93 | + guard let highlightr = highlightr, |
| 94 | + let highlightedCode = highlightr.highlight(codeFile.content) |
| 95 | + else { |
| 96 | + return view |
| 97 | + } |
| 98 | + textView?.textStorage?.setAttributedString(highlightedCode) |
| 99 | + return view |
| 100 | + } |
| 101 | + |
| 102 | + func updateNSView(_ nsView: NSViewType, context: Context) { |
| 103 | + } |
| 104 | + |
| 105 | + class Coordinator { |
| 106 | + private let highlightr: Highlightr? |
| 107 | + private var cancellables = Set<AnyCancellable>() |
| 108 | + |
| 109 | + init(highlightr: Highlightr?) { |
| 110 | + self.highlightr = highlightr |
| 111 | + observeTextChange() |
| 112 | + } |
| 113 | + |
| 114 | + deinit { |
| 115 | + cancellables.forEach { $0.cancel() } |
| 116 | + } |
| 117 | + |
| 118 | + private func observeTextChange() { |
| 119 | + NotificationCenter.default |
| 120 | + .publisher(for: NSText.didChangeNotification) |
| 121 | + // we debounce so that performances while typing are increased |
| 122 | + .debounce(for: .seconds(1.5), scheduler: RunLoop.main) |
| 123 | + .compactMap { notification in notification.object as? NSTextView } |
| 124 | + .sink { [weak self] textView in |
| 125 | + if let code = self?.highlightr?.highlight(textView.string) { |
| 126 | + let cursor = textView.selectedRange() |
| 127 | + textView.textStorage?.setAttributedString(code) |
| 128 | + textView.setSelectedRange(cursor) |
| 129 | + } |
| 130 | + } |
| 131 | + .store(in: &cancellables) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + func makeCoordinator() -> Coordinator { |
| 136 | + Coordinator(highlightr: highlightr) |
| 137 | + } |
| 138 | +} |
0 commit comments