10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- package import Csourcekitd
14
- import Dispatch
13
+ import Csourcekitd
15
14
package import Foundation
16
15
import SKLogging
17
16
import SwiftExtensions
@@ -39,76 +38,6 @@ package struct PluginPaths: Equatable, CustomLogStringConvertible {
39
38
}
40
39
}
41
40
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
-
112
41
package enum SKDError : Error , Equatable {
113
42
/// The service has crashed.
114
43
case connectionInterrupted
@@ -130,8 +59,6 @@ package enum SKDError: Error, Equatable {
130
59
}
131
60
132
61
extension SourceKitD {
133
- // MARK: - Convenience API for requests.
134
-
135
62
/// - Parameters:
136
63
/// - request: The request to send to sourcekitd.
137
64
/// - 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 {
145
72
) async throws -> SKDResponseDictionary {
146
73
let sourcekitdResponse = try await withTimeout ( timeout) {
147
74
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
+ )
149
81
var handle : sourcekitd_api_request_handle_t ? = nil
150
82
self . api. send_request ( request. dict, & handle) { response in
151
83
continuation. resume ( returning: SKDResponse ( response!, sourcekitd: self ) )
@@ -159,17 +91,43 @@ extension SourceKitD {
159
91
return nil
160
92
} cancel: { ( handle: SourceKitDRequestHandle ? ) in
161
93
if let handle {
162
- self . logRequestCancellation ( request: request)
94
+ logger. info (
95
+ """
96
+ Cancelling sourcekitd request:
97
+ \( request. forLogging)
98
+ """
99
+ )
163
100
self . api. cancel_request ( handle. handle)
164
101
}
165
102
}
166
103
}
167
104
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
+ )
169
112
170
113
guard let dict = sourcekitdResponse. value else {
171
114
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
+ }
173
131
}
174
132
if sourcekitdResponse. error == . requestCancelled && !Task. isCancelled {
175
133
throw SKDError . timedOut
0 commit comments