Skip to content

Commit 327fc72

Browse files
committed
Code Style, Add Docs, Clean Tests
1 parent b7bd8fc commit 327fc72

File tree

10 files changed

+58
-50
lines changed

10 files changed

+58
-50
lines changed

Sources/CodeEditSourceEditor/Enums/CaptureModifier.swift

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

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

10+
/// A collection of possible syntax capture modifiers. Represented by an integer for memory efficiency, and with the
11+
/// ability to convert to and from strings for ease of use with tools.
12+
///
13+
/// These are useful for helping differentiate between similar types of syntax. Eg two variables may be declared like
14+
/// ```swift
15+
/// var a = 1
16+
/// let b = 1
17+
/// ```
18+
/// ``CaptureName`` will represent both these later in code, but combined ``CaptureModifier`` themes can differentiate
19+
/// between constants (`b` in the example) and regular variables (`a` in the example).
20+
///
21+
/// This is `Int8` raw representable for memory considerations. In large documents there can be *lots* of these created
22+
/// and passed around, so representing them with a single integer is preferable to a string to save memory.
23+
///
1024
public enum CaptureModifier: Int8, CaseIterable, Sendable {
1125
case declaration
1226
case definition
@@ -74,20 +88,7 @@ public enum CaptureModifier: Int8, CaseIterable, Sendable {
7488
}
7589

7690
extension CaptureModifier: CustomDebugStringConvertible {
77-
public var debugDescription: String {
78-
switch self {
79-
case .declaration: return "declaration"
80-
case .definition: return "definition"
81-
case .readonly: return "readonly"
82-
case .static: return "static"
83-
case .deprecated: return "deprecated"
84-
case .abstract: return "abstract"
85-
case .async: return "async"
86-
case .modification: return "modification"
87-
case .documentation: return "documentation"
88-
case .defaultLibrary: return "defaultLibrary"
89-
}
90-
}
91+
public var debugDescription: String { stringValue }
9192
}
9293

9394
/// A set of capture modifiers, efficiently represented by a single integer.
@@ -112,9 +113,15 @@ public struct CaptureModifierSet: OptionSet, Equatable, Hashable, Sendable {
112113
/// All values in the set.
113114
public var values: [CaptureModifier] {
114115
var rawValue = self.rawValue
116+
117+
// This set is represented by an integer, where each `1` in the binary number represents a value.
118+
// We can treat the index of the `1` as the raw value of a ``CaptureModifier`` (the index in 0b0100 would be 2).
119+
// This loops through each `1` in the `rawValue`, finds the represented modifier, and 0's out the `1` so we can
120+
// get the next one using the binary & operator (0b0110 -> 0b0100 -> 0b0000 -> finish).
115121
var values: [Int8] = []
116122
while rawValue > 0 {
117123
values.append(Int8(rawValue.trailingZeroBitCount))
124+
// Clears the bit at the desired index (0b011 & 0b110 = 0b010 if clearing index 0)
118125
rawValue &= ~UInt(1 << rawValue.trailingZeroBitCount)
119126
}
120127
return values.compactMap({ CaptureModifier(rawValue: $0) })

Sources/CodeEditSourceEditor/Enums/CaptureName.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// Created by Lukas Pistrol on 16.08.22.
66
//
77

8-
/// A collection of possible capture names for `tree-sitter` with their respected raw values.
8+
/// A collection of possible syntax capture types. Represented by an integer for memory efficiency, and with the
9+
/// ability to convert to and from strings for ease of use with tools.
910
///
1011
/// This is `Int8` raw representable for memory considerations. In large documents there can be *lots* of these created
1112
/// and passed around, so representing them with a single integer is preferable to a string to save memory.

Sources/CodeEditSourceEditor/Highlighting/HighlighProviding/HighlightProviderState.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class HighlightProviderState {
4040
/// Any indexes that highlights have been requested for, but haven't been applied.
4141
/// Indexes/ranges are added to this when highlights are requested and removed
4242
/// after they are applied
43-
private var pendingSet: IndexSet = .init()
43+
private var pendingSet: IndexSet = IndexSet()
4444

4545
/// The set of valid indexes
46-
private var validSet: IndexSet = .init()
46+
private var validSet: IndexSet = IndexSet()
4747

4848
// MARK: - Providers
4949

Sources/CodeEditSourceEditor/Highlighting/HighlightRange.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import Foundation
99

1010
/// This struct represents a range to highlight, as well as the capture name for syntax coloring.
1111
public struct HighlightRange: Sendable {
12-
let range: NSRange
13-
let capture: CaptureName?
14-
let modifiers: CaptureModifierSet
12+
public let range: NSRange
13+
public let capture: CaptureName?
14+
public let modifiers: CaptureModifierSet
1515

1616
public init(range: NSRange, capture: CaptureName?, modifiers: CaptureModifierSet = []) {
1717
self.range = range

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeContainer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class StyledRangeContainer {
4040
///
4141
/// - Parameter range: The range to query.
4242
/// - Returns: An array of continuous styled runs.
43-
func runsIn(range: NSRange) -> [HighlightedRun] {
43+
func runsIn(range: NSRange) -> [StyledRangeStoreRun] {
4444
// Ordered by priority, lower = higher priority.
4545
var allRuns = _storage.sorted(by: { $0.key < $1.key }).map { $0.value.runs(in: range.intRange) }
46-
var runs: [HighlightedRun] = []
46+
var runs: [StyledRangeStoreRun] = []
4747

4848
var minValue = allRuns.compactMap { $0.last }.enumerated().min(by: { $0.1.length < $1.1.length })
4949

@@ -98,7 +98,7 @@ extension StyledRangeContainer: HighlightProviderStateDelegate {
9898
assertionFailure("No storage found for the given provider: \(provider)")
9999
return
100100
}
101-
var runs: [HighlightedRun] = []
101+
var runs: [StyledRangeStoreRun] = []
102102
var lastIndex = rangeToHighlight.lowerBound
103103

104104
for highlight in highlights {
@@ -108,7 +108,7 @@ extension StyledRangeContainer: HighlightProviderStateDelegate {
108108
continue // Skip! Overlapping
109109
}
110110
runs.append(
111-
HighlightedRun(
111+
StyledRangeStoreRun(
112112
length: highlight.range.length,
113113
capture: highlight.capture,
114114
modifiers: highlight.modifiers

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import _RopeModule
1313
/// Internally this class uses a `Rope` from the swift-collections package, allowing for efficient updates and
1414
/// retrievals.
1515
final class StyledRangeStore {
16-
typealias Run = HighlightedRun
16+
typealias Run = StyledRangeStoreRun
1717
typealias Index = Rope<StyledRun>.Index
1818
var _guts = Rope<StyledRun>()
1919

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/HighlightedRun.swift renamed to Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStoreRun.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
//
2-
// HighlightedRun.swift
2+
// StyledRangeStoreRun.swift
33
// CodeEditSourceEditor
44
//
55
// Created by Khan Winter on 11/4/24.
66
//
77

88
/// Consumer-facing value type for the stored values in this container.
9-
struct HighlightedRun: Equatable, Hashable {
9+
struct StyledRangeStoreRun: Equatable, Hashable {
1010
var length: Int
1111
var capture: CaptureName?
1212
var modifiers: CaptureModifierSet
1313

1414
static func empty(length: Int) -> Self {
15-
HighlightedRun(length: length, capture: nil, modifiers: [])
15+
StyledRangeStoreRun(length: length, capture: nil, modifiers: [])
1616
}
1717

1818
var isEmpty: Bool {
1919
capture == nil && modifiers.isEmpty
2020
}
2121

22-
mutating package func combineLowerPriority(_ other: borrowing HighlightedRun) {
22+
mutating package func combineLowerPriority(_ other: borrowing StyledRangeStoreRun) {
2323
if self.capture == nil {
2424
self.capture = other.capture
2525
}
2626
self.modifiers.formUnion(other.modifiers)
2727
}
2828

29-
mutating package func combineHigherPriority(_ other: borrowing HighlightedRun) {
29+
mutating package func combineHigherPriority(_ other: borrowing StyledRangeStoreRun) {
3030
self.capture = other.capture ?? self.capture
3131
self.modifiers.formUnion(other.modifiers)
3232
}
3333

34-
mutating package func subtractLength(_ other: borrowing HighlightedRun) {
34+
mutating package func subtractLength(_ other: borrowing StyledRangeStoreRun) {
3535
self.length -= other.length
3636
}
3737
}
3838

39-
extension HighlightedRun: CustomDebugStringConvertible {
39+
extension StyledRangeStoreRun: CustomDebugStringConvertible {
4040
var debugDescription: String {
4141
if isEmpty {
4242
"\(length) (empty)"

Tests/CodeEditSourceEditorTests/Highlighting/HighlightProviderStateTest.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ class EmptyHighlightProviderStateDelegate: HighlightProviderStateDelegate {
2222
}
2323

2424
final class HighlightProviderStateTest: XCTestCase {
25+
var textView: TextView!
26+
var rangeProvider: MockVisibleRangeProvider!
27+
var delegate: EmptyHighlightProviderStateDelegate!
28+
2529
@MainActor
26-
func test_setup() {
27-
let textView = Mock.textView()
28-
let rangeProvider = MockVisibleRangeProvider(textView: textView)
29-
let delegate = EmptyHighlightProviderStateDelegate()
30+
override func setUp() async throws {
31+
try await super.setUp()
32+
textView = Mock.textView()
33+
rangeProvider = MockVisibleRangeProvider(textView: textView)
34+
delegate = EmptyHighlightProviderStateDelegate()
35+
}
3036

37+
@MainActor
38+
func test_setup() {
3139
let setUpExpectation = XCTestExpectation(description: "Set up called.")
3240

3341
let mockProvider = Mock.highlightProvider(
@@ -52,10 +60,6 @@ final class HighlightProviderStateTest: XCTestCase {
5260

5361
@MainActor
5462
func test_setLanguage() {
55-
let textView = Mock.textView()
56-
let rangeProvider = MockVisibleRangeProvider(textView: textView)
57-
let delegate = EmptyHighlightProviderStateDelegate()
58-
5963
let firstSetUpExpectation = XCTestExpectation(description: "Set up called.")
6064
let secondSetUpExpectation = XCTestExpectation(description: "Set up called.")
6165

@@ -92,10 +96,6 @@ final class HighlightProviderStateTest: XCTestCase {
9296

9397
@MainActor
9498
func test_storageUpdatedRangesPassedOn() {
95-
let textView = Mock.textView()
96-
let rangeProvider = MockVisibleRangeProvider(textView: textView)
97-
let delegate = EmptyHighlightProviderStateDelegate()
98-
9999
var updatedRanges: [(NSRange, Int)] = []
100100

101101
let mockProvider = Mock.highlightProvider(

Tests/CodeEditSourceEditorTests/Highlighting/StyledRangeContainerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import XCTest
22
@testable import CodeEditSourceEditor
33

44
final class StyledRangeContainerTests: XCTestCase {
5-
typealias Run = HighlightedRun
5+
typealias Run = StyledRangeStoreRun
66

77
@MainActor
88
func test_init() {
@@ -27,9 +27,9 @@ final class StyledRangeContainerTests: XCTestCase {
2727

2828
XCTAssertNotNil(store._storage[providers[0]])
2929
XCTAssertEqual(store._storage[providers[0]]!.count, 3)
30-
XCTAssertEqual(store._storage[providers[0]]!.runs(in: 0..<100)[0].capture, nil)
30+
XCTAssertNil(store._storage[providers[0]]!.runs(in: 0..<100)[0].capture)
3131
XCTAssertEqual(store._storage[providers[0]]!.runs(in: 0..<100)[1].capture, .comment)
32-
XCTAssertEqual(store._storage[providers[0]]!.runs(in: 0..<100)[2].capture, nil)
32+
XCTAssertNil(store._storage[providers[0]]!.runs(in: 0..<100)[2].capture)
3333

3434
XCTAssertEqual(
3535
store.runsIn(range: NSRange(location: 0, length: 100)),

Tests/CodeEditSourceEditorTests/Highlighting/StyledRangeStoreTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ final class StyledRangeStoreTests: XCTestCase {
120120
XCTAssertEqual(runs[1].length, 5)
121121
XCTAssertEqual(runs[2].length, 50)
122122

123-
XCTAssertEqual(runs[0].capture, nil)
123+
XCTAssertNil(runs[0].capture)
124124
XCTAssertEqual(runs[1].capture, .comment)
125-
XCTAssertEqual(runs[2].capture, nil)
125+
XCTAssertNil(runs[2].capture)
126126

127127
XCTAssertEqual(runs[0].modifiers, [])
128128
XCTAssertEqual(runs[1].modifiers, [.static])
@@ -141,7 +141,7 @@ final class StyledRangeStoreTests: XCTestCase {
141141
XCTAssertEqual(runs[1].length, 50)
142142

143143
XCTAssertEqual(runs[0].capture, .comment)
144-
XCTAssertEqual(runs[1].capture, nil)
144+
XCTAssertNil(runs[1].capture)
145145

146146
XCTAssertEqual(runs[0].modifiers, [.static])
147147
XCTAssertEqual(runs[1].modifiers, [])

0 commit comments

Comments
 (0)