forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectiveCScopeHierarchySyntaxVisitor.swift
More file actions
114 lines (102 loc) · 3.73 KB
/
ObjectiveCScopeHierarchySyntaxVisitor.swift
File metadata and controls
114 lines (102 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import ASTParser
import Foundation
import Preferences
import SuggestionModel
import SwiftTreeSitter
final class ObjectiveCScopeHierarchySyntaxVisitor: ASTTreeVisitor {
let range: CursorRange
let code: String
let textProvider: (ASTNode) -> String
var includes: [String] = []
var imports: [String] = []
private var _scopeHierarchy: [ASTNode] = []
init(
tree: ASTTree,
code: String,
textProvider: @escaping (ASTNode) -> String,
range: CursorRange
) {
self.range = range
self.code = code
self.textProvider = textProvider
super.init(tree: tree)
}
/// The nodes containing the current range, sorted from inner to outer.
func findScopeHierarchy(_ node: ASTNode) -> [ASTNode] {
walk(node)
return _scopeHierarchy.sorted { $0.range.location > $1.range.location }
}
/// The nodes containing the current range, sorted from inner to outer.
func findScopeHierarchy() -> [ASTNode] {
walk()
return _scopeHierarchy.sorted { $0.range.location > $1.range.location }
}
override func visit(_ node: ASTNode) -> ASTTreeVisitorContinueKind {
let cursorRange = CursorRange(pointRange: node.pointRange)
switch ObjectiveCNodeType(rawValue: node.nodeType ?? "") {
case .translationUnit:
return .visitChildren
case .preprocInclude:
handlePreprocInclude(node)
return .skipChildren
case .preprocImport:
handlePreprocImport(node)
return .skipChildren
case .moduleImport:
handleModuleImport(node)
return .skipChildren
case .classInterface, .categoryInterface, .protocolDeclaration:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .visitChildren
case .classImplementation, .categoryImplementation:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .visitChildren
case .methodDefinition:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .typeDefinition:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .structSpecifier, .enumSpecifier, .nsEnumSpecifier:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .functionDefinition:
guard cursorRange.strictlyContains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
default:
return .skipChildren
}
}
override func visitPost(_: ASTNode) {}
// MARK: Imports
func handlePreprocInclude(_ node: ASTNode) {
if let pathNode = node.child(byFieldName: "path") {
let path = textProvider(pathNode)
if !path.isEmpty {
includes.append(path)
}
}
}
func handlePreprocImport(_ node: ASTNode) {
if let pathNode = node.child(byFieldName: "path") {
let path = textProvider(pathNode)
if !path.isEmpty {
imports.append(path)
}
}
}
func handleModuleImport(_ node: ASTNode) {
if let pathNode = node.child(byFieldName: "module") {
let path = textProvider(pathNode)
if !path.isEmpty {
imports.append(path)
}
}
}
}