forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextViewController.swift
More file actions
334 lines (284 loc) · 10.3 KB
/
TextViewController.swift
File metadata and controls
334 lines (284 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// TextViewController.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 6/25/23.
//
import AppKit
import CodeEditTextView
import CodeEditLanguages
import SwiftUI
import Combine
import TextFormation
/// # TextViewController
///
/// A view controller class for managing a source editor. Uses ``CodeEditTextView/TextView`` for input and rendering,
/// tree-sitter for syntax highlighting, and TextFormation for live editing completions.
public class TextViewController: NSViewController {
// swiftlint:disable:next line_length
public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification")
var scrollView: NSScrollView!
private(set) public var textView: TextView!
var gutterView: GutterView!
internal var _undoManager: CEUndoManager?
/// Internal reference to any injected layers in the text view.
internal var highlightLayers: [CALayer] = []
internal var systemAppearance: NSAppearance.Name?
package var localEvenMonitor: Any?
package var isPostingCursorNotification: Bool = false
/// The string contents.
public var string: String {
textView.string
}
/// The associated `CodeLanguage`
public var language: CodeLanguage {
didSet {
highlighter?.setLanguage(language: language)
setUpTextFormation()
}
}
/// The font to use in the `textView`
public var font: NSFont {
didSet {
textView.font = font
highlighter?.invalidate()
}
}
/// The associated `Theme` used for highlighting.
public var theme: EditorTheme {
didSet {
textView.layoutManager.setNeedsLayout()
textView.textStorage.setAttributes(
attributesFor(nil),
range: NSRange(location: 0, length: textView.textStorage.length)
)
textView.selectionManager.selectedLineBackgroundColor = theme.selection
highlighter?.invalidate()
}
}
/// The visual width of tab characters in the text view measured in number of spaces.
public var tabWidth: Int {
didSet {
paragraphStyle = generateParagraphStyle()
textView.layoutManager.setNeedsLayout()
highlighter?.invalidate()
}
}
/// The behavior to use when the tab key is pressed.
public var indentOption: IndentOption {
didSet {
setUpTextFormation()
}
}
/// A multiplier for setting the line height. Defaults to `1.0`
public var lineHeightMultiple: CGFloat {
didSet {
textView.layoutManager.lineHeightMultiplier = lineHeightMultiple
}
}
/// Whether lines wrap to the width of the editor
public var wrapLines: Bool {
didSet {
textView.layoutManager.wrapLines = wrapLines
scrollView.hasHorizontalScroller = !wrapLines
textView.edgeInsets = HorizontalEdgeInsets(
left: textView.edgeInsets.left,
right: textViewTrailingInset // Refresh this value, see docs
)
}
}
/// The current cursors' positions ordered by the location of the cursor.
internal(set) public var cursorPositions: [CursorPosition] = []
/// The editorOverscroll to use for the textView over scroll
///
/// Measured in a percentage of the view's total height, meaning a `0.3` value will result in overscroll
/// of 1/3 of the view.
public var editorOverscroll: CGFloat
/// Whether the code editor should use the theme background color or be transparent
public var useThemeBackground: Bool
/// The provided highlight provider.
public var highlightProviders: [HighlightProviding]
/// Optional insets to offset the text view in the scroll view by.
public var contentInsets: NSEdgeInsets?
/// Whether or not text view is editable by user
public var isEditable: Bool {
didSet {
textView.isEditable = isEditable
}
}
/// Whether or not text view is selectable by user
public var isSelectable: Bool {
didSet {
textView.isSelectable = isSelectable
}
}
/// A multiplier that determines the amount of space between characters. `1.0` indicates no space,
/// `2.0` indicates one character of space between other characters.
public var letterSpacing: Double = 1.0 {
didSet {
textView.letterSpacing = letterSpacing
highlighter?.invalidate()
}
}
/// The type of highlight to use when highlighting bracket pairs. Leave as `nil` to disable highlighting.
public var bracketPairHighlight: BracketPairHighlight? {
didSet {
highlightSelectionPairs()
}
}
/// Passthrough value for the `textView`s string
public var text: String {
get {
textView.string
}
set {
self.setText(newValue)
}
}
/// If true, uses the system cursor on macOS 14 or greater.
public var useSystemCursor: Bool {
get {
textView.useSystemCursor
}
set {
if #available(macOS 14, *) {
textView.useSystemCursor = newValue
}
}
}
var textCoordinators: [WeakCoordinator] = []
var highlighter: Highlighter?
/// The tree sitter client managed by the source editor.
///
/// This will be `nil` if another highlighter provider is passed to the source editor.
internal(set) public var treeSitterClient: TreeSitterClient?
package var fontCharWidth: CGFloat { (" " as NSString).size(withAttributes: [.font: font]).width }
/// Filters used when applying edits..
internal var textFilters: [TextFormation.Filter] = []
internal var cancellables = Set<AnyCancellable>()
/// ScrollView's bottom inset using as editor overscroll
package var bottomContentInsets: CGFloat {
let height = view.frame.height
var inset = editorOverscroll * height
if height - inset < font.lineHeight * lineHeightMultiple {
inset = height - font.lineHeight * lineHeightMultiple
}
return max(inset, .zero)
}
/// The trailing inset for the editor. Grows when line wrapping is disabled.
package var textViewTrailingInset: CGFloat {
wrapLines ? 1 : 48
}
// MARK: Init
init(
string: String,
language: CodeLanguage,
font: NSFont,
theme: EditorTheme,
tabWidth: Int,
indentOption: IndentOption,
lineHeight: CGFloat,
wrapLines: Bool,
cursorPositions: [CursorPosition],
editorOverscroll: CGFloat,
useThemeBackground: Bool,
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
contentInsets: NSEdgeInsets?,
isEditable: Bool,
isSelectable: Bool,
letterSpacing: Double,
useSystemCursor: Bool,
bracketPairHighlight: BracketPairHighlight?,
undoManager: CEUndoManager? = nil,
coordinators: [TextViewCoordinator] = []
) {
self.language = language
self.font = font
self.theme = theme
self.tabWidth = tabWidth
self.indentOption = indentOption
self.lineHeightMultiple = lineHeight
self.wrapLines = wrapLines
self.cursorPositions = cursorPositions
self.editorOverscroll = editorOverscroll
self.useThemeBackground = useThemeBackground
self.highlightProviders = highlightProviders
self.contentInsets = contentInsets
self.isEditable = isEditable
self.isSelectable = isSelectable
self.letterSpacing = letterSpacing
self.bracketPairHighlight = bracketPairHighlight
self._undoManager = undoManager
super.init(nibName: nil, bundle: nil)
let platformGuardedSystemCursor: Bool
if #available(macOS 14, *) {
platformGuardedSystemCursor = useSystemCursor
} else {
platformGuardedSystemCursor = false
}
if let idx = highlightProviders.firstIndex(where: { $0 is TreeSitterClient }),
let client = highlightProviders[idx] as? TreeSitterClient {
self.treeSitterClient = client
}
self.textView = TextView(
string: string,
font: font,
textColor: theme.text.color,
lineHeightMultiplier: lineHeightMultiple,
wrapLines: wrapLines,
isEditable: isEditable,
isSelectable: isSelectable,
letterSpacing: letterSpacing,
useSystemCursor: platformGuardedSystemCursor,
delegate: self
)
coordinators.forEach {
$0.prepareCoordinator(controller: self)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// Set the contents of the editor.
/// - Parameter text: The new contents of the editor.
public func setText(_ text: String) {
self.textView.setText(text)
self.setUpHighlighter()
self.gutterView.setNeedsDisplay(self.gutterView.frame)
}
// MARK: Paragraph Style
/// A default `NSParagraphStyle` with a set `lineHeight`
package lazy var paragraphStyle: NSMutableParagraphStyle = generateParagraphStyle()
// MARK: - Reload UI
func reloadUI() {
textView.isEditable = isEditable
textView.isSelectable = isSelectable
styleScrollView()
styleTextView()
styleGutterView()
highlighter?.invalidate()
}
deinit {
if let highlighter {
textView.removeStorageDelegate(highlighter)
}
highlighter = nil
highlightProviders.removeAll()
textCoordinators.values().forEach {
$0.destroy()
}
textCoordinators.removeAll()
NotificationCenter.default.removeObserver(self)
cancellables.forEach { $0.cancel() }
if let localEvenMonitor {
NSEvent.removeMonitor(localEvenMonitor)
}
localEvenMonitor = nil
}
}
extension TextViewController: GutterViewDelegate {
public func gutterViewWidthDidUpdate(newWidth: CGFloat) {
gutterView?.frame.size.width = newWidth
textView?.edgeInsets = HorizontalEdgeInsets(left: newWidth, right: textViewTrailingInset)
}
}