Skip to content

Commit 2749e2e

Browse files
committed
Fix parsing includes and imports
1 parent b17d282 commit 2749e2e

File tree

3 files changed

+155
-33
lines changed

3 files changed

+155
-33
lines changed

Tool/Sources/FocusedCodeFinder/ObjectiveC/ObjectiveCCodeFinder.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ public class ObjectiveCFocusedCodeFinder: KnownLanguageFocusedCodeFinder<
172172
textProvider: @escaping TextProvider
173173
) -> NodeInfo? {
174174
guard let typeNode = node.child(byFieldName: "type") else { return nil }
175-
return parseSignatureBeforeBody(typeNode, textProvider: textProvider)
175+
guard var result = parseSignatureBeforeBody(typeNode, textProvider: textProvider)
176+
else { return nil }
177+
result.signature = "typedef \(result.signature)"
178+
result.node = node
179+
return result
176180
}
177181

178182
func parseFunctionDefinitionNode(

Tool/Sources/FocusedCodeFinder/ObjectiveC/ObjectiveCScopeHierarchySyntaxVisitor.swift

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ final class ObjectiveCScopeHierarchySyntaxVisitor: ASTTreeVisitor {
2323
self.textProvider = textProvider
2424
super.init(tree: tree)
2525
}
26-
26+
2727
/// The nodes containing the current range, sorted from inner to outer.
2828
func findScopeHierarchy(_ node: ASTNode) -> [ASTNode] {
2929
walk(node)
3030
return _scopeHierarchy.sorted { $0.range.location > $1.range.location }
3131
}
32-
32+
3333
/// The nodes containing the current range, sorted from inner to outer.
3434
func findScopeHierarchy() -> [ASTNode] {
3535
walk()
@@ -70,7 +70,7 @@ final class ObjectiveCScopeHierarchySyntaxVisitor: ASTTreeVisitor {
7070
case .structSpecifier, .enumSpecifier, .nsEnumSpecifier:
7171
guard cursorRange.strictlyContains(range) else { return .skipChildren }
7272
_scopeHierarchy.append(node)
73-
return .skipChildren
73+
return .skipChildren
7474
case .functionDefinition:
7575
guard cursorRange.strictlyContains(range) else { return .skipChildren }
7676
_scopeHierarchy.append(node)
@@ -85,45 +85,28 @@ final class ObjectiveCScopeHierarchySyntaxVisitor: ASTTreeVisitor {
8585
// MARK: Imports
8686

8787
func handlePreprocInclude(_ node: ASTNode) {
88-
let children = node.children
89-
for child in children {
90-
if let pathNode = child.child(byFieldName: "path") {
91-
let path = textProvider(pathNode)
92-
if !path.isEmpty {
93-
includes.append(path.replacingOccurrences(of: "\"", with: ""))
94-
}
95-
break
88+
if let pathNode = node.child(byFieldName: "path") {
89+
let path = textProvider(pathNode)
90+
if !path.isEmpty {
91+
includes.append(path)
9692
}
9793
}
9894
}
9995

10096
func handlePreprocImport(_ node: ASTNode) {
101-
let children = node.children
102-
for child in children {
103-
if let pathNode = child.child(byFieldName: "path") {
104-
let path = textProvider(pathNode)
105-
if !path.isEmpty {
106-
imports.append(
107-
path
108-
.replacingOccurrences(of: "\"", with: "")
109-
.replacingOccurrences(of: "<", with: "")
110-
.replacingOccurrences(of: ">", with: "")
111-
)
112-
}
113-
break
97+
if let pathNode = node.child(byFieldName: "path") {
98+
let path = textProvider(pathNode)
99+
if !path.isEmpty {
100+
imports.append(path)
114101
}
115102
}
116103
}
117104

118105
func handleModuleImport(_ node: ASTNode) {
119-
let children = node.children
120-
for child in children {
121-
if let pathNode = child.child(byFieldName: "module") {
122-
let path = textProvider(pathNode)
123-
if !path.isEmpty {
124-
imports.append(path)
125-
}
126-
break
106+
if let pathNode = node.child(byFieldName: "module") {
107+
let path = textProvider(pathNode)
108+
if !path.isEmpty {
109+
imports.append(path)
127110
}
128111
}
129112
}

Tool/Tests/FocusedCodeFinderTests/ObjectiveCFocusedCodeFinderTests.swift

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,140 @@ final class ObjectiveCFocusedCodeFinder_Selection_Tests: XCTestCase {
231231
includes: []
232232
))
233233
}
234+
235+
func test_selecting_a_line_inside_a_struct_the_scope_should_be_the_struct() {
236+
let code = """
237+
struct Foo {
238+
NSInteger foo;
239+
NSInteger bar;
240+
NSInteger baz;
241+
}
242+
"""
243+
let range = CursorRange(
244+
start: CursorPosition(line: 1, character: 0),
245+
end: CursorPosition(line: 3, character: 31)
246+
)
247+
let context = ObjectiveCFocusedCodeFinder().findFocusedCode(
248+
in: document(code: code),
249+
containingRange: range
250+
)
251+
XCTAssertEqual(context, .init(
252+
scope: .scope(signature: [
253+
.init(
254+
signature: "struct Foo",
255+
name: "Foo",
256+
range: .init(startPair: (0, 0), endPair: (4, 1))
257+
),
258+
]),
259+
contextRange: .init(startPair: (0, 0), endPair: (4, 1)),
260+
focusedRange: range,
261+
focusedCode: """
262+
NSInteger foo;
263+
NSInteger bar;
264+
NSInteger baz;
265+
266+
""",
267+
imports: [],
268+
includes: []
269+
))
270+
}
271+
272+
func test_selecting_a_line_inside_a_enum_the_scope_should_be_the_enum() {
273+
let code = """
274+
enum Foo {
275+
foo,
276+
bar,
277+
baz
278+
};
279+
"""
280+
let range = CursorRange(
281+
start: CursorPosition(line: 1, character: 0),
282+
end: CursorPosition(line: 3, character: 31)
283+
)
284+
let context = ObjectiveCFocusedCodeFinder().findFocusedCode(
285+
in: document(code: code),
286+
containingRange: range
287+
)
288+
XCTAssertEqual(context, .init(
289+
scope: .scope(signature: [
290+
.init(
291+
signature: "enum Foo",
292+
name: "Foo",
293+
range: .init(startPair: (0, 0), endPair: (4, 1))
294+
),
295+
]),
296+
contextRange: .init(startPair: (0, 0), endPair: (4, 1)),
297+
focusedRange: range,
298+
focusedCode: """
299+
foo,
300+
bar,
301+
baz
302+
303+
""",
304+
imports: [],
305+
includes: []
306+
))
307+
}
308+
309+
func test_selecting_a_line_inside_an_NSEnum_the_scope_should_be_the_enum() {
310+
let code = """
311+
typedef NS_ENUM(NSInteger, Foo) {
312+
foo,
313+
bar,
314+
baz
315+
};
316+
"""
317+
let range = CursorRange(
318+
start: CursorPosition(line: 1, character: 0),
319+
end: CursorPosition(line: 3, character: 31)
320+
)
321+
let context = ObjectiveCFocusedCodeFinder().findFocusedCode(
322+
in: document(code: code),
323+
containingRange: range
324+
)
325+
XCTAssertEqual(context, .init(
326+
scope: .scope(signature: [
327+
.init(
328+
signature: "typedef NS_ENUM(NSInteger, Foo)",
329+
name: "Foo",
330+
range: .init(startPair: (0, 0), endPair: (4, 2))
331+
),
332+
]),
333+
contextRange: .init(startPair: (0, 0), endPair: (4, 2)),
334+
focusedRange: range,
335+
focusedCode: """
336+
foo,
337+
bar,
338+
baz
339+
340+
""",
341+
imports: [],
342+
includes: []
343+
))
344+
}
234345
}
235346

347+
final class ObjectiveCFocusedCodeFinder_Imports_Tests: XCTestCase {
348+
func test_parsing_imports() {
349+
let code = """
350+
#import <Foundation/Foundation.h>
351+
@import UIKit;
352+
#import "Foo.h"
353+
#include "Bar.h"
354+
"""
355+
356+
let context = ObjectiveCFocusedCodeFinder().findFocusedCode(
357+
in: document(code: code),
358+
containingRange: .zero
359+
)
360+
361+
XCTAssertEqual(context.imports, [
362+
"<Foundation/Foundation.h>",
363+
"UIKit",
364+
"\"Foo.h\""
365+
])
366+
XCTAssertEqual(context.includes, [
367+
"\"Bar.h\""
368+
])
369+
}
370+
}

0 commit comments

Comments
 (0)