forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspace+SuggestionService.swift
More file actions
134 lines (110 loc) · 4.42 KB
/
Workspace+SuggestionService.swift
File metadata and controls
134 lines (110 loc) · 4.42 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
import Foundation
import SuggestionModel
import SuggestionService
import Workspace
import XPCShared
public extension Workspace {
var suggestionPlugin: SuggestionServiceWorkspacePlugin? {
plugin(for: SuggestionServiceWorkspacePlugin.self)
}
var suggestionService: SuggestionServiceType? {
suggestionPlugin?.suggestionService
}
var isSuggestionFeatureEnabled: Bool {
suggestionPlugin?.isSuggestionFeatureEnabled ?? false
}
struct SuggestionFeatureDisabledError: Error, LocalizedError {
public var errorDescription: String? {
"Suggestion feature is disabled for this project."
}
}
}
public extension Workspace {
@WorkspaceActor
@discardableResult
func generateSuggestions(
forFileAt fileURL: URL,
editor: EditorContent
) async throws -> [CodeSuggestion] {
refreshUpdateTime()
let filespace = createFilespaceIfNeeded(fileURL: fileURL)
guard !(await filespace.isGitIgnored) else { return [] }
if !editor.uti.isEmpty {
filespace.codeMetadata.uti = editor.uti
filespace.codeMetadata.tabSize = editor.tabSize
filespace.codeMetadata.indentSize = editor.indentSize
filespace.codeMetadata.usesTabsForIndentation = editor.usesTabsForIndentation
}
let snapshot = FilespaceSuggestionSnapshot(
linesHash: editor.lines.hashValue,
cursorPosition: editor.cursorPosition
)
filespace.suggestionSourceSnapshot = snapshot
guard let suggestionService else { throw SuggestionFeatureDisabledError() }
let completions = try await suggestionService.getSuggestions(
fileURL: fileURL,
content: editor.lines.joined(separator: ""),
cursorPosition: editor.cursorPosition,
tabSize: editor.tabSize,
indentSize: editor.indentSize,
usesTabsForIndentation: editor.usesTabsForIndentation,
ignoreSpaceOnlySuggestions: true
)
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 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 ?? [])
}
filespaces[fileURL]?.reset()
}
@WorkspaceActor
func acceptSuggestion(forFileAt fileURL: URL, editor: EditorContent?) -> 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)
Task { [allSuggestions] in
await suggestionService?.notifyAccepted(suggestion)
await suggestionService?.notifyRejected(allSuggestions)
}
filespaces[fileURL]?.reset()
filespaces[fileURL]?.resetSnapshot()
return suggestion
}
}