forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnownLanguageFocusedCodeFinder.swift
More file actions
214 lines (186 loc) · 7.1 KB
/
KnownLanguageFocusedCodeFinder.swift
File metadata and controls
214 lines (186 loc) · 7.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import Foundation
import Preferences
import SuggestionBasic
public typealias KnownLanguageFocusedCodeFinder<Tree, Node, TextPosition> =
BaseKnownLanguageFocusedCodeFinder<Tree, Node, TextPosition> &
KnownLanguageFocusedCodeFinderType
public class BaseKnownLanguageFocusedCodeFinder<Tree, Node, TextPosition> {
public typealias TextProvider = (TextPosition) -> String
public typealias RangeConverter = (Node) -> CursorRange
public struct NodeInfo {
var node: Node
var signature: String
var name: String
var canBeUsedAsCodeRange: Bool = true
}
public struct ContextInfo {
var nodes: [Node]
var includes: [String]
var imports: [String]
}
public let maxFocusedCodeLineCount: Int
init(
maxFocusedCodeLineCount: Int = UserDefaults.shared.value(for: \.maxFocusedCodeLineCount)
) {
self.maxFocusedCodeLineCount = maxFocusedCodeLineCount
}
}
public protocol KnownLanguageFocusedCodeFinderType: FocusedCodeFinderType {
associatedtype Tree
associatedtype Node
associatedtype TextPosition
typealias Document = FocusedCodeFinder.Document
typealias Finder = BaseKnownLanguageFocusedCodeFinder<Tree, Node, TextPosition>
typealias NodeInfo = Finder.NodeInfo
typealias ContextInfo = Finder.ContextInfo
typealias TextProvider = Finder.TextProvider
typealias RangeConverter = Finder.RangeConverter
var maxFocusedCodeLineCount: Int { get }
func parseSyntaxTree(from document: Document) -> Tree?
func collectContextNodes(
in document: Document,
tree: Tree,
containingRange: SuggestionBasic.CursorRange,
textProvider: @escaping TextProvider,
rangeConverter: @escaping RangeConverter
) -> ContextInfo
func contextContainingNode(
_ node: Node,
textProvider: @escaping TextProvider,
rangeConverter: @escaping RangeConverter
) -> NodeInfo?
func createTextProviderAndRangeConverter(
for document: Document,
tree: Tree
) -> (TextProvider, RangeConverter)
}
public extension KnownLanguageFocusedCodeFinderType {
func findFocusedCode(
in document: Document,
containingRange range: SuggestionBasic.CursorRange
) -> CodeContext {
guard let tree = parseSyntaxTree(from: document) else { return .empty }
let (textProvider, rangeConverter) = createTextProviderAndRangeConverter(
for: document,
tree: tree
)
var contextInfo = collectContextNodes(
in: document,
tree: tree,
containingRange: range,
textProvider: textProvider,
rangeConverter: rangeConverter
)
var codeRange: CursorRange
let noSelection = range.isEmpty
if noSelection {
// use the first scope as code, the second as context
var focusedNode: Node?
while let node = contextInfo.nodes.first {
contextInfo.nodes.removeFirst()
let nodeInfo = contextContainingNode(
node,
textProvider: textProvider,
rangeConverter: rangeConverter
)
if nodeInfo?.canBeUsedAsCodeRange ?? false {
focusedNode = node
break
}
}
guard let focusedNode else {
// fallback to unknown language focused code finder when no scope found
var result = UnknownLanguageFocusedCodeFinder(proposedSearchRange: 8)
.findFocusedCode(in: document, containingRange: range)
result.imports = contextInfo.imports
result.includes = contextInfo.includes
return result
}
codeRange = rangeConverter(focusedNode)
} else {
// use the selection as code, the first scope as context
codeRange = range
}
let (code, _, focusedRange) = extractFocusedCode(
in: codeRange,
in: document,
containingRange: range
)
let (contextRange, scopeContexts) = extractScopeContext(
contextNodes: contextInfo.nodes,
textProvider: textProvider,
rangeConverter: rangeConverter
)
return .init(
scope: scopeContexts.isEmpty ? .file : .scope(signature: scopeContexts),
contextRange: contextRange,
smallestContextRange: codeRange,
focusedRange: focusedRange,
focusedCode: code,
imports: contextInfo.imports,
includes: contextInfo.includes
)
}
}
extension KnownLanguageFocusedCodeFinderType {
func extractFocusedCode(
in codeRange: CursorRange,
in document: Document,
containingRange range: SuggestionBasic.CursorRange
) -> (code: String, lines: [String], codeRange: CursorRange) {
var codeRange = codeRange
let codeInCodeRange = EditorInformation.code(
in: document.lines,
inside: codeRange,
ignoreColumns: true
)
var code = codeInCodeRange.code
var lines = codeInCodeRange.lines
if range.isEmpty, codeInCodeRange.lines.count > maxFocusedCodeLineCount {
// if the focused code is too long, truncate it to be shorter
let centerLine = range.start.line
let relativeCenterLine = centerLine - codeRange.start.line
let startLine = max(0, relativeCenterLine - maxFocusedCodeLineCount / 2)
let endLine = max(
startLine,
min(codeInCodeRange.lines.count - 1, startLine + maxFocusedCodeLineCount - 1)
)
lines = Array(codeInCodeRange.lines[startLine...endLine])
code = lines.joined()
codeRange = .init(
start: .init(line: startLine + codeRange.start.line, character: 0),
end: .init(
line: endLine + codeRange.start.line,
character: codeInCodeRange.lines[endLine].count
)
)
}
return (code, lines, codeRange)
}
func extractScopeContext(
contextNodes: [Node],
textProvider: @escaping TextProvider,
rangeConverter: @escaping RangeConverter
) -> (contextRange: CursorRange, scopeContexts: [CodeContext.ScopeContext]) {
var nodes = contextNodes
var contextRange = CursorRange.zero
var signature = [CodeContext.ScopeContext]()
while let node = nodes.first {
nodes.removeFirst()
let context = contextContainingNode(
node,
textProvider: textProvider,
rangeConverter: rangeConverter
)
if let context {
contextRange = rangeConverter(context.node)
signature.insert(.init(
signature: context.signature,
name: context.name,
range: contextRange
), at: 0)
}
}
return (contextRange, signature)
}
}