forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModificationState.swift
More file actions
87 lines (79 loc) · 2.68 KB
/
ModificationState.swift
File metadata and controls
87 lines (79 loc) · 2.68 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
import ChatBasic
import Foundation
import IdentifiedCollections
import SuggestionBasic
public struct ModificationState {
public typealias Source = ModificationAgentRequest.ModificationSource
public var source: Source
public var history: [ModificationHistoryNode] = []
public var snippets: IdentifiedArrayOf<ModificationSnippet> = []
public var isGenerating: Bool = false
public var extraSystemPrompt: String
public var isAttachedToTarget: Bool = true
public var status = [String]()
public var references: [ChatMessage.Reference] = []
public init(
source: Source,
history: [ModificationHistoryNode] = [],
snippets: IdentifiedArrayOf<ModificationSnippet>,
extraSystemPrompt: String,
isAttachedToTarget: Bool,
isGenerating: Bool = false,
status: [String] = [],
references: [ChatMessage.Reference] = []
) {
self.history = history
self.snippets = snippets
self.isGenerating = isGenerating
self.isAttachedToTarget = isAttachedToTarget
self.extraSystemPrompt = extraSystemPrompt
self.source = source
self.status = status
self.references = references
}
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
),
],
extraSystemPrompt: extraSystemPrompt,
isAttachedToTarget: !attachedRange.isEmpty
)
}
public mutating func popHistory() -> NSAttributedString? {
if !history.isEmpty {
let last = history.removeLast()
references = last.references
snippets = last.snippets
let instruction = last.instruction
return instruction
}
return nil
}
public mutating func pushHistory(instruction: NSAttributedString) {
history.append(.init(snippets: snippets, instruction: instruction, references: references))
let oldSnippets = snippets
snippets = IdentifiedArrayOf()
for var snippet in oldSnippets {
snippet.originalCode = snippet.modifiedCode
snippet.modifiedCode = ""
snippet.description = ""
snippet.error = nil
snippets.append(snippet)
}
}
}