Skip to content

Commit b8b5363

Browse files
authored
Merge pull request #1676 from ahoppen/no-reload-if-settings-didnt-change
Only reopen files for which the build settings actually changed
2 parents 5574fdc + 0f78562 commit b8b5363

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Sources/BuildSystemIntegration/BuildSystemManagerDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import LanguageServerProtocol
1515

1616
/// Handles build system events, such as file build settings changes.
1717
package protocol BuildSystemManagerDelegate: AnyObject, Sendable {
18-
/// Notify the delegate that the given files' build settings have changed.
18+
/// Notify the delegate that the result of `BuildSystemManager.buildSettingsInferredFromMainFile` might have changed
19+
/// for the given files.
1920
func fileBuildSettingsChanged(_ changedFiles: Set<DocumentURI>) async
2021

2122
/// Notify the delegate that the dependencies of the given files have changed

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ package actor SwiftLanguageService: LanguageService, Sendable {
159159
}
160160
}
161161

162+
/// The build settings that were used to open the given files.
163+
///
164+
/// - Note: Not all documents open in `SwiftLanguageService` are necessarily in this dictionary because files where
165+
/// `buildSettings(for:)` returns `nil` are not included.
166+
private var buildSettingsForOpenFiles: [DocumentURI: SwiftCompileCommand] = [:]
167+
162168
/// Calling `scheduleCall` on `refreshDiagnosticsDebouncer` schedules a `DiagnosticsRefreshRequest` to be sent to
163169
/// to the client.
164170
///
@@ -400,10 +406,12 @@ extension SwiftLanguageService {
400406
try await self.sendSourcekitdRequest(closeReq, fileContents: nil)
401407
}
402408

409+
let buildSettings = await buildSettings(for: snapshot.uri)
403410
let openReq = openDocumentSourcekitdRequest(
404411
snapshot: snapshot,
405-
compileCommand: await buildSettings(for: snapshot.uri)
412+
compileCommand: buildSettings
406413
)
414+
self.buildSettingsForOpenFiles[snapshot.uri] = buildSettings
407415
_ = await orLog("Re-opening document") {
408416
try await self.sendSourcekitdRequest(openReq, fileContents: snapshot.text)
409417
}
@@ -420,11 +428,14 @@ extension SwiftLanguageService {
420428
guard (try? documentManager.openDocuments.contains(uri)) ?? false else {
421429
return
422430
}
423-
// Close and re-open the document internally to inform sourcekitd to update the compile command. At the moment
424-
// there's no better way to do this.
425-
// Schedule the document re-open in the SourceKit-LSP server. This ensures that the re-open happens exclusively with
426-
// no other request running at the same time.
427-
sourceKitLSPServer?.handle(ReopenTextDocumentNotification(textDocument: TextDocumentIdentifier(uri)))
431+
let newBuildSettings = await self.buildSettings(for: uri)
432+
if newBuildSettings != buildSettingsForOpenFiles[uri] {
433+
// Close and re-open the document internally to inform sourcekitd to update the compile command. At the moment
434+
// there's no better way to do this.
435+
// Schedule the document re-open in the SourceKit-LSP server. This ensures that the re-open happens exclusively with
436+
// no other request running at the same time.
437+
sourceKitLSPServer?.handle(ReopenTextDocumentNotification(textDocument: TextDocumentIdentifier(uri)))
438+
}
428439
}
429440

430441
package func documentDependenciesUpdated(_ uris: Set<DocumentURI>) async {
@@ -445,8 +456,7 @@ extension SwiftLanguageService {
445456

446457
for uri in uris {
447458
await macroExpansionManager.purge(primaryFile: uri)
448-
// `documentUpdatedBuildSettings` already handles reopening the document, so we do that here as well.
449-
await self.documentUpdatedBuildSettings(uri)
459+
sourceKitLSPServer?.handle(ReopenTextDocumentNotification(textDocument: TextDocumentIdentifier(uri)))
450460
}
451461
}
452462

@@ -485,6 +495,7 @@ extension SwiftLanguageService {
485495
await diagnosticReportManager.removeItemsFromCache(with: notification.textDocument.uri)
486496

487497
let buildSettings = await self.buildSettings(for: snapshot.uri)
498+
buildSettingsForOpenFiles[snapshot.uri] = buildSettings
488499

489500
let req = openDocumentSourcekitdRequest(snapshot: snapshot, compileCommand: buildSettings)
490501
_ = try? await self.sendSourcekitdRequest(req, fileContents: snapshot.text)
@@ -500,6 +511,7 @@ extension SwiftLanguageService {
500511
cancelInFlightPublishDiagnosticsTask(for: notification.textDocument.uri)
501512
inFlightPublishDiagnosticsTasks[notification.textDocument.uri] = nil
502513
await diagnosticReportManager.removeItemsFromCache(with: notification.textDocument.uri)
514+
buildSettingsForOpenFiles[notification.textDocument.uri] = nil
503515

504516
let req = closeDocumentSourcekitdRequest(uri: notification.textDocument.uri)
505517
_ = try? await self.sendSourcekitdRequest(req, fileContents: nil)

0 commit comments

Comments
 (0)