Skip to content

Commit 9a34b89

Browse files
committed
Break SuggestionService into SuggestionProvider and SuggestionService in Core
1 parent 870dc52 commit 9a34b89

File tree

11 files changed

+164
-133
lines changed

11 files changed

+164
-133
lines changed

Core/Package.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let package = Package(
109109
name: "Client",
110110
dependencies: [
111111
.product(name: "XPCShared", package: "Tool"),
112-
.product(name: "SuggestionService", package: "Tool"),
112+
.product(name: "SuggestionProvider", package: "Tool"),
113113
.product(name: "SuggestionModel", package: "Tool"),
114114
.product(name: "Logger", package: "Tool"),
115115
.product(name: "Preferences", package: "Tool"),
@@ -119,12 +119,13 @@ let package = Package(
119119
name: "Service",
120120
dependencies: [
121121
"SuggestionWidget",
122+
"SuggestionService",
122123
"ChatService",
123124
"PromptToCodeService",
124125
"ServiceUpdateMigration",
125126
"ChatGPTChatTab",
126127
.product(name: "XPCShared", package: "Tool"),
127-
.product(name: "SuggestionService", package: "Tool"),
128+
.product(name: "SuggestionProvider", package: "Tool"),
128129
.product(name: "Workspace", package: "Tool"),
129130
.product(name: "UserDefaultsObserver", package: "Tool"),
130131
.product(name: "AppMonitoring", package: "Tool"),
@@ -149,7 +150,7 @@ let package = Package(
149150
"Client",
150151
"SuggestionInjector",
151152
.product(name: "XPCShared", package: "Tool"),
152-
.product(name: "SuggestionService", package: "Tool"),
153+
.product(name: "SuggestionProvider", package: "Tool"),
153154
.product(name: "SuggestionModel", package: "Tool"),
154155
.product(name: "Environment", package: "Tool"),
155156
.product(name: "Preferences", package: "Tool"),
@@ -164,7 +165,7 @@ let package = Package(
164165
"Client",
165166
"LaunchAgentManager",
166167
"PlusFeatureFlag",
167-
.product(name: "SuggestionService", package: "Tool"),
168+
.product(name: "SuggestionProvider", package: "Tool"),
168169
.product(name: "Toast", package: "Tool"),
169170
.product(name: "SharedUIComponents", package: "Tool"),
170171
.product(name: "SuggestionModel", package: "Tool"),
@@ -180,6 +181,13 @@ let package = Package(
180181

181182
// MARK: - Suggestion Service
182183

184+
.target(
185+
name: "SuggestionService",
186+
dependencies: [
187+
.product(name: "SuggestionModel", package: "Tool"),
188+
.product(name: "SuggestionProvider", package: "Tool")
189+
]
190+
),
183191
.target(
184192
name: "SuggestionInjector",
185193
dependencies: [.product(name: "SuggestionModel", package: "Tool")]
@@ -290,7 +298,7 @@ let package = Package(
290298
.target(
291299
name: "ServiceUpdateMigration",
292300
dependencies: [
293-
.product(name: "SuggestionService", package: "Tool"),
301+
.product(name: "SuggestionProvider", package: "Tool"),
294302
.product(name: "Preferences", package: "Tool"),
295303
.product(name: "Keychain", package: "Tool"),
296304
]

Core/Sources/Service/Service.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Dependencies
22
import Foundation
3+
import SuggestionService
34
import Workspace
45
import WorkspaceSuggestionService
56
import XcodeInspector
@@ -33,7 +34,11 @@ public final class Service {
3334
@Dependency(\.workspacePool) var workspacePool
3435

3536
scheduledCleaner = .init()
36-
workspacePool.registerPlugin { SuggestionServiceWorkspacePlugin(workspace: $0) }
37+
workspacePool.registerPlugin {
38+
SuggestionServiceWorkspacePlugin(workspace: $0) { projectRootURL, onLaunched in
39+
SuggestionService(projectRootURL: projectRootURL, onServiceLaunched: onLaunched)
40+
}
41+
}
3742
self.workspacePool = workspacePool
3843
globalShortcutManager = .init(guiController: guiController)
3944

Core/Sources/Service/WorkspaceExtension/Workspace+Cleanup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22
import Workspace
3-
import SuggestionService
3+
import SuggestionProvider
44
import WorkspaceSuggestionService
55

66
extension Workspace {

Tool/Sources/SuggestionService/SuggestionService.swift renamed to Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 12 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,18 @@
1-
import AppKit
21
import Foundation
32
import Preferences
43
import SuggestionModel
4+
import SuggestionProvider
55
import UserDefaultsObserver
66

7-
public struct SuggestionRequest {
8-
public var fileURL: URL
9-
public var content: String
10-
public var cursorPosition: CursorPosition
11-
public var tabSize: Int
12-
public var indentSize: Int
13-
public var usesTabsForIndentation: Bool
14-
public var ignoreSpaceOnlySuggestions: Bool
15-
16-
public init(
17-
fileURL: URL,
18-
content: String,
19-
cursorPosition: CursorPosition,
20-
tabSize: Int,
21-
indentSize: Int,
22-
usesTabsForIndentation: Bool,
23-
ignoreSpaceOnlySuggestions: Bool
24-
) {
25-
self.fileURL = fileURL
26-
self.content = content
27-
self.cursorPosition = cursorPosition
28-
self.tabSize = tabSize
29-
self.indentSize = indentSize
30-
self.usesTabsForIndentation = usesTabsForIndentation
31-
self.ignoreSpaceOnlySuggestions = ignoreSpaceOnlySuggestions
32-
}
33-
}
34-
35-
public protocol SuggestionServiceType {
36-
func getSuggestions(_ request: SuggestionRequest) async throws -> [CodeSuggestion]
37-
38-
func notifyAccepted(_ suggestion: CodeSuggestion) async
39-
func notifyRejected(_ suggestions: [CodeSuggestion]) async
40-
func notifyOpenTextDocument(fileURL: URL, content: String) async throws
41-
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
42-
func notifyCloseTextDocument(fileURL: URL) async throws
43-
func notifySaveTextDocument(fileURL: URL) async throws
44-
func cancelRequest() async
45-
func terminate() async
46-
}
47-
48-
public extension SuggestionServiceType {
49-
func getSuggestions(
50-
fileURL: URL,
51-
content: String,
52-
cursorPosition: CursorPosition,
53-
tabSize: Int,
54-
indentSize: Int,
55-
usesTabsForIndentation: Bool,
56-
ignoreSpaceOnlySuggestions: Bool
57-
) async throws -> [CodeSuggestion] {
58-
return try await getSuggestions(.init(
59-
fileURL: fileURL,
60-
content: content,
61-
cursorPosition: cursorPosition,
62-
tabSize: tabSize,
63-
indentSize: indentSize,
64-
usesTabsForIndentation: usesTabsForIndentation,
65-
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
66-
))
67-
}
68-
}
69-
70-
protocol SuggestionServiceProvider: SuggestionServiceType {}
7+
public protocol SuggestionServiceType: SuggestionServiceProvider {}
718

729
public actor SuggestionService: SuggestionServiceType {
73-
static var builtInMiddlewares: [SuggestionServiceMiddleware] = [
74-
DisabledLanguageSuggestionServiceMiddleware(),
75-
]
76-
77-
static var customMiddlewares: [SuggestionServiceMiddleware] = []
78-
79-
static var middlewares: [SuggestionServiceMiddleware] {
80-
builtInMiddlewares + customMiddlewares
81-
}
82-
83-
public static func addMiddleware(_ middleware: SuggestionServiceMiddleware) {
84-
customMiddlewares.append(middleware)
10+
var middlewares: [SuggestionServiceMiddleware] {
11+
SuggestionServiceMiddlewareContainer.middlewares
8512
}
8613

8714
let projectRootURL: URL
88-
let onServiceLaunched: (SuggestionServiceType) -> Void
15+
let onServiceLaunched: (SuggestionServiceProvider) -> Void
8916
let providerChangeObserver = UserDefaultsObserver(
9017
object: UserDefaults.shared,
9118
forKeyPaths: [UserDefaultPreferenceKeys().suggestionFeatureProvider.key],
@@ -98,7 +25,10 @@ public actor SuggestionService: SuggestionServiceType {
9825
UserDefaults.shared.value(for: \.suggestionFeatureProvider)
9926
}
10027

101-
public init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
28+
public init(
29+
projectRootURL: URL,
30+
onServiceLaunched: @escaping (SuggestionServiceProvider) -> Void
31+
) {
10232
self.projectRootURL = projectRootURL
10333
self.onServiceLaunched = onServiceLaunched
10434

@@ -131,12 +61,12 @@ public actor SuggestionService: SuggestionServiceType {
13161
}
13262

13363
public extension SuggestionService {
134-
func getSuggestions(
64+
func getSuggestions(
13565
_ request: SuggestionRequest
13666
) async throws -> [SuggestionModel.CodeSuggestion] {
13767
var getSuggestion = suggestionProvider.getSuggestions
138-
139-
for middleware in Self.middlewares.reversed() {
68+
69+
for middleware in middlewares.reversed() {
14070
getSuggestion = { [getSuggestion] request in
14171
try await middleware.getSuggestion(request, next: getSuggestion)
14272
}

Tool/Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ let package = Package(
2929
.library(name: "UserDefaultsObserver", targets: ["UserDefaultsObserver"]),
3030
.library(name: "Workspace", targets: ["Workspace", "WorkspaceSuggestionService"]),
3131
.library(
32-
name: "SuggestionService",
33-
targets: ["SuggestionService", "GitHubCopilotService", "CodeiumService"]
32+
name: "SuggestionProvider",
33+
targets: ["SuggestionProvider", "GitHubCopilotService", "CodeiumService"]
3434
),
3535
.library(
3636
name: "AppMonitoring",
@@ -217,7 +217,7 @@ let package = Package(
217217
name: "WorkspaceSuggestionService",
218218
dependencies: [
219219
"Workspace",
220-
"SuggestionService",
220+
"SuggestionProvider",
221221
"XPCShared",
222222
]
223223
),
@@ -264,7 +264,7 @@ let package = Package(
264264

265265
.target(name: "BingSearchService"),
266266

267-
.target(name: "SuggestionService", dependencies: [
267+
.target(name: "SuggestionProvider", dependencies: [
268268
"GitHubCopilotService",
269269
"CodeiumService",
270270
"UserDefaultsObserver",

Tool/Sources/SuggestionService/CodeiumSuggestionProvider.swift renamed to Tool/Sources/SuggestionProvider/CodeiumSuggestionProvider.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import Foundation
33
import Preferences
44
import SuggestionModel
55

6-
actor CodeiumSuggestionProvider: SuggestionServiceProvider {
6+
public actor CodeiumSuggestionProvider: SuggestionServiceProvider {
77
let projectRootURL: URL
8-
let onServiceLaunched: (SuggestionServiceType) -> Void
8+
let onServiceLaunched: (SuggestionServiceProvider) -> Void
99
var codeiumService: CodeiumSuggestionServiceType?
1010

11-
init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
11+
public init(
12+
projectRootURL: URL,
13+
onServiceLaunched: @escaping (SuggestionServiceProvider) -> Void
14+
) {
1215
self.projectRootURL = projectRootURL
1316
self.onServiceLaunched = onServiceLaunched
1417
}
@@ -27,7 +30,7 @@ actor CodeiumSuggestionProvider: SuggestionServiceProvider {
2730
}
2831
}
2932

30-
extension CodeiumSuggestionProvider {
33+
public extension CodeiumSuggestionProvider {
3134
func getSuggestions(_ request: SuggestionRequest) async throws
3235
-> [SuggestionModel.CodeSuggestion]
3336
{

Tool/Sources/SuggestionService/GitHubCopilotSuggestionProvider.swift renamed to Tool/Sources/SuggestionProvider/GitHubCopilotSuggestionProvider.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import GitHubCopilotService
33
import Preferences
44
import SuggestionModel
55

6-
actor GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
6+
public actor GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
77
let projectRootURL: URL
8-
let onServiceLaunched: (SuggestionServiceType) -> Void
8+
let onServiceLaunched: (SuggestionServiceProvider) -> Void
99
var gitHubCopilotService: GitHubCopilotSuggestionServiceType?
1010

11-
init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceType) -> Void) {
11+
public init(projectRootURL: URL, onServiceLaunched: @escaping (SuggestionServiceProvider) -> Void) {
1212
self.projectRootURL = projectRootURL
1313
self.onServiceLaunched = onServiceLaunched
1414
}
@@ -25,7 +25,7 @@ actor GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
2525
}
2626
}
2727

28-
extension GitHubCopilotSuggestionProvider {
28+
public extension GitHubCopilotSuggestionProvider {
2929
func getSuggestions(_ request: SuggestionRequest) async throws
3030
-> [SuggestionModel.CodeSuggestion]
3131
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import AppKit
2+
import Foundation
3+
import Preferences
4+
import SuggestionModel
5+
import UserDefaultsObserver
6+
7+
public struct SuggestionRequest {
8+
public var fileURL: URL
9+
public var content: String
10+
public var cursorPosition: CursorPosition
11+
public var tabSize: Int
12+
public var indentSize: Int
13+
public var usesTabsForIndentation: Bool
14+
public var ignoreSpaceOnlySuggestions: Bool
15+
16+
public init(
17+
fileURL: URL,
18+
content: String,
19+
cursorPosition: CursorPosition,
20+
tabSize: Int,
21+
indentSize: Int,
22+
usesTabsForIndentation: Bool,
23+
ignoreSpaceOnlySuggestions: Bool
24+
) {
25+
self.fileURL = fileURL
26+
self.content = content
27+
self.cursorPosition = cursorPosition
28+
self.tabSize = tabSize
29+
self.indentSize = indentSize
30+
self.usesTabsForIndentation = usesTabsForIndentation
31+
self.ignoreSpaceOnlySuggestions = ignoreSpaceOnlySuggestions
32+
}
33+
}
34+
35+
public protocol SuggestionServiceProvider {
36+
func getSuggestions(_ request: SuggestionRequest) async throws -> [CodeSuggestion]
37+
38+
func notifyAccepted(_ suggestion: CodeSuggestion) async
39+
func notifyRejected(_ suggestions: [CodeSuggestion]) async
40+
func notifyOpenTextDocument(fileURL: URL, content: String) async throws
41+
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
42+
func notifyCloseTextDocument(fileURL: URL) async throws
43+
func notifySaveTextDocument(fileURL: URL) async throws
44+
func cancelRequest() async
45+
func terminate() async
46+
}
47+

0 commit comments

Comments
 (0)