forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptToCodeCustomization.swift
More file actions
156 lines (140 loc) · 4.58 KB
/
PromptToCodeCustomization.swift
File metadata and controls
156 lines (140 loc) · 4.58 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
import ComposableArchitecture
import Dependencies
import Foundation
import PromptToCodeBasic
import SuggestionBasic
import SwiftUI
public enum PromptToCodeCustomization {
public static var CustomizedUI: any PromptToCodeCustomizedUI = NoPromptToCodeCustomizedUI()
}
public struct PromptToCodeCustomizationContextWrapper<Content: View>: View {
@State var context: AnyObject
let content: (AnyObject) -> Content
init<O: AnyObject>(context: O, @ViewBuilder content: @escaping (O) -> Content) {
self.context = context
self.content = { context in
content(context as! O)
}
}
public var body: some View {
content(context)
}
}
public protocol PromptToCodeCustomizedUI {
typealias PromptToCodeCustomizedViews = (
extraMenuItems: AnyView?,
extraButtons: AnyView?,
extraAcceptButtonVariants: AnyView?,
inputField: AnyView?
)
func callAsFunction<V: View>(
state: Shared<PromptToCodeState>,
isInputFieldFocused: Binding<Bool>,
@ViewBuilder view: @escaping (PromptToCodeCustomizedViews) -> V
) -> PromptToCodeCustomizationContextWrapper<V>
}
struct NoPromptToCodeCustomizedUI: PromptToCodeCustomizedUI {
private class Context {}
func callAsFunction<V: View>(
state: Shared<PromptToCodeState>,
isInputFieldFocused: Binding<Bool>,
@ViewBuilder view: @escaping (PromptToCodeCustomizedViews) -> V
) -> PromptToCodeCustomizationContextWrapper<V> {
PromptToCodeCustomizationContextWrapper(context: Context()) { _ in
view((
extraMenuItems: nil,
extraButtons: nil,
extraAcceptButtonVariants: nil,
inputField: nil
))
}
}
}
public struct PromptToCodeState: Equatable {
public struct Source: Equatable {
public var language: CodeLanguage
public var documentURL: URL
public var projectRootURL: URL
public var content: String
public var lines: [String]
public init(
language: CodeLanguage,
documentURL: URL,
projectRootURL: URL,
content: String,
lines: [String]
) {
self.language = language
self.documentURL = documentURL
self.projectRootURL = projectRootURL
self.content = content
self.lines = lines
}
}
public var source: Source
public var history: [PromptToCodeHistoryNode] = []
public var snippets: IdentifiedArrayOf<PromptToCodeSnippet> = []
public var isGenerating: Bool = false
public var instruction: String
public var extraSystemPrompt: String
public var isAttachedToTarget: Bool = true
public init(
source: Source,
history: [PromptToCodeHistoryNode] = [],
snippets: IdentifiedArrayOf<PromptToCodeSnippet>,
instruction: String,
extraSystemPrompt: String,
isAttachedToTarget: Bool
) {
self.history = history
self.snippets = snippets
isGenerating = false
self.instruction = instruction
self.isAttachedToTarget = isAttachedToTarget
self.extraSystemPrompt = extraSystemPrompt
self.source = source
}
public init(
source: Source,
originalCode: String,
attachedRange: CursorRange,
instruction: String,
extraSystemPrompt: String
) {
self.init(
source: source,
snippets: [
.init(
startLineIndex: 0,
originalCode: originalCode,
modifiedCode: originalCode,
description: "",
error: nil,
attachedRange: attachedRange
),
],
instruction: instruction,
extraSystemPrompt: extraSystemPrompt,
isAttachedToTarget: !attachedRange.isEmpty
)
}
public mutating func popHistory() {
if !history.isEmpty {
let last = history.removeLast()
snippets = last.snippets
instruction = last.instruction
}
}
public mutating func pushHistory() {
history.append(.init(snippets: snippets, instruction: instruction))
let oldSnippets = snippets
snippets = IdentifiedArrayOf()
for var snippet in oldSnippets {
snippet.originalCode = snippet.modifiedCode
snippet.modifiedCode = ""
snippet.description = ""
snippet.error = nil
snippets.append(snippet)
}
}
}