Skip to content

Commit 0c09a2d

Browse files
committed
Remove Environment.createAuthService
1 parent 36e45cb commit 0c09a2d

File tree

6 files changed

+77
-57
lines changed

6 files changed

+77
-57
lines changed

Core/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ let package = Package(
5252
name: "Service",
5353
dependencies: [
5454
"SuggestionModel",
55+
"SuggestionService",
5556
"GitHubCopilotService",
5657
"OpenAIService",
5758
"Preferences",

Core/Sources/Environment/Environment.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import ActiveApplicationMonitor
22
import AppKit
33
import AXExtension
4-
import GitHubCopilotService
54
import Foundation
5+
import GitHubCopilotService
66
import Logger
7+
import SuggestionService
78

89
public struct NoAccessToAccessibilityAPIError: Error, LocalizedError {
910
public var errorDescription: String? {
@@ -105,32 +106,28 @@ public enum Environment {
105106
}
106107
throw FailedToFetchFileURLError()
107108
}
108-
109+
109110
public static var fetchFocusedElementURI: () async throws -> URL = {
110111
guard let xcode = ActiveApplicationMonitor.activeXcode
111112
?? ActiveApplicationMonitor.latestXcode
112113
else {
113114
throw FailedToFetchFileURLError()
114115
}
115-
116+
116117
let application = AXUIElementCreateApplication(xcode.processIdentifier)
117118
let focusedElement = application.focusedElement
118119
if focusedElement?.description != "Source Editor" {
119120
let window = application.focusedWindow
120121
let id = window?.identifier.hashValue
121122
return URL(fileURLWithPath: "/xcode-focused-element/\(id ?? 0)")
122123
}
123-
124-
return try await fetchCurrentFileURL()
125-
}
126124

127-
public static var createAuthService: () -> GitHubCopilotAuthServiceType = {
128-
GitHubCopilotAuthService()
125+
return try await fetchCurrentFileURL()
129126
}
130127

131128
public static var createSuggestionService: (_ projectRootURL: URL)
132-
-> GitHubCopilotSuggestionServiceType = { projectRootURL in
133-
GitHubCopilotSuggestionService(projectRootURL: projectRootURL)
129+
-> SuggestionServiceType = { projectRootURL in
130+
SuggestionService(projectRootURL: projectRootURL)
134131
}
135132

136133
public static var triggerAction: (_ name: String) async throws -> Void = { name in
@@ -139,7 +136,7 @@ public enum Environment {
139136
else { return }
140137
let bundleName = Bundle.main
141138
.object(forInfoDictionaryKey: "EXTENSION_BUNDLE_NAME") as! String
142-
139+
143140
await Task.yield()
144141

145142
if UserDefaults.shared.value(for: \.triggerActionWithAccessibilityAPI) {
@@ -173,7 +170,7 @@ public enum Environment {
173170
"Can't run command \(name)."
174171
}
175172
}
176-
173+
177174
throw CantRunCommand(name: name)
178175
}
179176
} else {
@@ -254,3 +251,4 @@ extension FileManager {
254251
return isDirectory.boolValue && exists
255252
}
256253
}
254+

Core/Sources/Service/Workspace.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import ChatService
2-
import SuggestionModel
3-
import GitHubCopilotService
42
import Environment
53
import Foundation
4+
import GitHubCopilotService
65
import Logger
76
import Preferences
87
import SuggestionInjector
8+
import SuggestionModel
9+
import SuggestionService
910
import XPCShared
1011

1112
@ServiceActor
@@ -91,25 +92,25 @@ final class Workspace {
9192
], context: nil
9293
)
9394

94-
private var _copilotSuggestionService: GitHubCopilotSuggestionServiceType?
95+
private var _suggestionService: SuggestionServiceType?
9596

96-
private var copilotSuggestionService: GitHubCopilotSuggestionServiceType? {
97+
private var suggestionService: SuggestionServiceType? {
9798
// Check if the workspace is disabled.
9899
let isSuggestionDisabledGlobally = UserDefaults.shared
99100
.value(for: \.disableSuggestionFeatureGlobally)
100101
if isSuggestionDisabledGlobally {
101102
let enabledList = UserDefaults.shared.value(for: \.suggestionFeatureEnabledProjectList)
102103
if !enabledList.contains(where: { path in projectRootURL.path.hasPrefix(path) }) {
103104
// If it's disable, remove the service
104-
_copilotSuggestionService = nil
105+
_suggestionService = nil
105106
return nil
106107
}
107108
}
108109

109-
if _copilotSuggestionService == nil {
110-
_copilotSuggestionService = Environment.createSuggestionService(projectRootURL)
110+
if _suggestionService == nil {
111+
_suggestionService = Environment.createSuggestionService(projectRootURL)
111112
}
112-
return _copilotSuggestionService
113+
return _suggestionService
113114
}
114115

115116
var isSuggestionFeatureEnabled: Bool {
@@ -129,10 +130,10 @@ final class Workspace {
129130

130131
userDefaultsObserver.onChange = { [weak self] in
131132
guard let self else { return }
132-
_ = self.copilotSuggestionService
133+
_ = self.suggestionService
133134
}
134135
}
135-
136+
136137
func refreshUpdateTime() {
137138
lastSuggestionUpdateTime = Environment.now()
138139
}
@@ -219,15 +220,16 @@ extension Workspace {
219220

220221
filespace.suggestionSourceSnapshot = snapshot
221222

222-
guard let copilotSuggestionService else { throw SuggestionFeatureDisabledError() }
223-
let completions = try await copilotSuggestionService.getCompletions(
223+
guard let suggestionService else { throw SuggestionFeatureDisabledError() }
224+
let completions = try await suggestionService.getSuggestions(
224225
fileURL: fileURL,
225226
content: editor.lines.joined(separator: ""),
226227
cursorPosition: editor.cursorPosition,
227228
tabSize: editor.tabSize,
228229
indentSize: editor.indentSize,
229230
usesTabsForIndentation: editor.usesTabsForIndentation,
230-
ignoreSpaceOnlySuggestions: true
231+
ignoreSpaceOnlySuggestions: true,
232+
referenceFileURL: filespaces.values.map(\.fileURL)
231233
)
232234

233235
filespace.suggestions = completions
@@ -271,7 +273,7 @@ extension Workspace {
271273
filespaces[fileURL]?.usesTabsForIndentation = editor.usesTabsForIndentation
272274
}
273275
Task {
274-
await copilotSuggestionService?.notifyRejected(filespaces[fileURL]?.suggestions ?? [])
276+
await suggestionService?.notifyRejected(filespaces[fileURL]?.suggestions ?? [])
275277
}
276278
filespaces[fileURL]?.reset(resetSnapshot: false)
277279
}
@@ -296,8 +298,8 @@ extension Workspace {
296298
let suggestion = allSuggestions.remove(at: filespace.suggestionIndex)
297299

298300
Task {
299-
await copilotSuggestionService?.notifyAccepted(suggestion)
300-
await copilotSuggestionService?.notifyRejected(allSuggestions)
301+
await suggestionService?.notifyAccepted(suggestion)
302+
await suggestionService?.notifyRejected(allSuggestions)
301303
}
302304

303305
filespaces[fileURL]?.reset()
@@ -308,7 +310,7 @@ extension Workspace {
308310
func notifyOpenFile(filespace: Filespace) {
309311
refreshUpdateTime()
310312
Task {
311-
try await copilotSuggestionService?.notifyOpenTextDocument(
313+
try await suggestionService?.notifyOpenTextDocument(
312314
fileURL: filespace.fileURL,
313315
content: try String(contentsOf: filespace.fileURL, encoding: .utf8)
314316
)
@@ -319,7 +321,7 @@ extension Workspace {
319321
filespace.refreshUpdateTime()
320322
refreshUpdateTime()
321323
Task {
322-
try await copilotSuggestionService?.notifyChangeTextDocument(
324+
try await suggestionService?.notifyChangeTextDocument(
323325
fileURL: filespace.fileURL,
324326
content: content
325327
)
@@ -330,7 +332,7 @@ extension Workspace {
330332
filespace.refreshUpdateTime()
331333
refreshUpdateTime()
332334
Task {
333-
try await copilotSuggestionService?.notifySaveTextDocument(fileURL: filespace.fileURL)
335+
try await suggestionService?.notifySaveTextDocument(fileURL: filespace.fileURL)
334336
}
335337
}
336338
}
@@ -340,7 +342,7 @@ extension Workspace {
340342
for (fileURL, _) in filespaces {
341343
if isFilespaceExpired(fileURL: fileURL, availableTabs: availableTabs) {
342344
Task {
343-
try await copilotSuggestionService?.notifyCloseTextDocument(fileURL: fileURL)
345+
try await suggestionService?.notifyCloseTextDocument(fileURL: fileURL)
344346
}
345347
filespaces[fileURL] = nil
346348
}

Core/Sources/Service/XPCService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public class XPCService: NSObject, XPCServiceProtocol {
3232
// MARK: - Copilot Auth
3333

3434
@ServiceActor
35-
lazy var authService: GitHubCopilotAuthServiceType = Environment.createAuthService()
35+
lazy var gitHubCopilotAuthService: GitHubCopilotAuthServiceType = GitHubCopilotAuthService()
3636

3737
public func checkStatus(withReply reply: @escaping (String?, Error?) -> Void) {
3838
Task { @ServiceActor in
3939
do {
40-
let status = try await authService.checkStatus()
40+
let status = try await gitHubCopilotAuthService.checkStatus()
4141
reply(status.rawValue, nil)
4242
} catch {
4343
reply(nil, NSError.from(error))
@@ -48,7 +48,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
4848
public func signInInitiate(withReply reply: @escaping (String?, String?, Error?) -> Void) {
4949
Task { @ServiceActor in
5050
do {
51-
let (verificationLink, userCode) = try await authService.signInInitiate()
51+
let (verificationLink, userCode) = try await gitHubCopilotAuthService.signInInitiate()
5252
reply(verificationLink, userCode, nil)
5353
} catch {
5454
reply(nil, nil, NSError.from(error))
@@ -62,7 +62,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
6262
) {
6363
Task { @ServiceActor in
6464
do {
65-
let (username, status) = try await authService.signInConfirm(userCode: userCode)
65+
let (username, status) = try await gitHubCopilotAuthService.signInConfirm(userCode: userCode)
6666
reply(username, status.rawValue, nil)
6767
} catch {
6868
reply(nil, nil, NSError.from(error))
@@ -73,7 +73,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
7373
public func getVersion(withReply reply: @escaping (String?, Error?) -> Void) {
7474
Task { @ServiceActor in
7575
do {
76-
let version = try await authService.version()
76+
let version = try await gitHubCopilotAuthService.version()
7777
reply(version, nil)
7878
} catch {
7979
reply(nil, NSError.from(error))
@@ -84,7 +84,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
8484
public func signOut(withReply reply: @escaping (String?, Error?) -> Void) {
8585
Task { @ServiceActor in
8686
do {
87-
let status = try await authService.signOut()
87+
let status = try await gitHubCopilotAuthService.signOut()
8888
reply(status.rawValue, nil)
8989
} catch {
9090
reply(nil, NSError.from(error))

Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import SuggestionModel
33
import GitHubCopilotService
44

5-
protocol SuggestionServiceType {
5+
public protocol SuggestionServiceType {
66
func getSuggestions(
77
fileURL: URL,
88
content: String,
@@ -23,31 +23,54 @@ protocol SuggestionServiceType {
2323
}
2424

2525
public final class SuggestionService: SuggestionServiceType {
26-
func getSuggestions(fileURL: URL, content: String, cursorPosition: SuggestionModel.CursorPosition, tabSize: Int, indentSize: Int, usesTabsForIndentation: Bool, ignoreSpaceOnlySuggestions: Bool, referenceFileURL: [URL]) async throws -> [SuggestionModel.CodeSuggestion] {
27-
fatalError()
26+
let gitHubCopilotService: GitHubCopilotSuggestionServiceType
27+
28+
public init(projectRootURL: URL) {
29+
gitHubCopilotService = GitHubCopilotSuggestionService(projectRootURL: projectRootURL)
30+
}
31+
32+
public func getSuggestions(
33+
fileURL: URL,
34+
content: String,
35+
cursorPosition: SuggestionModel.CursorPosition,
36+
tabSize: Int,
37+
indentSize: Int,
38+
usesTabsForIndentation: Bool,
39+
ignoreSpaceOnlySuggestions: Bool,
40+
referenceFileURL: [URL]
41+
) async throws -> [SuggestionModel.CodeSuggestion] {
42+
try await gitHubCopilotService.getCompletions(
43+
fileURL: fileURL,
44+
content: content,
45+
cursorPosition: cursorPosition,
46+
tabSize: tabSize,
47+
indentSize: indentSize,
48+
usesTabsForIndentation: usesTabsForIndentation,
49+
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
50+
)
2851
}
2952

30-
func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
31-
fatalError()
53+
public func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
54+
await gitHubCopilotService.notifyAccepted(suggestion)
3255
}
3356

34-
func notifyRejected(_ suggestions: [SuggestionModel.CodeSuggestion]) async {
35-
fatalError()
57+
public func notifyRejected(_ suggestions: [SuggestionModel.CodeSuggestion]) async {
58+
await gitHubCopilotService.notifyRejected(suggestions)
3659
}
3760

38-
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
39-
fatalError()
61+
public func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
62+
try await gitHubCopilotService.notifyOpenTextDocument(fileURL: fileURL, content: content)
4063
}
4164

42-
func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
43-
fatalError()
65+
public func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
66+
try await gitHubCopilotService.notifyChangeTextDocument(fileURL: fileURL, content: content)
4467
}
4568

46-
func notifyCloseTextDocument(fileURL: URL) async throws {
47-
fatalError()
69+
public func notifyCloseTextDocument(fileURL: URL) async throws {
70+
try await gitHubCopilotService.notifyCloseTextDocument(fileURL: fileURL)
4871
}
4972

50-
func notifySaveTextDocument(fileURL: URL) async throws {
51-
fatalError()
73+
public func notifySaveTextDocument(fileURL: URL) async throws {
74+
try await gitHubCopilotService.notifySaveTextDocument(fileURL: fileURL)
5275
}
5376
}

Core/Tests/ServiceTests/Environment.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import XPCShared
2222
URL(fileURLWithPath: "/path/to/project/file.swift")
2323
}
2424

25-
Environment.createAuthService = {
26-
fatalError("")
27-
}
28-
2925
Environment.createSuggestionService = {
3026
_ in fatalError("")
3127
}

0 commit comments

Comments
 (0)