@@ -25,18 +25,10 @@ import var TSCBasic.localFileSystem
25
25
///
26
26
/// Provides build settings from a `CompilationDatabase` found by searching a project. For now, only
27
27
/// one compilation database, located at the project root.
28
- public final class CompilationDatabaseBuildSystem {
29
-
30
- /// Queue guarding the following properties:
31
- /// - `compdb`
32
- /// - `watchedFiles`
33
- /// - `_indexStorePath`
34
- let queue : DispatchQueue = . init( label: " CompilationDatabaseBuildSystem.queue " , qos: . userInitiated)
35
-
28
+ public actor CompilationDatabaseBuildSystem {
36
29
/// The compilation database.
37
30
var compdb : CompilationDatabase ? = nil {
38
31
didSet {
39
- dispatchPrecondition ( condition: . onQueue( queue) )
40
32
// Build settings have changed and thus the index store path might have changed.
41
33
// Recompute it on demand.
42
34
_indexStorePath = nil
@@ -60,24 +52,22 @@ public final class CompilationDatabaseBuildSystem {
60
52
61
53
private var _indexStorePath : AbsolutePath ?
62
54
public var indexStorePath : AbsolutePath ? {
63
- return queue. sync {
64
- if let indexStorePath = _indexStorePath {
65
- return indexStorePath
66
- }
55
+ if let indexStorePath = _indexStorePath {
56
+ return indexStorePath
57
+ }
67
58
68
- if let allCommands = self . compdb? . allCommands {
69
- for command in allCommands {
70
- let args = command. commandLine
71
- for i in args. indices. reversed ( ) {
72
- if args [ i] == " -index-store-path " && i != args. endIndex - 1 {
73
- _indexStorePath = try ? AbsolutePath ( validating: args [ i+ 1 ] )
74
- return _indexStorePath
75
- }
59
+ if let allCommands = self . compdb? . allCommands {
60
+ for command in allCommands {
61
+ let args = command. commandLine
62
+ for i in args. indices. reversed ( ) {
63
+ if args [ i] == " -index-store-path " && i != args. endIndex - 1 {
64
+ _indexStorePath = try ? AbsolutePath ( validating: args [ i+ 1 ] )
65
+ return _indexStorePath
76
66
}
77
67
}
78
68
}
79
- return nil
80
69
}
70
+ return nil
81
71
}
82
72
83
73
public init ( projectRoot: AbsolutePath ? = nil , fileSystem: FileSystem = localFileSystem) {
@@ -98,44 +88,31 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
98
88
public var indexPrefixMappings : [ PathPrefixMapping ] { return [ ] }
99
89
100
90
public func buildSettings( for document: DocumentURI , language: Language ) async throws -> FileBuildSettings ? {
101
- // FIXME: (async) Convert this to an async function once `CompilationDatabaseBuildSystem` is an actor.
102
- return await withCheckedContinuation { continuation in
103
- self . queue. async {
104
- continuation. resume ( returning: self . settings ( for: document) )
105
- }
106
- }
91
+ return self . settings ( for: document)
107
92
}
108
93
109
94
public func registerForChangeNotifications( for uri: DocumentURI , language: Language ) {
110
- queue. async {
111
- self . watchedFiles [ uri] = language
95
+ self . watchedFiles [ uri] = language
112
96
113
- guard let delegate = self . delegate else { return }
97
+ guard let delegate = self . delegate else { return }
114
98
115
- let settings = self . settings ( for: uri)
116
- delegate. fileBuildSettingsChanged ( [ uri: FileBuildSettingsChange ( settings) ] )
117
- }
99
+ let settings = self . settings ( for: uri)
100
+ delegate. fileBuildSettingsChanged ( [ uri: FileBuildSettingsChange ( settings) ] )
118
101
}
119
102
120
103
/// We don't support change watching.
121
104
public func unregisterForChangeNotifications( for uri: DocumentURI ) {
122
- queue. async {
123
- self . watchedFiles [ uri] = nil
124
- }
105
+ self . watchedFiles [ uri] = nil
125
106
}
126
107
127
- /// Must be invoked on `queue`.
128
108
private func database( for url: URL ) -> CompilationDatabase ? {
129
- dispatchPrecondition ( condition: . onQueue( queue) )
130
109
if let path = try ? AbsolutePath ( validating: url. path) {
131
110
return database ( for: path)
132
111
}
133
112
return compdb
134
113
}
135
114
136
- /// Must be invoked on `queue`.
137
115
private func database( for path: AbsolutePath ) -> CompilationDatabase ? {
138
- dispatchPrecondition ( condition: . onQueue( queue) )
139
116
if compdb == nil {
140
117
var dir = path
141
118
while !dir. isRoot {
@@ -165,10 +142,7 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
165
142
166
143
/// The compilation database has been changed on disk.
167
144
/// Reload it and notify the delegate about build setting changes.
168
- /// Must be called on `queue`.
169
145
private func reloadCompilationDatabase( ) {
170
- dispatchPrecondition ( condition: . onQueue( queue) )
171
-
172
146
guard let projectRoot = self . projectRoot else { return }
173
147
174
148
self . compdb = tryLoadCompilationDatabase ( directory: projectRoot, self . fileSystem)
@@ -187,31 +161,25 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
187
161
}
188
162
189
163
public func filesDidChange( _ events: [ FileEvent ] ) {
190
- queue. async {
191
- if events. contains ( where: { self . fileEventShouldTriggerCompilationDatabaseReload ( event: $0) } ) {
192
- self . reloadCompilationDatabase ( )
193
- }
164
+ if events. contains ( where: { self . fileEventShouldTriggerCompilationDatabaseReload ( event: $0) } ) {
165
+ self . reloadCompilationDatabase ( )
194
166
}
195
167
}
196
168
197
169
public func fileHandlingCapability( for uri: DocumentURI ) -> FileHandlingCapability {
198
170
guard let fileUrl = uri. fileURL else {
199
171
return . unhandled
200
172
}
201
- return queue. sync {
202
- if database ( for: fileUrl) != nil {
203
- return . handled
204
- } else {
205
- return . unhandled
206
- }
173
+ if database ( for: fileUrl) != nil {
174
+ return . handled
175
+ } else {
176
+ return . unhandled
207
177
}
208
178
}
209
179
}
210
180
211
181
extension CompilationDatabaseBuildSystem {
212
- /// Must be invoked on `queue`.
213
182
private func settings( for uri: DocumentURI ) -> FileBuildSettings ? {
214
- dispatchPrecondition ( condition: . onQueue( queue) )
215
183
guard let url = uri. fileURL else {
216
184
// We can't determine build settings for non-file URIs.
217
185
return nil
@@ -225,8 +193,6 @@ extension CompilationDatabaseBuildSystem {
225
193
226
194
/// Exposed for *testing*.
227
195
public func _settings( for uri: DocumentURI ) -> FileBuildSettings ? {
228
- return queue. sync {
229
- return self . settings ( for: uri)
230
- }
196
+ return self . settings ( for: uri)
231
197
}
232
198
}
0 commit comments