Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit 1286a1a

Browse files
committed
Feature: Allow syntax highlighting to be disabled
1 parent 53210fa commit 1286a1a

File tree

7 files changed

+181
-4
lines changed

7 files changed

+181
-4
lines changed

Package.resolved

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/AuroraEditorSourceEditor/AuroraEditorSourceEditor/AuroraEditorSourceEditor+Coordinator.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extension AuroraEditorSourceEditor {
1515
weak var controller: TextViewController?
1616
var isUpdatingFromRepresentable: Bool = false
1717
var isUpdateFromTextView: Bool = false
18+
private var lastExternalText: String = ""
1819

1920
init(parent: AuroraEditorSourceEditor) {
2021
self.parent = parent
@@ -61,6 +62,20 @@ extension AuroraEditorSourceEditor {
6162
)
6263
}
6364
}
65+
self.isUpdateFromTextView = false
66+
}
67+
68+
func handleExternalTextChange() {
69+
guard let controller = controller,
70+
case .binding(let binding) = parent.text,
71+
binding.wrappedValue != lastExternalText else {
72+
return
73+
}
74+
75+
isUpdatingFromRepresentable = true
76+
controller.textView.setText(binding.wrappedValue)
77+
lastExternalText = binding.wrappedValue
78+
isUpdatingFromRepresentable = false
6479
}
6580

6681
deinit {

Sources/AuroraEditorSourceEditor/AuroraEditorSourceEditor/AuroraEditorSourceEditor.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
3333
/// background color
3434
/// - highlightProvider: A class you provide to perform syntax highlighting. Leave this as `nil` to use the
3535
/// built-in `TreeSitterClient` highlighter.
36+
/// - isSyntaxHighlightingDisabled: A boolean the allows disabling the syntax highliting for better performance.
3637
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
3738
/// scroll view automatically adjust content insets.
3839
/// - isEditable: A Boolean value that controls whether the text view allows the user to edit text.
@@ -57,6 +58,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
5758
cursorPositions: Binding<[CursorPosition]>,
5859
useThemeBackground: Bool = true,
5960
highlightProvider: HighlightProviding? = nil,
61+
isSyntaxHighlightingDisabled: Bool? = false,
6062
contentInsets: NSEdgeInsets? = nil,
6163
isEditable: Bool = true,
6264
isSelectable: Bool = true,
@@ -77,6 +79,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
7779
self.editorOverscroll = editorOverscroll
7880
self.cursorPositions = cursorPositions
7981
self.highlightProvider = highlightProvider
82+
self.isSyntaxHighlightingDisabled = isSyntaxHighlightingDisabled
8083
self.contentInsets = contentInsets
8184
self.isEditable = isEditable
8285
self.isSelectable = isSelectable
@@ -126,6 +129,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
126129
cursorPositions: Binding<[CursorPosition]>,
127130
useThemeBackground: Bool = true,
128131
highlightProvider: HighlightProviding? = nil,
132+
isSyntaxHighlightingDisabled: Bool? = false,
129133
contentInsets: NSEdgeInsets? = nil,
130134
isEditable: Bool = true,
131135
isSelectable: Bool = true,
@@ -146,6 +150,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
146150
self.editorOverscroll = editorOverscroll
147151
self.cursorPositions = cursorPositions
148152
self.highlightProvider = highlightProvider
153+
self.isSyntaxHighlightingDisabled = isSyntaxHighlightingDisabled
149154
self.contentInsets = contentInsets
150155
self.isEditable = isEditable
151156
self.isSelectable = isSelectable
@@ -167,6 +172,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
167172
package var cursorPositions: Binding<[CursorPosition]>
168173
private var useThemeBackground: Bool
169174
private var highlightProvider: HighlightProviding?
175+
private var isSyntaxHighlightingDisabled: Bool?
170176
private var contentInsets: NSEdgeInsets?
171177
private var isEditable: Bool
172178
private var isSelectable: Bool
@@ -191,6 +197,7 @@ public struct AuroraEditorSourceEditor: NSViewControllerRepresentable {
191197
editorOverscroll: editorOverscroll,
192198
useThemeBackground: useThemeBackground,
193199
highlightProvider: highlightProvider,
200+
isSyntaxHighlightingDisabled: isSyntaxHighlightingDisabled,
194201
contentInsets: contentInsets,
195202
isEditable: isEditable,
196203
isSelectable: isSelectable,

Sources/AuroraEditorSourceEditor/Controller/TextViewController+Highlighter.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ extension TextViewController {
2020
highlightProvider: highlightProvider,
2121
theme: theme,
2222
attributeProvider: self,
23-
language: language
23+
language: language,
24+
isSyntaxHighlightingDisabled: isSyntaxHighlightingDisabled ?? false
2425
)
2526
textView.addStorageDelegate(highlighter!)
2627
setHighlightProvider(self.highlightProvider)
2728
}
2829

2930
internal func setHighlightProvider(_ highlightProvider: HighlightProviding? = nil) {
31+
if isSyntaxHighlightingDisabled == true {
32+
return
33+
}
34+
3035
var provider: HighlightProviding?
3136

3237
if let highlightProvider = highlightProvider {
@@ -36,6 +41,10 @@ extension TextViewController {
3641
provider = self.treeSitterClient!
3742
}
3843

44+
if isSyntaxHighlightingDisabled == true {
45+
return
46+
}
47+
3948
if let provider = provider {
4049
self.highlightProvider = provider
4150
highlighter?.setHighlightProvider(provider)

Sources/AuroraEditorSourceEditor/Controller/TextViewController+TextViewDelegate.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@ import AuroraEditorTextView
1010
import TextStory
1111

1212
extension TextViewController: TextViewDelegate {
13-
public func textView(_ textView: TextView, didReplaceContentsIn range: NSRange, with: String) {
13+
public func textView(
14+
_ textView: TextView,
15+
willReplaceContentsIn range: NSRange,
16+
with string: String
17+
) { }
18+
19+
public func textView(
20+
_ textView: TextView,
21+
didReplaceContentsIn range: NSRange,
22+
with string: String
23+
) {
1424
gutterView.needsDisplay = true
25+
onTextDidChange?(string)
1526
}
1627

17-
public func textView(_ textView: TextView, shouldReplaceContentsIn range: NSRange, with string: String) -> Bool {
28+
public func textView(
29+
_ textView: TextView,
30+
shouldReplaceContentsIn range: NSRange,
31+
with string: String
32+
) -> Bool {
1833
let mutation = TextMutation(
1934
string: string,
2035
range: range,
@@ -23,4 +38,11 @@ extension TextViewController: TextViewDelegate {
2338

2439
return shouldApplyMutation(mutation, to: textView)
2540
}
41+
42+
func textViewDidChange(
43+
_ textView: TextView,
44+
newText: String
45+
) {
46+
onTextDidChange?(newText)
47+
}
2648
}

Sources/AuroraEditorSourceEditor/Controller/TextViewController.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public class TextViewController: NSViewController {
155155

156156
var highlighter: Highlighter?
157157

158+
var isSyntaxHighlightingDisabled: Bool?
159+
158160
/// The tree sitter client managed by the source editor.
159161
///
160162
/// This will be `nil` if another highlighter provider is passed to the source editor.
@@ -167,6 +169,8 @@ public class TextViewController: NSViewController {
167169

168170
internal var cancellables = Set<AnyCancellable>()
169171

172+
var onTextDidChange: ((String) -> Void)?
173+
170174
/// ScrollView's bottom inset using as editor overscroll
171175
private var bottomContentInsets: CGFloat {
172176
let height = view.frame.height
@@ -194,6 +198,7 @@ public class TextViewController: NSViewController {
194198
editorOverscroll: CGFloat,
195199
useThemeBackground: Bool,
196200
highlightProvider: HighlightProviding?,
201+
isSyntaxHighlightingDisabled: Bool?,
197202
contentInsets: NSEdgeInsets?,
198203
isEditable: Bool,
199204
isSelectable: Bool,
@@ -212,6 +217,7 @@ public class TextViewController: NSViewController {
212217
self.editorOverscroll = editorOverscroll
213218
self.useThemeBackground = useThemeBackground
214219
self.highlightProvider = highlightProvider
220+
self.isSyntaxHighlightingDisabled = isSyntaxHighlightingDisabled
215221
self.contentInsets = contentInsets
216222
self.isEditable = isEditable
217223
self.isSelectable = isSelectable

0 commit comments

Comments
 (0)