Skip to content

Commit ae9308f

Browse files
committed
Adjust SuggestionService to allow changing services
1 parent c16e7ef commit ae9308f

File tree

5 files changed

+188
-26
lines changed

5 files changed

+188
-26
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public enum SuggestionServiceProviderType: Int {
4+
case gitHubCopilot
5+
case codeium
6+
}

Core/Sources/Service/Workspace.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ extension Workspace {
257257
tabSize: editor.tabSize,
258258
indentSize: editor.indentSize,
259259
usesTabsForIndentation: editor.usesTabsForIndentation,
260-
ignoreSpaceOnlySuggestions: true,
261-
referenceFileURLs: filespaces.values.map(\.fileURL)
260+
ignoreSpaceOnlySuggestions: true
262261
)
263262

264263
filespace.suggestions = completions
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import CodeiumService
2+
import Foundation
3+
import Preferences
4+
import SuggestionModel
5+
6+
final class CodeiumSuggestionProvider: SuggestionServiceProvider {
7+
let projectRootURL: URL
8+
let onServiceLaunched: (SuggestionServiceType) -> Void
9+
var codeiumService: CodeiumSuggestionServiceType?
10+
11+
init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
12+
self.projectRootURL = projectRootURL
13+
self.onServiceLaunched = onServiceLaunched
14+
}
15+
16+
func createCodeiumServiceIfNeeded() throws -> CodeiumSuggestionServiceType {
17+
if let codeiumService { return codeiumService }
18+
let newService = try CodeiumSuggestionService(
19+
projectRootURL: projectRootURL,
20+
onServiceLaunched: { [weak self] in
21+
if let self { self.onServiceLaunched(self) }
22+
}
23+
)
24+
codeiumService = newService
25+
26+
return newService
27+
}
28+
}
29+
30+
extension CodeiumSuggestionProvider {
31+
func getSuggestions(
32+
fileURL: URL,
33+
content: String,
34+
cursorPosition: SuggestionModel.CursorPosition,
35+
tabSize: Int,
36+
indentSize: Int,
37+
usesTabsForIndentation: Bool,
38+
ignoreSpaceOnlySuggestions: Bool
39+
) async throws -> [SuggestionModel.CodeSuggestion] {
40+
try await (try createCodeiumServiceIfNeeded()).getCompletions(
41+
fileURL: fileURL,
42+
content: content,
43+
cursorPosition: cursorPosition,
44+
tabSize: tabSize,
45+
indentSize: indentSize,
46+
usesTabsForIndentation: usesTabsForIndentation,
47+
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
48+
)
49+
}
50+
51+
func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
52+
await (try? createCodeiumServiceIfNeeded())?.notifyAccepted(suggestion)
53+
}
54+
55+
func notifyRejected(_: [SuggestionModel.CodeSuggestion]) async {}
56+
57+
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
58+
try await (try? createCodeiumServiceIfNeeded())?
59+
.notifyOpenTextDocument(fileURL: fileURL, content: content)
60+
}
61+
62+
func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
63+
try await (try? createCodeiumServiceIfNeeded())?
64+
.notifyChangeTextDocument(fileURL: fileURL, content: content)
65+
}
66+
67+
func notifyCloseTextDocument(fileURL: URL) async throws {
68+
try await (try? createCodeiumServiceIfNeeded())?
69+
.notifyCloseTextDocument(fileURL: fileURL)
70+
}
71+
72+
func notifySaveTextDocument(fileURL: URL) async throws {}
73+
}
74+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import Foundation
2+
import GitHubCopilotService
3+
import Preferences
4+
import SuggestionModel
5+
6+
final class GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
7+
let projectRootURL: URL
8+
let onServiceLaunched: (SuggestionServiceType) -> Void
9+
var gitHubCopilotService: GitHubCopilotSuggestionServiceType?
10+
11+
init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
12+
self.projectRootURL = projectRootURL
13+
self.onServiceLaunched = onServiceLaunched
14+
}
15+
16+
func createGitHubCopilotServiceIfNeeded() throws -> GitHubCopilotSuggestionServiceType {
17+
if let gitHubCopilotService { return gitHubCopilotService }
18+
let newService = try GitHubCopilotSuggestionService(projectRootURL: projectRootURL)
19+
gitHubCopilotService = newService
20+
Task {
21+
try await Task.sleep(nanoseconds: 1_000_000_000)
22+
onServiceLaunched(self)
23+
}
24+
return newService
25+
}
26+
}
27+
28+
extension GitHubCopilotSuggestionProvider {
29+
func getSuggestions(
30+
fileURL: URL,
31+
content: String,
32+
cursorPosition: SuggestionModel.CursorPosition,
33+
tabSize: Int,
34+
indentSize: Int,
35+
usesTabsForIndentation: Bool,
36+
ignoreSpaceOnlySuggestions: Bool
37+
) async throws -> [SuggestionModel.CodeSuggestion] {
38+
try await (try createGitHubCopilotServiceIfNeeded()).getCompletions(
39+
fileURL: fileURL,
40+
content: content,
41+
cursorPosition: cursorPosition,
42+
tabSize: tabSize,
43+
indentSize: indentSize,
44+
usesTabsForIndentation: usesTabsForIndentation,
45+
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
46+
)
47+
}
48+
49+
func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
50+
await (try? createGitHubCopilotServiceIfNeeded())?.notifyAccepted(suggestion)
51+
}
52+
53+
func notifyRejected(_ suggestions: [SuggestionModel.CodeSuggestion]) async {
54+
await (try? createGitHubCopilotServiceIfNeeded())?.notifyRejected(suggestions)
55+
}
56+
57+
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
58+
try await (try? createGitHubCopilotServiceIfNeeded())?
59+
.notifyOpenTextDocument(fileURL: fileURL, content: content)
60+
}
61+
62+
func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
63+
try await (try? createGitHubCopilotServiceIfNeeded())?
64+
.notifyChangeTextDocument(fileURL: fileURL, content: content)
65+
}
66+
67+
func notifyCloseTextDocument(fileURL: URL) async throws {
68+
try await (try? createGitHubCopilotServiceIfNeeded())?
69+
.notifyCloseTextDocument(fileURL: fileURL)
70+
}
71+
72+
func notifySaveTextDocument(fileURL: URL) async throws {
73+
try await (try? createGitHubCopilotServiceIfNeeded())?
74+
.notifySaveTextDocument(fileURL: fileURL)
75+
}
76+
}
77+
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import CodeiumService
12
import Foundation
23
import GitHubCopilotService
4+
import Preferences
35
import SuggestionModel
46

57
public protocol SuggestionServiceType {
@@ -10,8 +12,7 @@ public protocol SuggestionServiceType {
1012
tabSize: Int,
1113
indentSize: Int,
1214
usesTabsForIndentation: Bool,
13-
ignoreSpaceOnlySuggestions: Bool,
14-
referenceFileURLs: [URL]
15+
ignoreSpaceOnlySuggestions: Bool
1516
) async throws -> [CodeSuggestion]
1617

1718
func notifyAccepted(_ suggestion: CodeSuggestion) async
@@ -22,25 +23,35 @@ public protocol SuggestionServiceType {
2223
func notifySaveTextDocument(fileURL: URL) async throws
2324
}
2425

26+
protocol SuggestionServiceProvider: SuggestionServiceType {}
27+
2528
public final class SuggestionService: SuggestionServiceType {
2629
let projectRootURL: URL
2730
let onServiceLaunched: (SuggestionServiceType) -> Void
28-
var gitHubCopilotService: GitHubCopilotSuggestionServiceType?
31+
lazy var suggestionProvider: SuggestionServiceProvider = buildService()
32+
33+
var codeiumService: CodeiumSuggestionServiceType?
34+
35+
var serviceType: SuggestionServiceProviderType { .gitHubCopilot }
2936

3037
public init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
3138
self.projectRootURL = projectRootURL
3239
self.onServiceLaunched = onServiceLaunched
3340
}
3441

35-
func createGitHubCopilotServiceIfNeeded() throws -> GitHubCopilotSuggestionServiceType {
36-
if let gitHubCopilotService { return gitHubCopilotService }
37-
let newService = try GitHubCopilotSuggestionService(projectRootURL: projectRootURL)
38-
gitHubCopilotService = newService
39-
Task {
40-
try await Task.sleep(nanoseconds: 1_000_000_000)
41-
onServiceLaunched(self)
42+
func buildService() -> SuggestionServiceProvider {
43+
switch serviceType {
44+
case .codeium:
45+
return CodeiumSuggestionProvider(
46+
projectRootURL: projectRootURL,
47+
onServiceLaunched: onServiceLaunched
48+
)
49+
case .gitHubCopilot:
50+
return GitHubCopilotSuggestionProvider(
51+
projectRootURL: projectRootURL,
52+
onServiceLaunched: onServiceLaunched
53+
)
4254
}
43-
return newService
4455
}
4556
}
4657

@@ -52,10 +63,9 @@ public extension SuggestionService {
5263
tabSize: Int,
5364
indentSize: Int,
5465
usesTabsForIndentation: Bool,
55-
ignoreSpaceOnlySuggestions: Bool,
56-
referenceFileURLs: [URL]
66+
ignoreSpaceOnlySuggestions: Bool
5767
) async throws -> [SuggestionModel.CodeSuggestion] {
58-
try await (try createGitHubCopilotServiceIfNeeded()).getCompletions(
68+
try await suggestionProvider.getSuggestions(
5969
fileURL: fileURL,
6070
content: content,
6171
cursorPosition: cursorPosition,
@@ -67,31 +77,27 @@ public extension SuggestionService {
6777
}
6878

6979
func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
70-
await (try? createGitHubCopilotServiceIfNeeded())?.notifyAccepted(suggestion)
80+
await suggestionProvider.notifyAccepted(suggestion)
7181
}
7282

7383
func notifyRejected(_ suggestions: [SuggestionModel.CodeSuggestion]) async {
74-
await (try? createGitHubCopilotServiceIfNeeded())?.notifyRejected(suggestions)
84+
await suggestionProvider.notifyRejected(suggestions)
7585
}
7686

7787
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
78-
try await (try? createGitHubCopilotServiceIfNeeded())?
79-
.notifyOpenTextDocument(fileURL: fileURL, content: content)
88+
try await suggestionProvider.notifyOpenTextDocument(fileURL: fileURL, content: content)
8089
}
8190

8291
func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
83-
try await (try? createGitHubCopilotServiceIfNeeded())?
84-
.notifyChangeTextDocument(fileURL: fileURL, content: content)
92+
try await suggestionProvider.notifyChangeTextDocument(fileURL: fileURL, content: content)
8593
}
8694

8795
func notifyCloseTextDocument(fileURL: URL) async throws {
88-
try await (try? createGitHubCopilotServiceIfNeeded())?
89-
.notifyCloseTextDocument(fileURL: fileURL)
96+
try await suggestionProvider.notifyCloseTextDocument(fileURL: fileURL)
9097
}
9198

9299
func notifySaveTextDocument(fileURL: URL) async throws {
93-
try await (try? createGitHubCopilotServiceIfNeeded())?
94-
.notifySaveTextDocument(fileURL: fileURL)
100+
try await suggestionProvider.notifySaveTextDocument(fileURL: fileURL)
95101
}
96102
}
97103

0 commit comments

Comments
 (0)