1010//
1111//===----------------------------------------------------------------------===//
1212
13- package import Csourcekitd
14- import Dispatch
13+ import Csourcekitd
1514package import Foundation
1615import SKLogging
1716import SwiftExtensions
@@ -39,76 +38,6 @@ package struct PluginPaths: Equatable, CustomLogStringConvertible {
3938 }
4039}
4140
42- /// Access to sourcekitd API, taking care of initialization, shutdown, and notification handler
43- /// multiplexing.
44- ///
45- /// *Users* of this protocol should not call the api functions `initialize`, `shutdown`, or
46- /// `set_notification_handler`, which are global state managed internally by this class.
47- ///
48- /// *Implementors* are expected to handle initialization and shutdown, e.g. during `init` and
49- /// `deinit` or by wrapping an existing sourcekitd session that outlives this object.
50- package protocol SourceKitD : AnyObject , Sendable {
51- /// The sourcekitd API functions.
52- var api : sourcekitd_api_functions_t { get }
53-
54- /// General API for the SourceKit service and client framework, eg. for plugin initialization and to set up custom
55- /// variant functions.
56- ///
57- /// This must not be referenced outside of `SwiftSourceKitPlugin`, `SwiftSourceKitPluginCommon`, or
58- /// `SwiftSourceKitClientPlugin`.
59- var pluginApi : sourcekitd_plugin_api_functions_t { get }
60-
61- /// The API with which the SourceKit plugin handles requests.
62- ///
63- /// This must not be referenced outside of `SwiftSourceKitPlugin`.
64- var servicePluginApi : sourcekitd_service_plugin_api_functions_t { get }
65-
66- /// The API with which the SourceKit plugin communicates with the type-checker in-process.
67- ///
68- /// This must not be referenced outside of `SwiftSourceKitPlugin`.
69- var ideApi : sourcekitd_ide_api_functions_t { get }
70-
71- /// Convenience for accessing known keys.
72- var keys : sourcekitd_api_keys { get }
73-
74- /// Convenience for accessing known keys.
75- var requests : sourcekitd_api_requests { get }
76-
77- /// Convenience for accessing known keys.
78- var values : sourcekitd_api_values { get }
79-
80- /// Adds a new notification handler, which will be weakly referenced.
81- func addNotificationHandler( _ handler: SKDNotificationHandler ) async
82-
83- /// Removes a previously registered notification handler.
84- func removeNotificationHandler( _ handler: SKDNotificationHandler ) async
85-
86- /// A function that gets called after the request has been sent to sourcekitd but but does not wait for results to be
87- /// received. This can be used by clients to implement hooks that should be executed for every sourcekitd request.
88- func didSend( request: SKDRequestDictionary ) async
89-
90- /// Log the given request.
91- ///
92- /// This log call is issued during normal operation. It is acceptable for the logger to truncate the log message
93- /// to achieve good performance.
94- func log( request: SKDRequestDictionary )
95-
96- /// Log the given request and file contents, ensuring they do not get truncated.
97- ///
98- /// This log call is used when a request has crashed. In this case we want the log to contain the entire request to be
99- /// able to reproduce it.
100- func log( crashedRequest: SKDRequestDictionary , fileContents: String ? )
101-
102- /// Log the given response.
103- ///
104- /// This log call is issued during normal operation. It is acceptable for the logger to truncate the log message
105- /// to achieve good performance.
106- func log( response: SKDResponse )
107-
108- /// Log that the given request has been cancelled.
109- func logRequestCancellation( request: SKDRequestDictionary )
110- }
111-
11241package enum SKDError : Error , Equatable {
11342 /// The service has crashed.
11443 case connectionInterrupted
@@ -130,8 +59,6 @@ package enum SKDError: Error, Equatable {
13059}
13160
13261extension SourceKitD {
133- // MARK: - Convenience API for requests.
134-
13562 /// - Parameters:
13663 /// - request: The request to send to sourcekitd.
13764 /// - timeout: The maximum duration how long to wait for a response. If no response is returned within this time,
@@ -145,7 +72,12 @@ extension SourceKitD {
14572 ) async throws -> SKDResponseDictionary {
14673 let sourcekitdResponse = try await withTimeout ( timeout) {
14774 return try await withCancellableCheckedThrowingContinuation { ( continuation) -> SourceKitDRequestHandle ? in
148- self . log ( request: request)
75+ logger. info (
76+ """
77+ Sending sourcekitd request:
78+ \( request. forLogging)
79+ """
80+ )
14981 var handle : sourcekitd_api_request_handle_t ? = nil
15082 self . api. send_request ( request. dict, & handle) { response in
15183 continuation. resume ( returning: SKDResponse ( response!, sourcekitd: self ) )
@@ -159,17 +91,43 @@ extension SourceKitD {
15991 return nil
16092 } cancel: { ( handle: SourceKitDRequestHandle ? ) in
16193 if let handle {
162- self . logRequestCancellation ( request: request)
94+ logger. info (
95+ """
96+ Cancelling sourcekitd request:
97+ \( request. forLogging)
98+ """
99+ )
163100 self . api. cancel_request ( handle. handle)
164101 }
165102 }
166103 }
167104
168- log ( response: sourcekitdResponse)
105+ logger. log (
106+ level: ( sourcekitdResponse. error == nil || sourcekitdResponse. error == . requestCancelled) ? . debug : . error,
107+ """
108+ Received sourcekitd response:
109+ \( sourcekitdResponse. forLogging)
110+ """
111+ )
169112
170113 guard let dict = sourcekitdResponse. value else {
171114 if sourcekitdResponse. error == . connectionInterrupted {
172- log ( crashedRequest: request, fileContents: fileContents)
115+ let log = """
116+ Request:
117+ \( request. description)
118+
119+ File contents:
120+ \( fileContents ?? " <nil> " )
121+ """
122+ let chunks = splitLongMultilineMessage ( message: log)
123+ for (index, chunk) in chunks. enumerated ( ) {
124+ logger. fault (
125+ """
126+ sourcekitd crashed ( \( index + 1 ) / \( chunks. count) )
127+ \( chunk)
128+ """
129+ )
130+ }
173131 }
174132 if sourcekitdResponse. error == . requestCancelled && !Task. isCancelled {
175133 throw SKDError . timedOut
0 commit comments