forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubCopilotSuggestionService.swift
More file actions
107 lines (97 loc) · 3.54 KB
/
GitHubCopilotSuggestionService.swift
File metadata and controls
107 lines (97 loc) · 3.54 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
import CopilotForXcodeKit
import Foundation
import SuggestionBasic
import Workspace
public final class GitHubCopilotSuggestionService: SuggestionServiceType {
public var configuration: SuggestionServiceConfiguration {
.init(
acceptsRelevantCodeSnippets: true,
mixRelevantCodeSnippetsInSource: true,
acceptsRelevantSnippetsFromOpenedFiles: false
)
}
let serviceLocator: ServiceLocatorType
init(serviceLocator: ServiceLocatorType) {
self.serviceLocator = serviceLocator
}
public func getSuggestions(
_ request: SuggestionRequest,
workspace: WorkspaceInfo
) async throws -> [CopilotForXcodeKit.CodeSuggestion] {
guard let service = await serviceLocator.getService(from: workspace) else { return [] }
return try await service.getCompletions(
fileURL: request.fileURL,
content: request.content,
originalContent: request.originalContent,
cursorPosition: .init(
line: request.cursorPosition.line,
character: request.cursorPosition.character
),
tabSize: request.tabSize,
indentSize: request.indentSize,
usesTabsForIndentation: request.usesTabsForIndentation
).map(Self.convert)
}
public func notifyAccepted(
_ suggestion: CopilotForXcodeKit.CodeSuggestion,
workspace: WorkspaceInfo
) async {
guard let service = await serviceLocator.getService(from: workspace) else { return }
await service.notifyAccepted(Self.convert(suggestion))
}
public func notifyRejected(
_ suggestions: [CopilotForXcodeKit.CodeSuggestion],
workspace: WorkspaceInfo
) async {
guard let service = await serviceLocator.getService(from: workspace) else { return }
await service.notifyRejected(suggestions.map(Self.convert))
}
public func cancelRequest(workspace: WorkspaceInfo) async {
guard let service = await serviceLocator.getService(from: workspace) else { return }
await service.cancelRequest()
}
static func convert(
_ suggestion: SuggestionBasic.CodeSuggestion
) -> CopilotForXcodeKit.CodeSuggestion {
.init(
id: suggestion.id,
text: suggestion.text,
position: .init(
line: suggestion.position.line,
character: suggestion.position.character
),
range: .init(
start: .init(
line: suggestion.range.start.line,
character: suggestion.range.start.character
),
end: .init(
line: suggestion.range.end.line,
character: suggestion.range.end.character
)
)
)
}
static func convert(
_ suggestion: CopilotForXcodeKit.CodeSuggestion
) -> SuggestionBasic.CodeSuggestion {
.init(
id: suggestion.id,
text: suggestion.text,
position: .init(
line: suggestion.position.line,
character: suggestion.position.character
),
range: .init(
start: .init(
line: suggestion.range.start.line,
character: suggestion.range.start.character
),
end: .init(
line: suggestion.range.end.line,
character: suggestion.range.end.character
)
)
)
}
}