Skip to content

Commit 99a839a

Browse files
Merge branch 'main' into itembox
2 parents 36b86c4 + bfcde1f commit 99a839a

38 files changed

+1946
-502
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ disabled_rules:
55
- todo
66
- trailing_comma
77
- nesting
8+
- optional_data_string_conversion
89

910
type_name:
1011
excluded:

.swiftpm/xcode/xcshareddata/xcschemes/CodeEditTextView.xcscheme

Lines changed: 0 additions & 101 deletions
This file was deleted.

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Documents/CodeEditSourceEditorExampleDocument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct CodeEditSourceEditorExampleDocument: FileDocument {
2929
}
3030

3131
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
32-
let data = text.data(using: .utf8)!
32+
let data = Data(text.utf8)
3333
return .init(regularFileWithContents: data)
3434
}
3535
}

Package.resolved

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

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ let package = Package(
1717
// A fast, efficient, text view for code.
1818
.package(
1919
// url: "https://github.com/CodeEditApp/CodeEditTextView.git",
20-
// from: "0.7.6"
20+
// from: "0.7.7"
2121
path: "../CodeEditTextView"
2222
),
2323
// tree-sitter languages
2424
.package(
2525
url: "https://github.com/CodeEditApp/CodeEditLanguages.git",
26-
exact: "0.1.19"
26+
exact: "0.1.20"
2727
),
2828
// SwiftLint
2929
.package(

Sources/CodeEditSourceEditor/CodeEditSourceEditor/CodeEditSourceEditor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
5656
editorOverscroll: CGFloat = 0,
5757
cursorPositions: Binding<[CursorPosition]>,
5858
useThemeBackground: Bool = true,
59-
highlightProvider: HighlightProviding? = nil,
59+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
6060
contentInsets: NSEdgeInsets? = nil,
6161
isEditable: Bool = true,
6262
isSelectable: Bool = true,
@@ -77,7 +77,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
7777
self.wrapLines = wrapLines
7878
self.editorOverscroll = editorOverscroll
7979
self.cursorPositions = cursorPositions
80-
self.highlightProvider = highlightProvider
80+
self.highlightProviders = highlightProviders
8181
self.contentInsets = contentInsets
8282
self.isEditable = isEditable
8383
self.isSelectable = isSelectable
@@ -131,7 +131,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
131131
editorOverscroll: CGFloat = 0,
132132
cursorPositions: Binding<[CursorPosition]>,
133133
useThemeBackground: Bool = true,
134-
highlightProvider: HighlightProviding? = nil,
134+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
135135
contentInsets: NSEdgeInsets? = nil,
136136
isEditable: Bool = true,
137137
isSelectable: Bool = true,
@@ -152,7 +152,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
152152
self.wrapLines = wrapLines
153153
self.editorOverscroll = editorOverscroll
154154
self.cursorPositions = cursorPositions
155-
self.highlightProvider = highlightProvider
155+
self.highlightProviders = highlightProviders
156156
self.contentInsets = contentInsets
157157
self.isEditable = isEditable
158158
self.isSelectable = isSelectable
@@ -178,7 +178,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
178178
private var editorOverscroll: CGFloat
179179
package var cursorPositions: Binding<[CursorPosition]>
180180
private var useThemeBackground: Bool
181-
private var highlightProvider: HighlightProviding?
181+
private var highlightProviders: [HighlightProviding]
182182
private var contentInsets: NSEdgeInsets?
183183
private var isEditable: Bool
184184
private var isSelectable: Bool
@@ -204,7 +204,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
204204
cursorPositions: cursorPositions.wrappedValue,
205205
editorOverscroll: editorOverscroll,
206206
useThemeBackground: useThemeBackground,
207-
highlightProvider: highlightProvider,
207+
highlightProviders: highlightProviders,
208208
contentInsets: contentInsets,
209209
isEditable: isEditable,
210210
isSelectable: isSelectable,

Sources/CodeEditSourceEditor/Controller/TextViewController+Highlighter.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@ extension TextViewController {
1515
self.highlighter = nil
1616
}
1717

18-
self.highlighter = Highlighter(
18+
let highlighter = Highlighter(
1919
textView: textView,
20-
highlightProvider: highlightProvider,
21-
theme: theme,
20+
providers: highlightProviders,
2221
attributeProvider: self,
2322
language: language
2423
)
25-
textView.addStorageDelegate(highlighter!)
26-
setHighlightProvider(self.highlightProvider)
27-
}
28-
29-
internal func setHighlightProvider(_ highlightProvider: HighlightProviding? = nil) {
30-
var provider: HighlightProviding?
31-
32-
if let highlightProvider = highlightProvider {
33-
provider = highlightProvider
34-
} else {
35-
self.treeSitterClient = TreeSitterClient()
36-
provider = self.treeSitterClient!
37-
}
38-
39-
if let provider = provider {
40-
self.highlightProvider = provider
41-
highlighter?.setHighlightProvider(provider)
42-
}
24+
textView.addStorageDelegate(highlighter)
25+
self.highlighter = highlighter
4326
}
4427
}
4528

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public class TextViewController: NSViewController {
9393
didSet {
9494
textView.layoutManager.wrapLines = wrapLines
9595
scrollView.hasHorizontalScroller = !wrapLines
96+
textView.edgeInsets = HorizontalEdgeInsets(
97+
left: textView.edgeInsets.left,
98+
right: textViewTrailingInset // Refresh this value, see docs
99+
)
96100
}
97101
}
98102

@@ -109,7 +113,7 @@ public class TextViewController: NSViewController {
109113
public var useThemeBackground: Bool
110114

111115
/// The provided highlight provider.
112-
public var highlightProvider: HighlightProviding?
116+
public var highlightProviders: [HighlightProviding]
113117

114118
/// Optional insets to offset the text view in the scroll view by.
115119
public var contentInsets: NSEdgeInsets?
@@ -194,6 +198,11 @@ public class TextViewController: NSViewController {
194198
return max(inset, .zero)
195199
}
196200

201+
/// The trailing inset for the editor. Grows when line wrapping is disabled.
202+
package var textViewTrailingInset: CGFloat {
203+
wrapLines ? 1 : 48
204+
}
205+
197206
// MARK: Init
198207

199208
init(
@@ -208,7 +217,7 @@ public class TextViewController: NSViewController {
208217
cursorPositions: [CursorPosition],
209218
editorOverscroll: CGFloat,
210219
useThemeBackground: Bool,
211-
highlightProvider: HighlightProviding?,
220+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
212221
contentInsets: NSEdgeInsets?,
213222
isEditable: Bool,
214223
isSelectable: Bool,
@@ -228,7 +237,7 @@ public class TextViewController: NSViewController {
228237
self.cursorPositions = cursorPositions
229238
self.editorOverscroll = editorOverscroll
230239
self.useThemeBackground = useThemeBackground
231-
self.highlightProvider = highlightProvider
240+
self.highlightProviders = highlightProviders
232241
self.contentInsets = contentInsets
233242
self.isEditable = isEditable
234243
self.isSelectable = isSelectable
@@ -245,6 +254,11 @@ public class TextViewController: NSViewController {
245254
platformGuardedSystemCursor = false
246255
}
247256

257+
if let idx = highlightProviders.firstIndex(where: { $0 is TreeSitterClient }),
258+
let client = highlightProviders[idx] as? TreeSitterClient {
259+
self.treeSitterClient = client
260+
}
261+
248262
self.textView = TextView(
249263
string: string,
250264
font: font,
@@ -297,7 +311,7 @@ public class TextViewController: NSViewController {
297311
textView.removeStorageDelegate(highlighter)
298312
}
299313
highlighter = nil
300-
highlightProvider = nil
314+
highlightProviders.removeAll()
301315
textCoordinators.values().forEach {
302316
$0.destroy()
303317
}
@@ -314,6 +328,6 @@ public class TextViewController: NSViewController {
314328
extension TextViewController: GutterViewDelegate {
315329
public func gutterViewWidthDidUpdate(newWidth: CGFloat) {
316330
gutterView?.frame.size.width = newWidth
317-
textView?.edgeInsets = HorizontalEdgeInsets(left: newWidth, right: 0)
331+
textView?.edgeInsets = HorizontalEdgeInsets(left: newWidth, right: textViewTrailingInset)
318332
}
319333
}

0 commit comments

Comments
 (0)