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
131 lines (119 loc) · 4.35 KB
/
ObjectiveCScopeHierarchySyntaxVisitor.swift
File metadata and controls
131 lines (119 loc) · 4.35 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .visitChildren
case .classImplementation, .categoryImplementation:
guard cursorRange.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .visitChildren
case .methodDefinition:
guard cursorRange.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .typeDefinition:
guard cursorRange.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .structSpecifier, .enumSpecifier, .nsEnumSpecifier:
guard cursorRange.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
case .functionDefinition:
guard cursorRange.contains(range) else { return .skipChildren }
_scopeHierarchy.append(node)
return .skipChildren
default:
return .skipChildren
}
}
override func visitPost(_: ASTNode) {}
// MARK: Imports
func handlePreprocInclude(_ node: ASTNode) {
let children = node.children
for child in children {
if let pathNode = child.child(byFieldName: "path") {
let path = textProvider(pathNode)
if !path.isEmpty {
includes.append(path.replacingOccurrences(of: "\"", with: ""))
}
break
}
}
}
func handlePreprocImport(_ node: ASTNode) {
let children = node.children
for child in children {
if let pathNode = child.child(byFieldName: "path") {
let path = textProvider(pathNode)
if !path.isEmpty {
imports.append(
path
.replacingOccurrences(of: "\"", with: "")
.replacingOccurrences(of: "<", with: "")
.replacingOccurrences(of: ">", with: "")
)
}
break
}
}
}
func handleModuleImport(_ node: ASTNode) {
let children = node.children
for child in children {
if let pathNode = child.child(byFieldName: "module") {
let path = textProvider(pathNode)
if !path.isEmpty {
imports.append(path)
}
break
}
}
}
}