forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspace+SuggestionService.swift
More file actions
188 lines (157 loc) · 6.26 KB
/
Workspace+SuggestionService.swift
File metadata and controls
188 lines (157 loc) · 6.26 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Foundation
import GitHubCopilotService
import SuggestionBasic
import SuggestionProvider
import Workspace
import Status
import XPCShared
public extension Workspace {
var suggestionPlugin: SuggestionServiceWorkspacePlugin? {
plugin(for: SuggestionServiceWorkspacePlugin.self)
}
var suggestionService: SuggestionServiceProvider? {
suggestionPlugin?.suggestionService
}
var isSuggestionFeatureEnabled: Bool {
suggestionPlugin?.isSuggestionFeatureEnabled ?? false
}
var gitHubCopilotPlugin: GitHubCopilotWorkspacePlugin? {
plugin(for: GitHubCopilotWorkspacePlugin.self)
}
var gitHubCopilotService: GitHubCopilotService? {
gitHubCopilotPlugin?.gitHubCopilotService
}
struct SuggestionFeatureDisabledError: Error, LocalizedError {
public var errorDescription: String? {
"Suggestion feature is disabled for this project."
}
}
struct EditorCursorOutOfScopeError: Error, LocalizedError {
public var errorDescription: String? {
"Cursor position is out of scope."
}
}
}
public extension Workspace {
@WorkspaceActor
@discardableResult
func generateSuggestions(
forFileAt fileURL: URL,
editor: EditorContent
) async throws -> [CodeSuggestion] {
refreshUpdateTime()
guard editor.cursorPosition != .outOfScope else {
throw EditorCursorOutOfScopeError()
}
let filespace = try createFilespaceIfNeeded(fileURL: fileURL)
if !editor.uti.isEmpty {
filespace.codeMetadata.uti = editor.uti
filespace.codeMetadata.tabSize = editor.tabSize
filespace.codeMetadata.indentSize = editor.indentSize
filespace.codeMetadata.usesTabsForIndentation = editor.usesTabsForIndentation
}
filespace.codeMetadata.guessLineEnding(from: editor.lines.first)
let snapshot = FilespaceSuggestionSnapshot(content: editor)
filespace.suggestionSourceSnapshot = snapshot
guard let suggestionService else { throw SuggestionFeatureDisabledError() }
let content = editor.lines.joined(separator: "")
let completions = try await suggestionService.getSuggestions(
.init(
fileURL: fileURL,
relativePath: fileURL.path.replacingOccurrences(of: projectRootURL.path, with: ""),
content: content,
originalContent: content,
lines: editor.lines,
cursorPosition: editor.cursorPosition,
cursorOffset: editor.cursorOffset,
tabSize: editor.tabSize,
indentSize: editor.indentSize,
usesTabsForIndentation: editor.usesTabsForIndentation,
relevantCodeSnippets: []
),
workspaceInfo: .init(workspaceURL: workspaceURL, projectURL: projectRootURL)
)
let clsStatus = await Status.shared.getCLSStatus()
if clsStatus.isErrorStatus && clsStatus.message.contains("Completions limit reached") {
filespace.setError(clsStatus.message)
} else {
filespace.setError("")
filespace.setSuggestions(completions)
}
return completions
}
@WorkspaceActor
func selectNextSuggestion(forFileAt fileURL: URL) {
refreshUpdateTime()
guard let filespace = filespaces[fileURL],
filespace.suggestions.count > 1
else { return }
filespace.nextSuggestion()
}
@WorkspaceActor
func selectPreviousSuggestion(forFileAt fileURL: URL) {
refreshUpdateTime()
guard let filespace = filespaces[fileURL],
filespace.suggestions.count > 1
else { return }
filespace.previousSuggestion()
}
@WorkspaceActor
func notifySuggestionShown(fileFileAt fileURL: URL) {
if let suggestion = filespaces[fileURL]?.presentingSuggestion {
Task {
await gitHubCopilotService?.notifyShown(suggestion)
}
}
}
@WorkspaceActor
func rejectSuggestion(forFileAt fileURL: URL, editor: EditorContent?) {
refreshUpdateTime()
if let editor, !editor.uti.isEmpty {
filespaces[fileURL]?.codeMetadata.uti = editor.uti
filespaces[fileURL]?.codeMetadata.tabSize = editor.tabSize
filespaces[fileURL]?.codeMetadata.indentSize = editor.indentSize
filespaces[fileURL]?.codeMetadata.usesTabsForIndentation = editor.usesTabsForIndentation
}
Task {
await suggestionService?.notifyRejected(
filespaces[fileURL]?.suggestions ?? [],
workspaceInfo: .init(
workspaceURL: workspaceURL,
projectURL: projectRootURL
)
)
}
filespaces[fileURL]?.reset()
}
@WorkspaceActor
func acceptSuggestion(forFileAt fileURL: URL, editor: EditorContent?, suggestionLineLimit: Int? = nil) -> CodeSuggestion? {
refreshUpdateTime()
guard let filespace = filespaces[fileURL],
!filespace.suggestions.isEmpty,
filespace.suggestionIndex >= 0,
filespace.suggestionIndex < filespace.suggestions.endIndex
else { return nil }
if let editor, !editor.uti.isEmpty {
filespaces[fileURL]?.codeMetadata.uti = editor.uti
filespaces[fileURL]?.codeMetadata.tabSize = editor.tabSize
filespaces[fileURL]?.codeMetadata.indentSize = editor.indentSize
filespaces[fileURL]?.codeMetadata.usesTabsForIndentation = editor.usesTabsForIndentation
}
var allSuggestions = filespace.suggestions
let suggestion = allSuggestions.remove(at: filespace.suggestionIndex)
var length: Int? = nil
if let suggestionLineLimit {
let lines = suggestion.text.breakLines(
proposedLineEnding: filespaces[fileURL]?.codeMetadata.lineEnding
)
length = lines.prefix(suggestionLineLimit).joined().count
}
Task {
await gitHubCopilotService?.notifyAccepted(suggestion, acceptedLength: length)
}
filespaces[fileURL]?.reset()
filespaces[fileURL]?.resetSnapshot()
return suggestion
}
}