|
| 1 | +import ASTParser |
| 2 | +import Foundation |
| 3 | +import OpenAIService |
| 4 | +import SuggestionModel |
| 5 | + |
| 6 | +struct GetCodeFunction: ChatGPTFunction { |
| 7 | + enum CodeType: String, Codable { |
| 8 | + case selected |
| 9 | + case focused |
| 10 | + } |
| 11 | + |
| 12 | + struct Arguments: Codable { |
| 13 | + var codeType: CodeType |
| 14 | + } |
| 15 | + |
| 16 | + struct Result: ChatGPTFunctionResult { |
| 17 | + struct Context { |
| 18 | + var parentName: String |
| 19 | + var parentType: String |
| 20 | + } |
| 21 | + |
| 22 | + var relativePath: String |
| 23 | + var code: String |
| 24 | + var range: CursorRange |
| 25 | + var context: Context |
| 26 | + var type: CodeType |
| 27 | + var language: String |
| 28 | + |
| 29 | + var botReadableContent: String { |
| 30 | + """ |
| 31 | + The \(type.rawValue) code is a part of `\(context.parentType) \(context.parentName)` \ |
| 32 | + in file \(relativePath). |
| 33 | + Range [\(range.start.line), \(range.start.character)] - \ |
| 34 | + [\(range.end.line), \(range.end.character)] |
| 35 | + ```\(language) |
| 36 | + \(code) |
| 37 | + ``` |
| 38 | + """ |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + var reportProgress: (String) async -> Void = { _ in } |
| 43 | + |
| 44 | + var name: String { |
| 45 | + "getCode" |
| 46 | + } |
| 47 | + |
| 48 | + var description: String { |
| 49 | + "Get selected or focused code from the active document." |
| 50 | + } |
| 51 | + |
| 52 | + var argumentSchema: JSONSchemaValue { [ |
| 53 | + .type: "object", |
| 54 | + .properties: [:], |
| 55 | + ] } |
| 56 | + |
| 57 | + func prepare() async { |
| 58 | + await reportProgress("Reading code..") |
| 59 | + } |
| 60 | + |
| 61 | + func call(arguments: Arguments) async throws -> Result { |
| 62 | + await reportProgress("Reading code..") |
| 63 | + let content = getEditorInformation() |
| 64 | + let selectionRange = content.editorContent?.selections.first ?? .outOfScope |
| 65 | + let editorContent = { |
| 66 | + if selectionRange.start == selectionRange.end { |
| 67 | + return content.editorContent?.content ?? "" |
| 68 | + } else { |
| 69 | + return content.selectedContent |
| 70 | + } |
| 71 | + }() |
| 72 | + |
| 73 | + let language = content.language.rawValue |
| 74 | + let type = CodeType.selected |
| 75 | + let relativePath = content.documentURL.path |
| 76 | + .replacingOccurrences(of: content.projectURL.path, with: "") |
| 77 | + let context = Result.Context( |
| 78 | + parentName: content.documentURL.lastPathComponent, |
| 79 | + parentType: "File" |
| 80 | + ) |
| 81 | + let range = selectionRange |
| 82 | + |
| 83 | + await reportProgress("Finish reading code..") |
| 84 | + return .init( |
| 85 | + relativePath: relativePath, |
| 86 | + code: editorContent, |
| 87 | + range: range, |
| 88 | + context: context, |
| 89 | + type: type, |
| 90 | + language: language |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +struct GetCodeResultParser { |
| 96 | + let editorInformation: EditorInformation |
| 97 | + |
| 98 | + func parse() -> GetCodeFunction.Result { |
| 99 | + let language = editorInformation.language.rawValue |
| 100 | + let relativePath = editorInformation.relativePath |
| 101 | + let selectionRange = editorInformation.editorContent?.selections.first |
| 102 | + |
| 103 | + if let selectionRange, let node = findSmallestScopeContainingRange(selectionRange) { |
| 104 | + let code = { |
| 105 | + if editorInformation.selectedContent.isEmpty { |
| 106 | + return editorInformation.selectedLines.first ?? "" |
| 107 | + } |
| 108 | + return editorInformation.selectedContent |
| 109 | + }() |
| 110 | + |
| 111 | + return .init( |
| 112 | + relativePath: relativePath, |
| 113 | + code: code, |
| 114 | + range: selectionRange, |
| 115 | + context: .init(parentName: "", parentType: ""), |
| 116 | + type: .selected, |
| 117 | + language: language |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + return .init( |
| 122 | + relativePath: relativePath, |
| 123 | + code: "", |
| 124 | + range: selectionRange ?? .zero, |
| 125 | + context: .init(parentName: "", parentType: ""), |
| 126 | + type: .focused, |
| 127 | + language: language |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + func findSmallestScopeContainingRange(_ range: CursorRange) -> ASTNode? { |
| 132 | + guard let language = { |
| 133 | + switch editorInformation.language { |
| 134 | + case .builtIn(.swift): |
| 135 | + return ParsableLanguage.swift |
| 136 | + case .builtIn(.objc), .builtIn(.objcpp): |
| 137 | + return ParsableLanguage.objectiveC |
| 138 | + default: |
| 139 | + return nil |
| 140 | + } |
| 141 | + }() else { return nil } |
| 142 | + |
| 143 | + let parser = ASTParser(language: language) |
| 144 | + guard let tree = parser.parse(editorInformation.editorContent?.content ?? "") |
| 145 | + else { return nil } |
| 146 | + |
| 147 | + return tree.smallestNodeContainingRange(range) { node in |
| 148 | + ScopeType.allCases.map { $0.rawValue }.contains(node.nodeType) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +enum ScopeType: String, CaseIterable { |
| 154 | + case protocolDeclaration = "protocol_declaration" |
| 155 | + case classDeclaration = "class_declaration" |
| 156 | + case functionDeclaration = "function_declaration" |
| 157 | + case propertyDeclaration = "property_declaration" |
| 158 | + case computedProperty = "computed_property" |
| 159 | +} |
| 160 | + |
0 commit comments