Skip to content

Commit f01eeb0

Browse files
committed
Apply Styles, Update Init Params, Update Capture Representation
1 parent 5c1a696 commit f01eeb0

File tree

15 files changed

+278
-101
lines changed

15 files changed

+278
-101
lines changed

Sources/CodeEditSourceEditor/CodeEditSourceEditor/CodeEditSourceEditor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
5757
editorOverscroll: CGFloat = 0,
5858
cursorPositions: Binding<[CursorPosition]>,
5959
useThemeBackground: Bool = true,
60-
highlightProvider: HighlightProviding? = nil,
60+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
6161
contentInsets: NSEdgeInsets? = nil,
6262
isEditable: Bool = true,
6363
isSelectable: Bool = true,
@@ -78,7 +78,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
7878
self.wrapLines = wrapLines
7979
self.editorOverscroll = editorOverscroll
8080
self.cursorPositions = cursorPositions
81-
self.highlightProvider = highlightProvider
81+
self.highlightProviders = highlightProviders
8282
self.contentInsets = contentInsets
8383
self.isEditable = isEditable
8484
self.isSelectable = isSelectable
@@ -132,7 +132,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
132132
editorOverscroll: CGFloat = 0,
133133
cursorPositions: Binding<[CursorPosition]>,
134134
useThemeBackground: Bool = true,
135-
highlightProvider: HighlightProviding? = nil,
135+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
136136
contentInsets: NSEdgeInsets? = nil,
137137
isEditable: Bool = true,
138138
isSelectable: Bool = true,
@@ -153,7 +153,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
153153
self.wrapLines = wrapLines
154154
self.editorOverscroll = editorOverscroll
155155
self.cursorPositions = cursorPositions
156-
self.highlightProvider = highlightProvider
156+
self.highlightProviders = highlightProviders
157157
self.contentInsets = contentInsets
158158
self.isEditable = isEditable
159159
self.isSelectable = isSelectable
@@ -179,7 +179,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
179179
private var editorOverscroll: CGFloat
180180
package var cursorPositions: Binding<[CursorPosition]>
181181
private var useThemeBackground: Bool
182-
private var highlightProvider: HighlightProviding?
182+
private var highlightProviders: [HighlightProviding]
183183
private var contentInsets: NSEdgeInsets?
184184
private var isEditable: Bool
185185
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: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,18 @@ import SwiftTreeSitter
1111
extension TextViewController {
1212
internal func setUpHighlighter() {
1313
if let highlighter {
14-
// textView.removeStorageDelegate(highlighter)
14+
textView.removeStorageDelegate(highlighter)
1515
self.highlighter = nil
1616
}
1717

18-
// self.highlighter = Highlighter(
19-
// textView: textView,
20-
// highlightProvider: highlightProvider,
21-
// theme: theme,
22-
// attributeProvider: self,
23-
// language: language
24-
// )
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-
}
18+
let highlighter = Highlighter(
19+
textView: textView,
20+
providers: highlightProviders,
21+
attributeProvider: self,
22+
language: language
23+
)
24+
textView.addStorageDelegate(highlighter)
25+
self.highlighter = highlighter
4326
}
4427
}
4528

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class TextViewController: NSViewController {
109109
public var useThemeBackground: Bool
110110

111111
/// The provided highlight provider.
112-
public var highlightProvider: HighlightProviding?
112+
public var highlightProviders: [HighlightProviding]
113113

114114
/// Optional insets to offset the text view in the scroll view by.
115115
public var contentInsets: NSEdgeInsets?
@@ -208,7 +208,7 @@ public class TextViewController: NSViewController {
208208
cursorPositions: [CursorPosition],
209209
editorOverscroll: CGFloat,
210210
useThemeBackground: Bool,
211-
highlightProvider: HighlightProviding?,
211+
highlightProviders: [HighlightProviding] = [TreeSitterClient()],
212212
contentInsets: NSEdgeInsets?,
213213
isEditable: Bool,
214214
isSelectable: Bool,
@@ -228,7 +228,7 @@ public class TextViewController: NSViewController {
228228
self.cursorPositions = cursorPositions
229229
self.editorOverscroll = editorOverscroll
230230
self.useThemeBackground = useThemeBackground
231-
self.highlightProvider = highlightProvider
231+
self.highlightProviders = highlightProviders
232232
self.contentInsets = contentInsets
233233
self.isEditable = isEditable
234234
self.isSelectable = isSelectable
@@ -295,10 +295,10 @@ public class TextViewController: NSViewController {
295295

296296
deinit {
297297
if let highlighter {
298-
// textView.removeStorageDelegate(highlighter)
298+
textView.removeStorageDelegate(highlighter)
299299
}
300300
highlighter = nil
301-
highlightProvider = nil
301+
highlightProviders.removeAll()
302302
textCoordinators.values().forEach {
303303
$0.destroy()
304304
}

Sources/CodeEditSourceEditor/Enums/CaptureModifiers.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokenModifiers
99

10-
enum CaptureModifiers: Int, CaseIterable, Sendable {
10+
public enum CaptureModifiers: Int8, CaseIterable, Sendable {
1111
case declaration
1212
case definition
1313
case readonly
@@ -21,7 +21,7 @@ enum CaptureModifiers: Int, CaseIterable, Sendable {
2121
}
2222

2323
extension CaptureModifiers: CustomDebugStringConvertible {
24-
var debugDescription: String {
24+
public var debugDescription: String {
2525
switch self {
2626
case .declaration: return "declaration"
2727
case .definition: return "definition"
@@ -37,8 +37,12 @@ extension CaptureModifiers: CustomDebugStringConvertible {
3737
}
3838
}
3939

40-
struct CaptureModifierSet: OptionSet, Equatable, Hashable {
41-
let rawValue: UInt
40+
public struct CaptureModifierSet: OptionSet, Equatable, Hashable {
41+
public let rawValue: UInt
42+
43+
public init(rawValue: UInt) {
44+
self.rawValue = rawValue
45+
}
4246

4347
static let declaration = CaptureModifierSet(rawValue: 1 << CaptureModifiers.declaration.rawValue)
4448
static let definition = CaptureModifierSet(rawValue: 1 << CaptureModifiers.definition.rawValue)
@@ -53,9 +57,9 @@ struct CaptureModifierSet: OptionSet, Equatable, Hashable {
5357

5458
var values: [CaptureModifiers] {
5559
var rawValue = self.rawValue
56-
var values: [Int] = []
60+
var values: [Int8] = []
5761
while rawValue > 0 {
58-
values.append(rawValue.trailingZeroBitCount)
62+
values.append(Int8(rawValue.trailingZeroBitCount))
5963
rawValue &= ~UInt(1 << rawValue.trailingZeroBitCount)
6064
}
6165
return values.compactMap({ CaptureModifiers(rawValue: $0) })

Sources/CodeEditSourceEditor/Enums/CaptureName.swift

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
//
77

88
/// A collection of possible capture names for `tree-sitter` with their respected raw values.
9-
public enum CaptureName: String, CaseIterable, Sendable {
9+
///
10+
/// This is `Int8` raw representable for memory considerations. In large documents there can be *lots* of these created
11+
/// and passed around, so representing them with a single integer is preferable to a string to save memory.
12+
///
13+
public enum CaptureName: Int8, CaseIterable, Sendable {
1014
case include
1115
case constructor
1216
case keyword
@@ -24,24 +28,123 @@ public enum CaptureName: String, CaseIterable, Sendable {
2428
case string
2529
case type
2630
case parameter
27-
case typeAlternate = "type_alternate"
28-
case variableBuiltin = "variable.builtin"
29-
case keywordReturn = "keyword.return"
30-
case keywordFunction = "keyword.function"
31+
case typeAlternate
32+
case variableBuiltin
33+
case keywordReturn
34+
case keywordFunction
35+
36+
var alternate: CaptureName {
37+
switch self {
38+
case .type:
39+
return .typeAlternate
40+
default:
41+
return self
42+
}
43+
}
3144

3245
/// Returns a specific capture name case from a given string.
46+
/// - Note: See ``CaptureName`` docs for why this enum isn't a raw representable.
3347
/// - Parameter string: A string to get the capture name from
3448
/// - Returns: A `CaptureNames` case
35-
static func fromString(_ string: String?) -> CaptureName? {
36-
CaptureName(rawValue: string ?? "")
49+
static func fromString(_ string: String?) -> CaptureName? { // swiftlint:disable:this cyclomatic_complexity
50+
guard let string else { return nil }
51+
switch string {
52+
case "include":
53+
return .include
54+
case "constructor":
55+
return .constructor
56+
case "keyword":
57+
return .keyword
58+
case "boolean":
59+
return .boolean
60+
case "repeat":
61+
return .repeat
62+
case "conditional":
63+
return .conditional
64+
case "tag":
65+
return .tag
66+
case "comment":
67+
return .comment
68+
case "variable":
69+
return .variable
70+
case "property":
71+
return .property
72+
case "function":
73+
return .function
74+
case "method":
75+
return .method
76+
case "number":
77+
return .number
78+
case "float":
79+
return .float
80+
case "string":
81+
return .string
82+
case "type":
83+
return .type
84+
case "parameter":
85+
return .parameter
86+
case "type_alternate":
87+
return .typeAlternate
88+
case "variable.builtin":
89+
return .variableBuiltin
90+
case "keyword.return":
91+
return .keywordReturn
92+
case "keyword.function":
93+
return .keywordFunction
94+
default:
95+
return nil
96+
}
3797
}
3898

39-
var alternate: CaptureName {
99+
/// See ``CaptureName`` docs for why this enum isn't a raw representable.
100+
var stringValue: String {
40101
switch self {
102+
case .include:
103+
return "include"
104+
case .constructor:
105+
return "constructor"
106+
case .keyword:
107+
return "keyword"
108+
case .boolean:
109+
return "boolean"
110+
case .repeat:
111+
return "`repeat`"
112+
case .conditional:
113+
return "conditional"
114+
case .tag:
115+
return "tag"
116+
case .comment:
117+
return "comment"
118+
case .variable:
119+
return "variable"
120+
case .property:
121+
return "property"
122+
case .function:
123+
return "function"
124+
case .method:
125+
return "method"
126+
case .number:
127+
return "number"
128+
case .float:
129+
return "float"
130+
case .string:
131+
return "string"
41132
case .type:
42-
return .typeAlternate
43-
default:
44-
return self
133+
return "type"
134+
case .parameter:
135+
return "parameter"
136+
case .typeAlternate:
137+
return "typeAlternate"
138+
case .variableBuiltin:
139+
return "variableBuiltin"
140+
case .keywordReturn:
141+
return "keywordReturn"
142+
case .keywordFunction:
143+
return "keywordFunction"
45144
}
46145
}
47146
}
147+
148+
extension CaptureName: CustomDebugStringConvertible {
149+
public var debugDescription: String { stringValue }
150+
}

Sources/CodeEditSourceEditor/Extensions/NSEdgeInsets+Equatable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
extension NSEdgeInsets: Equatable {
10+
extension NSEdgeInsets: @retroactive Equatable {
1111
public static func == (lhs: NSEdgeInsets, rhs: NSEdgeInsets) -> Bool {
1212
lhs.bottom == rhs.bottom &&
1313
lhs.top == rhs.top &&

Sources/CodeEditSourceEditor/Extensions/NSRange+/NSRange+Comparable.swift

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

Sources/CodeEditSourceEditor/Extensions/TextView+/TextView+TextFormation.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import CodeEditTextView
1010
import TextStory
1111
import TextFormation
1212

13-
extension TextView: TextInterface {
13+
extension TextView: @retroactive TextStoring {}
14+
15+
extension TextView: @retroactive TextInterface {
1416
public var selectedRange: NSRange {
1517
get {
1618
return selectionManager

0 commit comments

Comments
 (0)