|
| 1 | +import Foundation |
| 2 | +import SuggestionModel |
| 3 | + |
| 4 | +public struct ActiveDocumentContext { |
| 5 | + public var filePath: String |
| 6 | + public var relativePath: String |
| 7 | + public var language: CodeLanguage |
| 8 | + public var fileContent: String |
| 9 | + public var lines: [String] |
| 10 | + public var selectedCode: String |
| 11 | + public var selectionRange: CursorRange |
| 12 | + public var lineAnnotations: [EditorInformation.LineAnnotation] |
| 13 | + public var imports: [String] |
| 14 | + |
| 15 | + public struct FocusedContext { |
| 16 | + public var context: [String] |
| 17 | + public var contextRange: CursorRange |
| 18 | + public var codeRange: CursorRange |
| 19 | + public var code: String |
| 20 | + public var lineAnnotations: [EditorInformation.LineAnnotation] |
| 21 | + public var otherLineAnnotations: [EditorInformation.LineAnnotation] |
| 22 | + |
| 23 | + public init( |
| 24 | + context: [String], |
| 25 | + contextRange: CursorRange, |
| 26 | + codeRange: CursorRange, |
| 27 | + code: String, |
| 28 | + lineAnnotations: [EditorInformation.LineAnnotation], |
| 29 | + otherLineAnnotations: [EditorInformation.LineAnnotation] |
| 30 | + ) { |
| 31 | + self.context = context |
| 32 | + self.contextRange = contextRange |
| 33 | + self.codeRange = codeRange |
| 34 | + self.code = code |
| 35 | + self.lineAnnotations = lineAnnotations |
| 36 | + self.otherLineAnnotations = otherLineAnnotations |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public var focusedContext: FocusedContext? |
| 41 | + |
| 42 | + public init( |
| 43 | + filePath: String, |
| 44 | + relativePath: String, |
| 45 | + language: CodeLanguage, |
| 46 | + fileContent: String, |
| 47 | + lines: [String], |
| 48 | + selectedCode: String, |
| 49 | + selectionRange: CursorRange, |
| 50 | + lineAnnotations: [EditorInformation.LineAnnotation], |
| 51 | + imports: [String], |
| 52 | + focusedContext: FocusedContext? = nil |
| 53 | + ) { |
| 54 | + self.filePath = filePath |
| 55 | + self.relativePath = relativePath |
| 56 | + self.language = language |
| 57 | + self.fileContent = fileContent |
| 58 | + self.lines = lines |
| 59 | + self.selectedCode = selectedCode |
| 60 | + self.selectionRange = selectionRange |
| 61 | + self.lineAnnotations = lineAnnotations |
| 62 | + self.imports = imports |
| 63 | + self.focusedContext = focusedContext |
| 64 | + } |
| 65 | + |
| 66 | + public mutating func moveToFocusedCode() { |
| 67 | + moveToCodeContainingRange(selectionRange) |
| 68 | + } |
| 69 | + |
| 70 | + public mutating func moveToCodeAroundLine(_ line: Int) { |
| 71 | + moveToCodeContainingRange(.init( |
| 72 | + start: .init(line: line, character: 0), |
| 73 | + end: .init(line: line, character: 0) |
| 74 | + )) |
| 75 | + } |
| 76 | + |
| 77 | + public mutating func expandFocusedRangeToContextRange() { |
| 78 | + guard let focusedContext else { return } |
| 79 | + moveToCodeContainingRange(focusedContext.contextRange) |
| 80 | + } |
| 81 | + |
| 82 | + public mutating func moveToCodeContainingRange(_ range: CursorRange) { |
| 83 | + let finder: FocusedCodeFinder = { |
| 84 | + switch language { |
| 85 | + case .builtIn(.swift): |
| 86 | + return SwiftFocusedCodeFinder() |
| 87 | + default: |
| 88 | + return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 5) |
| 89 | + } |
| 90 | + }() |
| 91 | + |
| 92 | + let codeContext = finder.findFocusedCode( |
| 93 | + containingRange: range, |
| 94 | + activeDocumentContext: self |
| 95 | + ) |
| 96 | + |
| 97 | + imports = codeContext.imports |
| 98 | + |
| 99 | + let startLine = codeContext.focusedRange.start.line |
| 100 | + let endLine = codeContext.focusedRange.end.line |
| 101 | + var matchedAnnotations = [EditorInformation.LineAnnotation]() |
| 102 | + var otherAnnotations = [EditorInformation.LineAnnotation]() |
| 103 | + for annotation in lineAnnotations { |
| 104 | + if annotation.line >= startLine, annotation.line <= endLine { |
| 105 | + matchedAnnotations.append(annotation) |
| 106 | + } else { |
| 107 | + otherAnnotations.append(annotation) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + focusedContext = .init( |
| 112 | + context: codeContext.scopeSignatures, |
| 113 | + contextRange: codeContext.contextRange, |
| 114 | + codeRange: codeContext.focusedRange, |
| 115 | + code: codeContext.focusedCode, |
| 116 | + lineAnnotations: matchedAnnotations, |
| 117 | + otherLineAnnotations: otherAnnotations |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + public mutating func update(_ info: EditorInformation) { |
| 122 | + /// Whenever the file content, relative path, or selection range changes, |
| 123 | + /// we should reset the context. |
| 124 | + let changed: Bool = { |
| 125 | + if info.relativePath != relativePath { return true } |
| 126 | + if info.editorContent?.content != fileContent { return true } |
| 127 | + if let range = info.editorContent?.selections.first, |
| 128 | + range != selectionRange { return true } |
| 129 | + return false |
| 130 | + }() |
| 131 | + |
| 132 | + filePath = info.documentURL.path |
| 133 | + relativePath = info.relativePath |
| 134 | + language = info.language |
| 135 | + fileContent = info.editorContent?.content ?? "" |
| 136 | + lines = info.editorContent?.lines ?? [] |
| 137 | + selectedCode = info.selectedContent |
| 138 | + selectionRange = info.editorContent?.selections.first ?? .zero |
| 139 | + lineAnnotations = info.editorContent?.lineAnnotations ?? [] |
| 140 | + imports = [] |
| 141 | + |
| 142 | + if changed { |
| 143 | + moveToFocusedCode() |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments