Skip to content

Commit 11b03e8

Browse files
Merge pull request #31 from aboutcode-org/add-swift-parser
Add swift parser for source symbols
2 parents 5e501c7 + c9ffffd commit 11b03e8

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed

Diff for: CHANGELOG.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
=========
33

4+
v0.6.0
5+
------
6+
7+
- Bump tree-sitter to v0.23
8+
- Collect swift source symbols and strings using tree-sitter https://github.com/aboutcode-org/source-inspector/pull/31
9+
- Collect Objective C source symbols and strings using tree-sitter https://github.com/aboutcode-org/source-inspector/pull/30
10+
- Collect C# source symbols and strings using tree-sitter https://github.com/aboutcode-org/source-inspector/pull/32
11+
- Fix javascript and add typescript source symbols and strings collection using tree-sitter https://github.com/aboutcode-org/source-inspector/pull/33
12+
413
v0.5.2
514
------
615

Diff for: setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ install_requires =
5656
tree-sitter-objc==3.0.2
5757
tree-sitter-python==0.21.0
5858
tree-sitter-rust==0.21.2
59+
# See https://github.com/alex-pinkus/tree-sitter-swift/issues/432
60+
tree-sitter-swift @ git+https://github.com/alex-pinkus/tree-sitter-swift.git@9253825dd2570430b53fa128cbb40cb62498e75d
5961
pygments
6062

6163
[options.packages.find]

Diff for: src/source_inspector/symbols_tree_sitter.py

+5
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ def traverse(node, symbols, strings, language_info):
200200
"identifiers": ["identifier"],
201201
"string_literals": ["raw_string_literal"],
202202
},
203+
"Swift": {
204+
"wheel": "tree_sitter_swift",
205+
"identifiers": ["simple_identifier"],
206+
"string_literals": ["line_str_text"],
207+
},
203208
}
204209

205210

Diff for: tests/data/symbols_tree_sitter/swift/Client.swift

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import NIOCore
2+
import Logging
3+
import NIOHTTP1
4+
5+
let str1 = "Hello, world!"
6+
7+
public protocol Client: Sendable {
8+
var eventLoop: EventLoop { get }
9+
var byteBufferAllocator: ByteBufferAllocator { get }
10+
func delegating(to eventLoop: EventLoop) -> Client
11+
func logging(to logger: Logger) -> Client
12+
func allocating(to byteBufferAllocator: ByteBufferAllocator) -> Client
13+
func send(_ request: ClientRequest) -> EventLoopFuture<ClientResponse>
14+
}
15+
16+
extension Client {
17+
public func logging(to logger: Logger) -> Client {
18+
return self
19+
}
20+
21+
public func allocating(to byteBufferAllocator: ByteBufferAllocator) -> Client {
22+
return self
23+
}
24+
25+
public var byteBufferAllocator: ByteBufferAllocator {
26+
return ByteBufferAllocator()
27+
}
28+
}
29+
30+
extension Client {
31+
public func get(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
32+
return self.send(.GET, headers: headers, to: url, beforeSend: beforeSend)
33+
}
34+
35+
public func post(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
36+
return self.send(.POST, headers: headers, to: url, beforeSend: beforeSend)
37+
}
38+
39+
public func patch(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
40+
return self.send(.PATCH, headers: headers, to: url, beforeSend: beforeSend)
41+
}
42+
43+
public func put(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
44+
return self.send(.PUT, headers: headers, to: url, beforeSend: beforeSend)
45+
}
46+
47+
public func delete(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
48+
return self.send(.DELETE, headers: headers, to: url, beforeSend: beforeSend)
49+
}
50+
51+
public func post<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
52+
return self.post(url, headers: headers, beforeSend: { try $0.content.encode(content) })
53+
}
54+
55+
public func patch<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
56+
return self.patch(url, headers: headers, beforeSend: { try $0.content.encode(content) })
57+
}
58+
59+
public func put<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
60+
return self.put(url, headers: headers, beforeSend: { try $0.content.encode(content) })
61+
}
62+
63+
public func send(
64+
_ method: HTTPMethod,
65+
headers: HTTPHeaders = [:],
66+
to url: URI,
67+
beforeSend: (inout ClientRequest) throws -> () = { _ in }
68+
) -> EventLoopFuture<ClientResponse> {
69+
var request = ClientRequest(method: method, url: url, headers: headers, body: nil, byteBufferAllocator: self.byteBufferAllocator)
70+
do {
71+
try beforeSend(&request)
72+
} catch {
73+
return self.eventLoop.makeFailedFuture(error)
74+
}
75+
return self.send(request)
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
{
2+
"files": [
3+
{
4+
"path": "Client.swift",
5+
"type": "file",
6+
"source_symbols": [
7+
"NIOCore",
8+
"Logging",
9+
"NIOHTTP1",
10+
"str1",
11+
"eventLoop",
12+
"byteBufferAllocator",
13+
"delegating",
14+
"to",
15+
"eventLoop",
16+
"logging",
17+
"to",
18+
"logger",
19+
"allocating",
20+
"to",
21+
"byteBufferAllocator",
22+
"send",
23+
"_",
24+
"request",
25+
"logging",
26+
"to",
27+
"logger",
28+
"allocating",
29+
"to",
30+
"byteBufferAllocator",
31+
"byteBufferAllocator",
32+
"ByteBufferAllocator",
33+
"get",
34+
"_",
35+
"url",
36+
"headers",
37+
"beforeSend",
38+
"_",
39+
"send",
40+
"GET",
41+
"headers",
42+
"headers",
43+
"to",
44+
"url",
45+
"beforeSend",
46+
"beforeSend",
47+
"post",
48+
"_",
49+
"url",
50+
"headers",
51+
"beforeSend",
52+
"_",
53+
"send",
54+
"POST",
55+
"headers",
56+
"headers",
57+
"to",
58+
"url",
59+
"beforeSend",
60+
"beforeSend",
61+
"patch",
62+
"_",
63+
"url",
64+
"headers",
65+
"beforeSend",
66+
"_",
67+
"send",
68+
"PATCH",
69+
"headers",
70+
"headers",
71+
"to",
72+
"url",
73+
"beforeSend",
74+
"beforeSend",
75+
"put",
76+
"_",
77+
"url",
78+
"headers",
79+
"beforeSend",
80+
"_",
81+
"send",
82+
"PUT",
83+
"headers",
84+
"headers",
85+
"to",
86+
"url",
87+
"beforeSend",
88+
"beforeSend",
89+
"delete",
90+
"_",
91+
"url",
92+
"headers",
93+
"beforeSend",
94+
"_",
95+
"send",
96+
"DELETE",
97+
"headers",
98+
"headers",
99+
"to",
100+
"url",
101+
"beforeSend",
102+
"beforeSend",
103+
"post",
104+
"_",
105+
"url",
106+
"headers",
107+
"content",
108+
"T",
109+
"post",
110+
"url",
111+
"headers",
112+
"headers",
113+
"beforeSend",
114+
"$0",
115+
"content",
116+
"encode",
117+
"content",
118+
"patch",
119+
"_",
120+
"url",
121+
"headers",
122+
"content",
123+
"T",
124+
"patch",
125+
"url",
126+
"headers",
127+
"headers",
128+
"beforeSend",
129+
"$0",
130+
"content",
131+
"encode",
132+
"content",
133+
"put",
134+
"_",
135+
"url",
136+
"headers",
137+
"content",
138+
"T",
139+
"put",
140+
"url",
141+
"headers",
142+
"headers",
143+
"beforeSend",
144+
"$0",
145+
"content",
146+
"encode",
147+
"content",
148+
"send",
149+
"_",
150+
"method",
151+
"headers",
152+
"to",
153+
"url",
154+
"beforeSend",
155+
"_",
156+
"request",
157+
"ClientRequest",
158+
"method",
159+
"method",
160+
"url",
161+
"url",
162+
"headers",
163+
"headers",
164+
"body",
165+
"byteBufferAllocator",
166+
"byteBufferAllocator",
167+
"beforeSend",
168+
"request",
169+
"eventLoop",
170+
"makeFailedFuture",
171+
"error",
172+
"send",
173+
"request"
174+
],
175+
"source_strings": [
176+
"Hello, world!"
177+
],
178+
"scan_errors": []
179+
}
180+
]
181+
}

Diff for: tests/test_symbols_tree_sitter.py

+9
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,12 @@ def test_symbols_strings_typescript(self):
9393

9494
expected_loc = self.get_test_loc("typescript/main.ts-expected.json")
9595
check_json_scan(expected_loc, result_file, regen=REGEN_TEST_FIXTURES)
96+
97+
def test_symbols_strings_swift(self):
98+
test_file = self.get_test_loc("swift/Client.swift")
99+
result_file = self.get_temp_file("json")
100+
args = ["--treesitter-symbol-and-string", test_file, "--json-pp", result_file]
101+
run_scan_click(args)
102+
103+
expected_loc = self.get_test_loc("swift/Client.swift-expected.json")
104+
check_json_scan(expected_loc, result_file, regen=REGEN_TEST_FIXTURES)

0 commit comments

Comments
 (0)