1+ import CodeiumService
12import Foundation
23import GitHubCopilotService
4+ import Preferences
35import SuggestionModel
46
57public 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+
2528public 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