|
1 | 1 | import ASTParser |
2 | 2 | import ChatContextCollector |
| 3 | +import FocusedCodeFinder |
3 | 4 | import Foundation |
4 | 5 | import OpenAIService |
5 | 6 | import Preferences |
@@ -188,107 +189,3 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector { |
188 | 189 | } |
189 | 190 | } |
190 | 191 |
|
191 | | -struct ActiveDocumentContext { |
192 | | - var filePath: String |
193 | | - var relativePath: String |
194 | | - var language: CodeLanguage |
195 | | - var fileContent: String |
196 | | - var lines: [String] |
197 | | - var selectedCode: String |
198 | | - var selectionRange: CursorRange |
199 | | - var lineAnnotations: [EditorInformation.LineAnnotation] |
200 | | - var imports: [String] |
201 | | - |
202 | | - struct FocusedContext { |
203 | | - var context: [String] |
204 | | - var contextRange: CursorRange |
205 | | - var codeRange: CursorRange |
206 | | - var code: String |
207 | | - var lineAnnotations: [EditorInformation.LineAnnotation] |
208 | | - var otherLineAnnotations: [EditorInformation.LineAnnotation] |
209 | | - } |
210 | | - |
211 | | - var focusedContext: FocusedContext? |
212 | | - |
213 | | - mutating func moveToFocusedCode() { |
214 | | - moveToCodeContainingRange(selectionRange) |
215 | | - } |
216 | | - |
217 | | - mutating func moveToCodeAroundLine(_ line: Int) { |
218 | | - moveToCodeContainingRange(.init( |
219 | | - start: .init(line: line, character: 0), |
220 | | - end: .init(line: line, character: 0) |
221 | | - )) |
222 | | - } |
223 | | - |
224 | | - mutating func expandFocusedRangeToContextRange() { |
225 | | - guard let focusedContext else { return } |
226 | | - moveToCodeContainingRange(focusedContext.contextRange) |
227 | | - } |
228 | | - |
229 | | - mutating func moveToCodeContainingRange(_ range: CursorRange) { |
230 | | - let finder: FocusedCodeFinder = { |
231 | | - switch language { |
232 | | - case .builtIn(.swift): |
233 | | - return SwiftFocusedCodeFinder() |
234 | | - default: |
235 | | - return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5) |
236 | | - } |
237 | | - }() |
238 | | - |
239 | | - let codeContext = finder.findFocusedCode( |
240 | | - containingRange: range, |
241 | | - activeDocumentContext: self |
242 | | - ) |
243 | | - |
244 | | - imports = codeContext.imports |
245 | | - |
246 | | - let startLine = codeContext.focusedRange.start.line |
247 | | - let endLine = codeContext.focusedRange.end.line |
248 | | - var matchedAnnotations = [EditorInformation.LineAnnotation]() |
249 | | - var otherAnnotations = [EditorInformation.LineAnnotation]() |
250 | | - for annotation in lineAnnotations { |
251 | | - if annotation.line >= startLine, annotation.line <= endLine { |
252 | | - matchedAnnotations.append(annotation) |
253 | | - } else { |
254 | | - otherAnnotations.append(annotation) |
255 | | - } |
256 | | - } |
257 | | - |
258 | | - focusedContext = .init( |
259 | | - context: codeContext.scopeSignatures, |
260 | | - contextRange: codeContext.contextRange, |
261 | | - codeRange: codeContext.focusedRange, |
262 | | - code: codeContext.focusedCode, |
263 | | - lineAnnotations: matchedAnnotations, |
264 | | - otherLineAnnotations: otherAnnotations |
265 | | - ) |
266 | | - } |
267 | | - |
268 | | - mutating func update(_ info: EditorInformation) { |
269 | | - /// Whenever the file content, relative path, or selection range changes, |
270 | | - /// we should reset the context. |
271 | | - let changed: Bool = { |
272 | | - if info.relativePath != relativePath { return true } |
273 | | - if info.editorContent?.content != fileContent { return true } |
274 | | - if let range = info.editorContent?.selections.first, |
275 | | - range != selectionRange { return true } |
276 | | - return false |
277 | | - }() |
278 | | - |
279 | | - filePath = info.documentURL.path |
280 | | - relativePath = info.relativePath |
281 | | - language = info.language |
282 | | - fileContent = info.editorContent?.content ?? "" |
283 | | - lines = info.editorContent?.lines ?? [] |
284 | | - selectedCode = info.selectedContent |
285 | | - selectionRange = info.editorContent?.selections.first ?? .zero |
286 | | - lineAnnotations = info.editorContent?.lineAnnotations ?? [] |
287 | | - imports = [] |
288 | | - |
289 | | - if changed { |
290 | | - moveToFocusedCode() |
291 | | - } |
292 | | - } |
293 | | -} |
294 | | - |
0 commit comments