forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilespace+SuggestionService.swift
More file actions
170 lines (150 loc) · 5.55 KB
/
Filespace+SuggestionService.swift
File metadata and controls
170 lines (150 loc) · 5.55 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import Foundation
import SuggestionBasic
import SuggestionInjector
import Workspace
/// The moment when a suggestion is generated.
public struct FilespaceSuggestionSnapshot: Equatable {
public var editingLine: String
public var cursorPosition: CursorPosition
public var editingLinePrefix: String
public var editingLineSuffix: String
public static func == (
lhs: FilespaceSuggestionSnapshot,
rhs: FilespaceSuggestionSnapshot
) -> Bool {
lhs.editingLine == rhs.editingLine
&& lhs.cursorPosition == rhs.cursorPosition
}
public init(lines: [String], cursorPosition: CursorPosition) {
self.cursorPosition = cursorPosition
editingLine = if cursorPosition.line >= 0 && cursorPosition.line < lines.count {
lines[cursorPosition.line]
} else {
""
}
let col = cursorPosition.character
let view = editingLine.utf16
editingLinePrefix = if col >= 0 {
String(view.prefix(col)) ?? ""
} else {
""
}
editingLineSuffix = if col >= 0, col < editingLine.utf16.count {
String(view[view.index(view.startIndex, offsetBy: col)...]) ?? ""
} else {
""
}
}
}
public struct FilespaceSuggestionSnapshotKey: FilespacePropertyKey {
public static func createDefaultValue()
-> FilespaceSuggestionSnapshot { .init(lines: [], cursorPosition: .outOfScope) }
}
public extension FilespacePropertyValues {
/// The state of the file when a suggestion is generated.
@WorkspaceActor
var suggestionSourceSnapshot: FilespaceSuggestionSnapshot {
get { self[FilespaceSuggestionSnapshotKey.self] }
set { self[FilespaceSuggestionSnapshotKey.self] = newValue }
}
}
public extension Filespace {
@WorkspaceActor
func resetSnapshot() {
self[keyPath: \.suggestionSourceSnapshot] = FilespaceSuggestionSnapshotKey
.createDefaultValue()
}
/// Validate the suggestion is still valid.
/// - Parameters:
/// - lines: lines of the file
/// - cursorPosition: cursor position
/// - Returns: `true` if the suggestion is still valid
@WorkspaceActor
func validateSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool {
guard let presentingSuggestion else { return false }
let snapshot = self[keyPath: \.suggestionSourceSnapshot]
if snapshot.cursorPosition == .outOfScope { return false }
guard Self.validateSuggestion(
presentingSuggestion,
snapshot: snapshot,
lines: lines,
cursorPosition: cursorPosition
) else {
reset()
resetSnapshot()
return false
}
return true
}
}
extension Filespace {
static func validateSuggestion(
_ suggestion: CodeSuggestion,
snapshot: FilespaceSuggestionSnapshot,
lines: [String],
cursorPosition: CursorPosition
) -> Bool {
// cursor has moved to another line
if cursorPosition.line != suggestion.position.line { return false }
// the cursor position is valid
guard cursorPosition.line >= 0, cursorPosition.line < lines.count else { return false }
let editingLine = lines[cursorPosition.line].dropLast(1) // dropping line ending
let suggestionLines = suggestion.text.split(whereSeparator: \.isNewline)
// if the line will not change after accepting the suggestion
if Self.validateThatSuggestionMakeNoDifferent(
suggestion,
lines: lines,
suggestionLines: suggestionLines
) {
return false
}
// the line content doesn't match the suggestion snapshot
if !editingLine.hasPrefix(snapshot.editingLinePrefix) {
return false
}
return true
}
static func validateThatSuggestionMakeNoDifferent(
_ suggestion: CodeSuggestion,
lines: [String],
suggestionLines: [Substring]
) -> Bool {
var editingRange = suggestion.range
let startLine = max(0, editingRange.start.line)
let endLine = max(startLine, min(editingRange.end.line, lines.count - 1))
// The editing range is out of the file
if startLine < 0 || endLine >= lines.count {
return false
}
// The suggestion is apparently longer than the editing range
if endLine - startLine + 1 != suggestionLines.count {
return false
}
let originalEditingLines = Array(lines[startLine...endLine])
var editingLines = originalEditingLines
editingRange.end = .init(
line: editingRange.end.line - editingRange.start.line,
character: editingRange.end.character
)
editingRange.start = .init(line: 0, character: editingRange.start.character)
var cursorPosition = CursorPosition(
line: suggestion.position.line - startLine,
character: suggestion.position.character
)
let pseudoSuggestion = CodeSuggestion(
id: "",
text: suggestion.text,
position: cursorPosition,
range: editingRange
)
var extraInfo = SuggestionInjector.ExtraInfo()
let injector = SuggestionInjector()
injector.acceptSuggestion(
intoContentWithoutSuggestion: &editingLines,
cursorPosition: &cursorPosition,
completion: pseudoSuggestion,
extraInfo: &extraInfo
)
return editingLines == originalEditingLines
}
}