@@ -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 ,
@@ -116,8 +116,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
116
116
/// - cursorPositions: The cursor's position in the editor, measured in `(lineNum, columnNum)`
117
117
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
118
118
/// background color
119
- /// - highlightProvider : A class you provide to perform syntax highlighting. Leave this as `nil` to use the
120
- /// built-in `TreeSitterClient` highlighter.
119
+ /// - highlightProviders : A set of classes you provide to perform syntax highlighting. Leave this as `nil` to use
120
+ /// the default `TreeSitterClient` highlighter.
121
121
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
122
122
/// scroll view automatically adjust content insets.
123
123
/// - isEditable: A Boolean value that controls whether the text view allows the user to edit text.
@@ -141,7 +141,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
141
141
editorOverscroll: CGFloat = 0 ,
142
142
cursorPositions: Binding < [ CursorPosition ] > ,
143
143
useThemeBackground: Bool = true ,
144
- highlightProviders: [ any HighlightProviding ] = [ TreeSitterClient ( ) ] ,
144
+ highlightProviders: [ any HighlightProviding ] ? = nil ,
145
145
contentInsets: NSEdgeInsets ? = nil ,
146
146
additionalTextInsets: NSEdgeInsets ? = nil ,
147
147
isEditable: Bool = true ,
@@ -192,7 +192,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
192
192
private var editorOverscroll : CGFloat
193
193
package var cursorPositions : Binding < [ CursorPosition ] >
194
194
private var useThemeBackground : Bool
195
- private var highlightProviders : [ any HighlightProviding ]
195
+ private var highlightProviders : [ any HighlightProviding ] ?
196
196
private var contentInsets : NSEdgeInsets ?
197
197
private var additionalTextInsets : NSEdgeInsets ?
198
198
private var isEditable : Bool
@@ -219,7 +219,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
219
219
cursorPositions: cursorPositions. wrappedValue,
220
220
editorOverscroll: editorOverscroll,
221
221
useThemeBackground: useThemeBackground,
222
- highlightProviders: highlightProviders,
222
+ highlightProviders: context . coordinator . highlightProviders,
223
223
contentInsets: contentInsets,
224
224
additionalTextInsets: additionalTextInsets,
225
225
isEditable: isEditable,
@@ -249,10 +249,12 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
249
249
}
250
250
251
251
public func makeCoordinator( ) -> Coordinator {
252
- Coordinator ( text: text, cursorPositions: cursorPositions)
252
+ Coordinator ( text: text, cursorPositions: cursorPositions, highlightProviders : highlightProviders )
253
253
}
254
254
255
255
public func updateNSViewController( _ controller: TextViewController , context: Context ) {
256
+ context. coordinator. updateHighlightProviders ( highlightProviders)
257
+
256
258
if !context. coordinator. isUpdateFromTextView {
257
259
// Prevent infinite loop of update notifications
258
260
context. coordinator. isUpdatingFromRepresentable = true
@@ -267,23 +269,23 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
267
269
268
270
// Do manual diffing to reduce the amount of reloads.
269
271
// This helps a lot in view performance, as it otherwise gets triggered on each environment change.
270
- guard !paramsAreEqual( controller: controller) else {
272
+ guard !paramsAreEqual( controller: controller, coordinator : context . coordinator ) else {
271
273
return
272
274
}
273
275
274
- updateControllerParams ( controller: controller)
276
+ updateControllerParams ( controller: controller, coordinator : context . coordinator )
275
277
276
278
controller. reloadUI ( )
277
279
return
278
280
}
279
281
280
282
/// Update the parameters of the controller.
281
283
/// - Parameter controller: The controller to update.
282
- func updateControllerParams( controller: TextViewController ) {
284
+ func updateControllerParams( controller: TextViewController , coordinator : Coordinator ) {
283
285
updateTextProperties ( controller)
284
286
updateEditorProperties ( controller)
285
287
updateThemeAndLanguage ( controller)
286
- updateHighlighting ( controller)
288
+ updateHighlighting ( controller, coordinator : coordinator )
287
289
}
288
290
289
291
private func updateTextProperties( _ controller: TextViewController ) {
@@ -336,9 +338,9 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
336
338
}
337
339
}
338
340
339
- private func updateHighlighting( _ controller: TextViewController ) {
340
- if !areHighlightProvidersEqual( controller: controller) {
341
- controller. setHighlightProviders ( highlightProviders)
341
+ private func updateHighlighting( _ controller: TextViewController , coordinator : Coordinator ) {
342
+ if !areHighlightProvidersEqual( controller: controller, coordinator : coordinator ) {
343
+ controller. setHighlightProviders ( coordinator . highlightProviders)
342
344
}
343
345
344
346
if controller. bracketPairEmphasis != bracketPairEmphasis {
@@ -349,7 +351,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
349
351
/// Checks if the controller needs updating.
350
352
/// - Parameter controller: The controller to check.
351
353
/// - Returns: True, if the controller's parameters should be updated.
352
- func paramsAreEqual( controller: NSViewControllerType ) -> Bool {
354
+ func paramsAreEqual( controller: NSViewControllerType , coordinator : Coordinator ) -> Bool {
353
355
controller. font == font &&
354
356
controller. isEditable == isEditable &&
355
357
controller. isSelectable == isSelectable &&
@@ -367,11 +369,12 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
367
369
controller. bracketPairEmphasis == bracketPairEmphasis &&
368
370
controller. useSystemCursor == useSystemCursor &&
369
371
controller. showMinimap == showMinimap &&
370
- areHighlightProvidersEqual ( controller: controller)
372
+ areHighlightProvidersEqual ( controller: controller, coordinator : coordinator )
371
373
}
372
374
373
- private func areHighlightProvidersEqual( controller: TextViewController ) -> Bool {
374
- controller. highlightProviders. map { ObjectIdentifier ( $0) } == highlightProviders. map { ObjectIdentifier ( $0) }
375
+ private func areHighlightProvidersEqual( controller: TextViewController , coordinator: Coordinator ) -> Bool {
376
+ controller. highlightProviders. map { ObjectIdentifier ( $0) }
377
+ == coordinator. highlightProviders. map { ObjectIdentifier ( $0) }
375
378
}
376
379
}
377
380
0 commit comments