Skip to content

Commit ea445cb

Browse files
committed
Convert ManualMainFilesProvider to be an actor
Refactor the code Remove unused code Refine the code
1 parent f265857 commit ea445cb

File tree

3 files changed

+49
-54
lines changed

3 files changed

+49
-54
lines changed

Sources/SKCore/BuildSystemManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ extension BuildSystemManager {
151151
for document: DocumentURI,
152152
language: Language
153153
) async -> FileBuildSettings? {
154-
let mainFile = mainFile(for: document)
154+
let mainFile = await mainFile(for: document)
155155
guard var settings = await buildSettings(for: mainFile, language: language) else {
156156
return nil
157157
}
@@ -182,7 +182,7 @@ extension BuildSystemManager {
182182

183183
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
184184
logger.debug("registerForChangeNotifications(\(uri.forLogging))")
185-
let mainFile = mainFile(for: uri)
185+
let mainFile = await mainFile(for: uri)
186186
self.watchedFiles[uri] = (mainFile, language)
187187

188188
// Register for change notifications of the main file in the underlying build
@@ -274,7 +274,7 @@ extension BuildSystemManager: MainFilesDelegate {
274274
public func mainFilesChanged() async {
275275
var changedMainFileAssociations: Set<DocumentURI> = []
276276
for (file, (oldMainFile, language)) in self.watchedFiles {
277-
let newMainFile = self.mainFile(for: file, useCache: false)
277+
let newMainFile = await self.mainFile(for: file, useCache: false)
278278
if newMainFile != oldMainFile {
279279
self.watchedFiles[file] = (newMainFile, language)
280280
changedMainFileAssociations.insert(file)
@@ -303,7 +303,7 @@ extension BuildSystemManager: MainFilesDelegate {
303303
/// For Swift or normal C files, this will be the file itself. For header
304304
/// files, we pick a main file that includes the header since header files
305305
/// don't have build settings by themselves.
306-
private func mainFile(for uri: DocumentURI, useCache: Bool = true) -> DocumentURI {
306+
private func mainFile(for uri: DocumentURI, useCache: Bool = true) async -> DocumentURI {
307307
if useCache, let mainFile = self.watchedFiles[uri]?.mainFile {
308308
// Performance optimization: We did already compute the main file and have
309309
// it cached. We can just return it.
@@ -313,7 +313,7 @@ extension BuildSystemManager: MainFilesDelegate {
313313
return uri
314314
}
315315

316-
let mainFiles = mainFilesProvider.mainFilesContainingFile(uri)
316+
let mainFiles = await mainFilesProvider.mainFilesContainingFile(uri)
317317
if mainFiles.contains(uri) {
318318
// If the main files contain the file itself, prefer to use that one
319319
return uri

Sources/SKCore/MainFilesProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol MainFilesProvider: AnyObject {
2323
/// mainFilesContainingFile("foo.cpp") == Set(["foo.cpp"])
2424
/// mainFilesContainingFile("foo.h") == Set(["foo.cpp", "bar.cpp"])
2525
/// ```
26-
func mainFilesContainingFile(_: DocumentURI) -> Set<DocumentURI>
26+
func mainFilesContainingFile(_: DocumentURI) async -> Set<DocumentURI>
2727
}
2828

2929
/// Delegate that responds to possible main file changes.

Tests/SKCoreTests/BuildSystemManagerTests.swift

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ final class BuildSystemManagerTests: XCTestCase {
2525
let c = DocumentURI(string: "bsm:c")
2626
let d = DocumentURI(string: "bsm:d")
2727

28-
let mainFiles = ManualMainFilesProvider()
29-
mainFiles.mainFiles = [
30-
a: Set([c]),
31-
b: Set([c, d]),
32-
c: Set([c]),
33-
d: Set([d]),
34-
]
28+
let mainFiles = ManualMainFilesProvider(
29+
[
30+
a: [c],
31+
b: [c, d],
32+
c: [c],
33+
d: [d],
34+
]
35+
)
3536

3637
let bsm = await BuildSystemManager(
3738
buildSystem: nil,
@@ -55,13 +56,9 @@ final class BuildSystemManagerTests: XCTestCase {
5556
await assertEqual(bsm._cachedMainFile(for: c), c)
5657
await assertEqual(bsm._cachedMainFile(for: d), d)
5758

58-
mainFiles.mainFiles = [
59-
a: Set([a]),
60-
b: Set([c, d, a]),
61-
c: Set([c]),
62-
d: Set([d]),
63-
]
64-
59+
await mainFiles.updateMainFiles(for: a, to: [a])
60+
await mainFiles.updateMainFiles(for: b, to: [c, d, a])
61+
6562
await assertEqual(bsm._cachedMainFile(for: a), c)
6663
await assertEqual(bsm._cachedMainFile(for: b), bMain)
6764
await assertEqual(bsm._cachedMainFile(for: c), c)
@@ -92,8 +89,7 @@ final class BuildSystemManagerTests: XCTestCase {
9289

9390
func testSettingsMainFile() async throws {
9491
let a = DocumentURI(string: "bsm:a.swift")
95-
let mainFiles = ManualMainFilesProvider()
96-
mainFiles.mainFiles = [a: Set([a])]
92+
let mainFiles = ManualMainFilesProvider([a: [a]])
9793
let bs = ManualBuildSystem()
9894
let bsm = await BuildSystemManager(
9995
buildSystem: bs,
@@ -116,8 +112,7 @@ final class BuildSystemManagerTests: XCTestCase {
116112

117113
func testSettingsMainFileInitialNil() async throws {
118114
let a = DocumentURI(string: "bsm:a.swift")
119-
let mainFiles = ManualMainFilesProvider()
120-
mainFiles.mainFiles = [a: Set([a])]
115+
let mainFiles = ManualMainFilesProvider([a: [a]])
121116
let bs = ManualBuildSystem()
122117
let bsm = await BuildSystemManager(
123118
buildSystem: bs,
@@ -138,8 +133,7 @@ final class BuildSystemManagerTests: XCTestCase {
138133

139134
func testSettingsMainFileWithFallback() async throws {
140135
let a = DocumentURI(string: "bsm:a.swift")
141-
let mainFiles = ManualMainFilesProvider()
142-
mainFiles.mainFiles = [a: Set([a])]
136+
let mainFiles = ManualMainFilesProvider([a: [a]])
143137
let bs = ManualBuildSystem()
144138
let fallback = FallbackBuildSystem(buildSetup: .default)
145139
let bsm = await BuildSystemManager(
@@ -169,8 +163,7 @@ final class BuildSystemManagerTests: XCTestCase {
169163
func testSettingsMainFileInitialIntersect() async throws {
170164
let a = DocumentURI(string: "bsm:a.swift")
171165
let b = DocumentURI(string: "bsm:b.swift")
172-
let mainFiles = ManualMainFilesProvider()
173-
mainFiles.mainFiles = [a: Set([a]), b: Set([b])]
166+
let mainFiles = ManualMainFilesProvider([a: [a], b: [b]])
174167
let bs = ManualBuildSystem()
175168
let bsm = await BuildSystemManager(
176169
buildSystem: bs,
@@ -210,8 +203,7 @@ final class BuildSystemManagerTests: XCTestCase {
210203
func testSettingsMainFileUnchanged() async throws {
211204
let a = DocumentURI(string: "bsm:a.swift")
212205
let b = DocumentURI(string: "bsm:b.swift")
213-
let mainFiles = ManualMainFilesProvider()
214-
mainFiles.mainFiles = [a: Set([a]), b: Set([b])]
206+
let mainFiles = ManualMainFilesProvider([a: [a], b: [b]])
215207
let bs = ManualBuildSystem()
216208
let bsm = await BuildSystemManager(
217209
buildSystem: bs,
@@ -242,12 +234,13 @@ final class BuildSystemManagerTests: XCTestCase {
242234
let h = DocumentURI(string: "bsm:header.h")
243235
let cpp1 = DocumentURI(string: "bsm:main.cpp")
244236
let cpp2 = DocumentURI(string: "bsm:other.cpp")
245-
let mainFiles = ManualMainFilesProvider()
246-
mainFiles.mainFiles = [
247-
h: Set([cpp1]),
248-
cpp1: Set([cpp1]),
249-
cpp2: Set([cpp2]),
250-
]
237+
let mainFiles = ManualMainFilesProvider(
238+
[
239+
h: [cpp1],
240+
cpp1: [cpp1],
241+
cpp2: [cpp2],
242+
]
243+
)
251244

252245
let bs = ManualBuildSystem()
253246
let bsm = await BuildSystemManager(
@@ -264,7 +257,7 @@ final class BuildSystemManagerTests: XCTestCase {
264257
await bsm.registerForChangeNotifications(for: h, language: .c)
265258
assertEqual(await bsm.buildSettingsInferredFromMainFile(for: h, language: .c), bs.map[cpp1]!)
266259

267-
mainFiles.mainFiles[h] = Set([cpp2])
260+
await mainFiles.updateMainFiles(for: h, to: [cpp2])
268261

269262
let changed = expectation(description: "changed settings to cpp2")
270263
await del.setExpected([(h, .c, bs.map[cpp2]!, changed, #file, #line)])
@@ -277,14 +270,14 @@ final class BuildSystemManagerTests: XCTestCase {
277270
await bsm.mainFilesChanged()
278271
try await fulfillmentOfOrThrow([changed2], timeout: 1)
279272

280-
mainFiles.mainFiles[h] = Set([cpp1, cpp2])
273+
await mainFiles.updateMainFiles(for: h, to: [cpp1, cpp2])
281274

282275
let changed3 = expectation(description: "added lexicographically earlier main file")
283276
await del.setExpected([(h, .c, bs.map[cpp1]!, changed3, #file, #line)])
284277
await bsm.mainFilesChanged()
285278
try await fulfillmentOfOrThrow([changed3], timeout: 1)
286279

287-
mainFiles.mainFiles[h] = Set([])
280+
await mainFiles.updateMainFiles(for: h, to: [])
288281

289282
let changed4 = expectation(description: "changed settings to []")
290283
await del.setExpected([(h, .c, nil, changed4, #file, #line)])
@@ -296,11 +289,12 @@ final class BuildSystemManagerTests: XCTestCase {
296289
let h1 = DocumentURI(string: "bsm:header1.h")
297290
let h2 = DocumentURI(string: "bsm:header2.h")
298291
let cpp = DocumentURI(string: "bsm:main.cpp")
299-
let mainFiles = ManualMainFilesProvider()
300-
mainFiles.mainFiles = [
301-
h1: Set([cpp]),
302-
h2: Set([cpp]),
303-
]
292+
let mainFiles = ManualMainFilesProvider(
293+
[
294+
h1: [cpp],
295+
h2: [cpp],
296+
]
297+
)
304298

305299
let bs = ManualBuildSystem()
306300
let bsm = await BuildSystemManager(
@@ -342,8 +336,7 @@ final class BuildSystemManagerTests: XCTestCase {
342336
let a = DocumentURI(string: "bsm:a.swift")
343337
let b = DocumentURI(string: "bsm:b.swift")
344338
let c = DocumentURI(string: "bsm:c.swift")
345-
let mainFiles = ManualMainFilesProvider()
346-
mainFiles.mainFiles = [a: Set([a]), b: Set([b]), c: Set([c])]
339+
let mainFiles = ManualMainFilesProvider([a: [a], b: [b], c: [c]])
347340
let bs = ManualBuildSystem()
348341
let bsm = await BuildSystemManager(
349342
buildSystem: bs,
@@ -384,8 +377,7 @@ final class BuildSystemManagerTests: XCTestCase {
384377

385378
func testDependenciesUpdated() async throws {
386379
let a = DocumentURI(string: "bsm:a.swift")
387-
let mainFiles = ManualMainFilesProvider()
388-
mainFiles.mainFiles = [a: Set([a])]
380+
let mainFiles = ManualMainFilesProvider([a: [a]])
389381

390382
let bs = ManualBuildSystem()
391383
let bsm = await BuildSystemManager(
@@ -412,12 +404,15 @@ final class BuildSystemManagerTests: XCTestCase {
412404
// MARK: Helper Classes for Testing
413405

414406
/// A simple `MainFilesProvider` that wraps a dictionary, for testing.
415-
private final class ManualMainFilesProvider: MainFilesProvider {
416-
let lock: DispatchQueue = DispatchQueue(label: "\(ManualMainFilesProvider.self)-lock")
417-
private var _mainFiles: [DocumentURI: Set<DocumentURI>] = [:]
418-
var mainFiles: [DocumentURI: Set<DocumentURI>] {
419-
get { lock.sync { _mainFiles } }
420-
set { lock.sync { _mainFiles = newValue } }
407+
private final actor ManualMainFilesProvider: MainFilesProvider {
408+
private var mainFiles: [DocumentURI: Set<DocumentURI>]
409+
410+
init(_ mainFiles: [DocumentURI: Set<DocumentURI>]) {
411+
self.mainFiles = mainFiles
412+
}
413+
414+
func updateMainFiles(for file: DocumentURI, to mainFiles: Set<DocumentURI>)async {
415+
self.mainFiles[file] = mainFiles
421416
}
422417

423418
func mainFilesContainingFile(_ file: DocumentURI) -> Set<DocumentURI> {

0 commit comments

Comments
 (0)