@@ -93,7 +93,7 @@ public struct SwiftCompileCommand: Sendable, Equatable {
9393}
9494
9595public actor SwiftLanguageService : LanguageService , Sendable {
96- /// The ``SourceKitLSPServer`` instance that created this `ClangLanguageService `.
96+ /// The ``SourceKitLSPServer`` instance that created this `SwiftLanguageService `.
9797 weak var sourceKitLSPServer : SourceKitLSPServer ?
9898
9999 let sourcekitd : SourceKitD
@@ -121,9 +121,6 @@ public actor SwiftLanguageService: LanguageService, Sendable {
121121 options. generatedFilesAbsolutePath. asURL. appendingPathComponent ( " GeneratedMacroExpansions " )
122122 }
123123
124- // FIXME: ideally we wouldn't need separate management from a parent server in the same process.
125- var documentManager : DocumentManager
126-
127124 /// For each edited document, the last task that was triggered to send a `PublishDiagnosticsNotification`.
128125 ///
129126 /// This is used to cancel previous publish diagnostics tasks if an edit is made to a document.
@@ -148,6 +145,15 @@ public actor SwiftLanguageService: LanguageService, Sendable {
148145
149146 private var diagnosticReportManager : DiagnosticReportManager !
150147
148+ var documentManager : DocumentManager {
149+ get throws {
150+ guard let sourceKitLSPServer = self . sourceKitLSPServer else {
151+ throw ResponseError . unknown ( " Connection to the editor closed " )
152+ }
153+ return sourceKitLSPServer. documentManager
154+ }
155+ }
156+
151157 /// Calling `scheduleCall` on `refreshDiagnosticsDebouncer` schedules a `DiagnosticsRefreshRequest` to be sent to
152158 /// to the client.
153159 ///
@@ -183,7 +189,6 @@ public actor SwiftLanguageService: LanguageService, Sendable {
183189 self . capabilityRegistry = workspace. capabilityRegistry
184190 self . semanticIndexManager = workspace. semanticIndexManager
185191 self . testHooks = testHooks
186- self . documentManager = DocumentManager ( )
187192 self . state = . connected
188193 self . diagnosticReportManager = nil // Needed to work around rdar://116221716
189194 self . options = options
@@ -203,7 +208,7 @@ public actor SwiftLanguageService: LanguageService, Sendable {
203208 sourcekitd: self . sourcekitd,
204209 options: options,
205210 syntaxTreeManager: syntaxTreeManager,
206- documentManager: documentManager,
211+ documentManager: try documentManager,
207212 clientHasDiagnosticsCodeDescriptionSupport: await self . clientHasDiagnosticsCodeDescriptionSupport
208213 )
209214
@@ -439,15 +444,10 @@ extension SwiftLanguageService {
439444 ] )
440445 }
441446
442- public func openDocument( _ notification: DidOpenTextDocumentNotification ) async {
447+ public func openDocument( _ notification: DidOpenTextDocumentNotification , snapshot : DocumentSnapshot ) async {
443448 cancelInFlightPublishDiagnosticsTask ( for: notification. textDocument. uri)
444449 await diagnosticReportManager. removeItemsFromCache ( with: notification. textDocument. uri)
445450
446- guard let snapshot = self . documentManager. open ( notification) else {
447- // Already logged failure.
448- return
449- }
450-
451451 let buildSettings = await self . buildSettings ( for: snapshot. uri)
452452
453453 let req = openDocumentSourcekitdRequest ( snapshot: snapshot, compileCommand: buildSettings)
@@ -460,8 +460,6 @@ extension SwiftLanguageService {
460460 inFlightPublishDiagnosticsTasks [ notification. textDocument. uri] = nil
461461 await diagnosticReportManager. removeItemsFromCache ( with: notification. textDocument. uri)
462462
463- self . documentManager. close ( notification)
464-
465463 let req = closeDocumentSourcekitdRequest ( uri: notification. textDocument. uri)
466464 _ = try ? await self . sendSourcekitdRequest ( req, fileContents: nil )
467465 }
@@ -542,7 +540,12 @@ extension SwiftLanguageService {
542540 }
543541 }
544542
545- public func changeDocument( _ notification: DidChangeTextDocumentNotification ) async {
543+ public func changeDocument(
544+ _ notification: DidChangeTextDocumentNotification ,
545+ preEditSnapshot: DocumentSnapshot ,
546+ postEditSnapshot: DocumentSnapshot ,
547+ edits: [ SourceEdit ]
548+ ) async {
546549 cancelInFlightPublishDiagnosticsTask ( for: notification. textDocument. uri)
547550
548551 let keys = self . keys
@@ -552,10 +555,6 @@ extension SwiftLanguageService {
552555 let replacement : String
553556 }
554557
555- guard let ( preEditSnapshot, postEditSnapshot, edits) = self . documentManager. edit ( notification) else {
556- return
557- }
558-
559558 for edit in edits {
560559 let req = sourcekitd. dictionary ( [
561560 keys. request: self . requests. editorReplaceText,
@@ -1006,10 +1005,6 @@ extension SwiftLanguageService: SKDNotificationHandler {
10061005
10071006 if notification. error == . connectionInterrupted {
10081007 await self . setState ( . connectionInterrupted)
1009-
1010- // We don't have any open documents anymore after sourcekitd crashed.
1011- // Reset the document manager to reflect that.
1012- self . documentManager = DocumentManager ( )
10131008 }
10141009 }
10151010}
0 commit comments