-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSuggestionWorkspacePlugin.swift
More file actions
95 lines (81 loc) · 3.29 KB
/
SuggestionWorkspacePlugin.swift
File metadata and controls
95 lines (81 loc) · 3.29 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
import BuiltinExtension
import Foundation
import Preferences
import SuggestionBasic
import SuggestionProvider
import UserDefaultsObserver
import Workspace
public final class SuggestionServiceWorkspacePlugin: WorkspacePlugin {
public typealias SuggestionServiceFactory = () -> any SuggestionServiceProvider
let suggestionServiceFactory: SuggestionServiceFactory
let suggestionFeatureUsabilityObserver = UserDefaultsObserver(
object: UserDefaults.shared, forKeyPaths: [
UserDefaultPreferenceKeys().suggestionFeatureEnabledProjectList.key,
UserDefaultPreferenceKeys().disableSuggestionFeatureGlobally.key,
], context: nil
)
let providerChangeObserver = UserDefaultsObserver(
object: UserDefaults.shared,
forKeyPaths: [UserDefaultPreferenceKeys().suggestionFeatureProvider.key],
context: nil
)
public var isRealtimeSuggestionEnabled: Bool {
UserDefaults.shared.value(for: \.realtimeSuggestionToggle)
}
private var _suggestionService: SuggestionServiceProvider?
public var suggestionService: SuggestionServiceProvider? {
// Check if the workspace is disabled.
let isSuggestionDisabledGlobally = UserDefaults.shared
.value(for: \.disableSuggestionFeatureGlobally)
if isSuggestionDisabledGlobally {
let enabledList = UserDefaults.shared.value(for: \.suggestionFeatureEnabledProjectList)
if !enabledList.contains(where: { path in projectRootURL.path.hasPrefix(path) }) {
// If it's disable, remove the service
_suggestionService = nil
return nil
}
}
if _suggestionService == nil {
_suggestionService = suggestionServiceFactory()
}
return _suggestionService
}
public var isSuggestionFeatureEnabled: Bool {
let isSuggestionDisabledGlobally = UserDefaults.shared
.value(for: \.disableSuggestionFeatureGlobally)
if isSuggestionDisabledGlobally {
let enabledList = UserDefaults.shared.value(for: \.suggestionFeatureEnabledProjectList)
if !enabledList.contains(where: { path in projectRootURL.path.hasPrefix(path) }) {
return false
}
}
return true
}
public init(
workspace: Workspace,
suggestionProviderFactory: @escaping SuggestionServiceFactory
) {
suggestionServiceFactory = suggestionProviderFactory
super.init(workspace: workspace)
suggestionFeatureUsabilityObserver.onChange = { [weak self] in
guard let self else { return }
_ = self.suggestionService
}
providerChangeObserver.onChange = { [weak self] in
guard let self else { return }
self._suggestionService = nil
}
}
func notifyAccepted(_ suggestion: CodeSuggestion) async {
await suggestionService?.notifyAccepted(
suggestion,
workspaceInfo: .init(workspaceURL: workspaceURL, projectURL: projectRootURL)
)
}
func notifyRejected(_ suggestions: [CodeSuggestion]) async {
await suggestionService?.notifyRejected(
suggestions,
workspaceInfo: .init(workspaceURL: workspaceURL, projectURL: projectRootURL)
)
}
}