@@ -35,8 +35,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
35
35
/// - cursorPositions: The cursor's position in the editor, measured in `(lineNum, columnNum)`
36
36
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
37
37
/// background color
38
- /// - highlightProvider : A class you provide to perform syntax highlighting. Leave this as `nil` to use the
39
- /// built-in `TreeSitterClient` highlighter.
38
+ /// - highlightProviders : A set of classes you provide to perform syntax highlighting. Leave this as `nil` to use
39
+ /// the default `TreeSitterClient` highlighter.
40
40
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
41
41
/// scroll view automatically adjust content insets.
42
42
/// - additionalTextInsets: An additional amount to inset the text of the editor by.
@@ -62,7 +62,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
62
62
editorOverscroll: CGFloat = 0 ,
63
63
cursorPositions: Binding < [ CursorPosition ] > ,
64
64
useThemeBackground: Bool = true ,
65
- highlightProviders: [ any HighlightProviding ] = [ TreeSitterClient ( ) ] ,
65
+ highlightProviders: [ any HighlightProviding ] ? = nil ,
66
66
contentInsets: NSEdgeInsets ? = nil ,
67
67
additionalTextInsets: NSEdgeInsets ? = nil ,
68
68
isEditable: Bool = true ,
@@ -114,8 +114,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
114
114
/// - cursorPositions: The cursor's position in the editor, measured in `(lineNum, columnNum)`
115
115
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
116
116
/// background color
117
- /// - highlightProvider : A class you provide to perform syntax highlighting. Leave this as `nil` to use the
118
- /// built-in `TreeSitterClient` highlighter.
117
+ /// - highlightProviders : A set of classes you provide to perform syntax highlighting. Leave this as `nil` to use
118
+ /// the default `TreeSitterClient` highlighter.
119
119
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
120
120
/// scroll view automatically adjust content insets.
121
121
/// - isEditable: A Boolean value that controls whether the text view allows the user to edit text.
@@ -139,7 +139,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
139
139
editorOverscroll: CGFloat = 0 ,
140
140
cursorPositions: Binding < [ CursorPosition ] > ,
141
141
useThemeBackground: Bool = true ,
142
- highlightProviders: [ any HighlightProviding ] = [ TreeSitterClient ( ) ] ,
142
+ highlightProviders: [ any HighlightProviding ] ? = nil ,
143
143
contentInsets: NSEdgeInsets ? = nil ,
144
144
additionalTextInsets: NSEdgeInsets ? = nil ,
145
145
isEditable: Bool = true ,
@@ -188,7 +188,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
188
188
private var editorOverscroll : CGFloat
189
189
package var cursorPositions : Binding < [ CursorPosition ] >
190
190
private var useThemeBackground : Bool
191
- private var highlightProviders : [ any HighlightProviding ]
191
+ private var highlightProviders : [ any HighlightProviding ] ?
192
192
private var contentInsets : NSEdgeInsets ?
193
193
private var additionalTextInsets : NSEdgeInsets ?
194
194
private var isEditable : Bool
@@ -214,7 +214,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
214
214
cursorPositions: cursorPositions. wrappedValue,
215
215
editorOverscroll: editorOverscroll,
216
216
useThemeBackground: useThemeBackground,
217
- highlightProviders: highlightProviders,
217
+ highlightProviders: context . coordinator . highlightProviders,
218
218
contentInsets: contentInsets,
219
219
additionalTextInsets: additionalTextInsets,
220
220
isEditable: isEditable,
@@ -243,10 +243,12 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
243
243
}
244
244
245
245
public func makeCoordinator( ) -> Coordinator {
246
- Coordinator ( text: text, cursorPositions: cursorPositions)
246
+ Coordinator ( text: text, cursorPositions: cursorPositions, highlightProviders : highlightProviders )
247
247
}
248
248
249
249
public func updateNSViewController( _ controller: TextViewController , context: Context ) {
250
+ context. coordinator. updateHighlightProviders ( highlightProviders)
251
+
250
252
if !context. coordinator. isUpdateFromTextView {
251
253
// Prevent infinite loop of update notifications
252
254
context. coordinator. isUpdatingFromRepresentable = true
@@ -261,23 +263,23 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
261
263
262
264
// Do manual diffing to reduce the amount of reloads.
263
265
// This helps a lot in view performance, as it otherwise gets triggered on each environment change.
264
- guard !paramsAreEqual( controller: controller) else {
266
+ guard !paramsAreEqual( controller: controller, coordinator : context . coordinator ) else {
265
267
return
266
268
}
267
269
268
- updateControllerParams ( controller: controller)
270
+ updateControllerParams ( controller: controller, coordinator : context . coordinator )
269
271
270
272
controller. reloadUI ( )
271
273
return
272
274
}
273
275
274
276
/// Update the parameters of the controller.
275
277
/// - Parameter controller: The controller to update.
276
- func updateControllerParams( controller: TextViewController ) {
278
+ func updateControllerParams( controller: TextViewController , coordinator : Coordinator ) {
277
279
updateTextProperties ( controller)
278
280
updateEditorProperties ( controller)
279
281
updateThemeAndLanguage ( controller)
280
- updateHighlighting ( controller)
282
+ updateHighlighting ( controller, coordinator : coordinator )
281
283
}
282
284
283
285
private func updateTextProperties( _ controller: TextViewController ) {
@@ -329,9 +331,9 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
329
331
}
330
332
}
331
333
332
- private func updateHighlighting( _ controller: TextViewController ) {
333
- if !areHighlightProvidersEqual( controller: controller) {
334
- controller. setHighlightProviders ( highlightProviders)
334
+ private func updateHighlighting( _ controller: TextViewController , coordinator : Coordinator ) {
335
+ if !areHighlightProvidersEqual( controller: controller, coordinator : coordinator ) {
336
+ controller. setHighlightProviders ( coordinator . highlightProviders)
335
337
}
336
338
337
339
if controller. bracketPairEmphasis != bracketPairEmphasis {
@@ -342,7 +344,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
342
344
/// Checks if the controller needs updating.
343
345
/// - Parameter controller: The controller to check.
344
346
/// - Returns: True, if the controller's parameters should be updated.
345
- func paramsAreEqual( controller: NSViewControllerType ) -> Bool {
347
+ func paramsAreEqual( controller: NSViewControllerType , coordinator : Coordinator ) -> Bool {
346
348
controller. font == font &&
347
349
controller. isEditable == isEditable &&
348
350
controller. isSelectable == isSelectable &&
@@ -359,11 +361,12 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
359
361
controller. letterSpacing == letterSpacing &&
360
362
controller. bracketPairEmphasis == bracketPairEmphasis &&
361
363
controller. useSystemCursor == useSystemCursor &&
362
- areHighlightProvidersEqual ( controller: controller)
364
+ areHighlightProvidersEqual ( controller: controller, coordinator : coordinator )
363
365
}
364
366
365
- private func areHighlightProvidersEqual( controller: TextViewController ) -> Bool {
366
- controller. highlightProviders. map { ObjectIdentifier ( $0) } == highlightProviders. map { ObjectIdentifier ( $0) }
367
+ private func areHighlightProvidersEqual( controller: TextViewController , coordinator: Coordinator ) -> Bool {
368
+ controller. highlightProviders. map { ObjectIdentifier ( $0) }
369
+ == coordinator. highlightProviders. map { ObjectIdentifier ( $0) }
367
370
}
368
371
}
369
372
0 commit comments