Skip to content

Commit 9b300d8

Browse files
authored
Merge pull request #53 from lukepistrol/chore/capture-names
chore: added `CaptureNames` enum, added tests
2 parents dcf1973 + 48ae73c commit 9b300d8

3 files changed

Lines changed: 119 additions & 16 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// STTextViewController+CaptureNames.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Lukas Pistrol on 16.08.22.
6+
//
7+
8+
import Foundation
9+
10+
internal extension STTextViewController {
11+
12+
/// A collection of possible capture names for `tree-sitter` with their respected raw values.
13+
enum CaptureNames: String, CaseIterable {
14+
case include
15+
case constructor
16+
case keyword
17+
case boolean
18+
case `repeat`
19+
case conditional
20+
case tag
21+
case comment
22+
case variable
23+
case property
24+
case function
25+
case method
26+
case number
27+
case float
28+
case string
29+
case type
30+
case parameter
31+
case typeAlternate = "type_alternate"
32+
case variableBuiltin = "variable.builtin"
33+
case keywordReturn = "keyword.return"
34+
case keywordFunction = "keyword.function"
35+
36+
/// Returns a specific capture name case from a given string.
37+
/// - Parameter string: A string to get the capture name from
38+
/// - Returns: A `CaptureNames` case
39+
static func fromString(_ string: String?) -> CaptureNames? {
40+
allCases.first { $0.rawValue == string }
41+
}
42+
}
43+
}

Sources/CodeEditTextView/STTextViewController+TreeSitter.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,20 @@ internal extension STTextViewController {
7777
/// - Parameter capture: The capture name
7878
/// - Returns: A `NSColor`
7979
func colorForCapture(_ capture: String?) -> NSColor {
80-
let colors = theme
81-
switch capture {
82-
case "include", "constructor", "keyword", "boolean", "variable.builtin",
83-
"keyword.return", "keyword.function", "repeat", "conditional", "tag":
84-
return colors.keywords
85-
case "comment": return colors.comments
86-
case "variable", "property": return colors.variables
87-
case "function", "method": return colors.variables
88-
case "number", "float": return colors.numbers
89-
case "string": return colors.strings
90-
case "type": return colors.types
91-
case "parameter": return colors.variables
92-
case "type_alternate": return colors.attributes
93-
default:
94-
// print(capture)
95-
return colors.text
80+
let captureName = CaptureNames.fromString(capture)
81+
switch captureName {
82+
case .include, .constructor, .keyword, .boolean, .variableBuiltin,
83+
.keywordReturn, .keywordFunction, .repeat, .conditional, .tag:
84+
return theme.keywords
85+
case .comment: return theme.comments
86+
case .variable, .property: return theme.variables
87+
case .function, .method: return theme.variables
88+
case .number, .float: return theme.numbers
89+
case .string: return theme.strings
90+
case .type: return theme.types
91+
case .parameter: return theme.variables
92+
case .typeAlternate: return theme.attributes
93+
default: return theme.text
9694
}
9795
}
9896
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import XCTest
2+
@testable import CodeEditTextView
3+
import SwiftTreeSitter
4+
5+
final class STTextViewControllerTests: XCTestCase {
6+
7+
var controller: STTextViewController!
8+
var theme: EditorTheme!
9+
10+
override func setUpWithError() throws {
11+
theme = EditorTheme(
12+
text: .textColor,
13+
insertionPoint: .textColor,
14+
invisibles: .gray,
15+
background: .textBackgroundColor,
16+
lineHighlight: .highlightColor,
17+
selection: .selectedTextColor,
18+
keywords: .systemPink,
19+
commands: .systemBlue,
20+
types: .systemMint,
21+
attributes: .systemTeal,
22+
variables: .systemCyan,
23+
values: .systemOrange,
24+
numbers: .systemYellow,
25+
strings: .systemRed,
26+
characters: .systemRed,
27+
comments: .systemGreen
28+
)
29+
controller = STTextViewController(
30+
text: .constant(""),
31+
language: .default,
32+
font: .monospacedSystemFont(ofSize: 11, weight: .medium),
33+
theme: theme,
34+
tabWidth: 4
35+
)
36+
}
37+
38+
func test_captureNames() throws {
39+
// test for "keyword"
40+
let captureName1 = "keyword"
41+
let color1 = controller.colorForCapture(captureName1)
42+
XCTAssertEqual(color1, .systemPink)
43+
44+
// test for "comment"
45+
let captureName2 = "comment"
46+
let color2 = controller.colorForCapture(captureName2)
47+
XCTAssertEqual(color2, .systemGreen)
48+
49+
/* ... additional tests here ... */
50+
51+
// test for empty case
52+
let captureName3 = ""
53+
let color3 = controller.colorForCapture(captureName3)
54+
XCTAssertEqual(color3, .textColor)
55+
56+
// test for random case
57+
let captureName4 = "abc123"
58+
let color4 = controller.colorForCapture(captureName4)
59+
XCTAssertEqual(color4, .textColor)
60+
}
61+
62+
}

0 commit comments

Comments
 (0)