Skip to content

Commit 11dad82

Browse files
authored
Merge pull request swiftlang#137 from benlangmuir/remove-testable-imports
Remove `@testable import` and `-enable-testing`
2 parents 50bb56c + 0857bd5 commit 11dad82

32 files changed

+218
-233
lines changed

Sources/LanguageServerProtocol/TextEdit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
public struct TextEdit: ResponseType, Hashable {
1515

1616
/// The range of text to be replaced.
17-
var range: PositionRange
17+
public var range: PositionRange
1818

1919
/// The new text.
20-
var newText: String
20+
public var newText: String
2121

2222
public init(range: Range<Position>, newText: String) {
2323
self.range = PositionRange(range)

Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public final class JSONRPCConection {
3333
/// Current state of the connection, used to ensure correct usage.
3434
var state: State
3535

36-
/// Buffer of received bytes that haven't been parsed.
37-
var requestBuffer: [UInt8] = []
36+
/// *Public for testing* Buffer of received bytes that haven't been parsed.
37+
public var _requestBuffer: [UInt8] = []
3838

3939
private var _nextRequestID: Int = 0
4040

@@ -103,19 +103,19 @@ public final class JSONRPCConection {
103103
}
104104

105105
// Parse and handle any messages in `buffer + data`, leaving any remaining unparsed bytes in `buffer`.
106-
if self.requestBuffer.isEmpty {
106+
if self._requestBuffer.isEmpty {
107107
data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
108108
let rest = self.parseAndHandleMessages(from: UnsafeBufferPointer(start: pointer, count: data.count))
109-
self.requestBuffer.append(contentsOf: rest)
109+
self._requestBuffer.append(contentsOf: rest)
110110
}
111111
} else {
112-
self.requestBuffer.append(contentsOf: data)
112+
self._requestBuffer.append(contentsOf: data)
113113
var unused = 0
114-
self.requestBuffer.withUnsafeBufferPointer { buffer in
114+
self._requestBuffer.withUnsafeBufferPointer { buffer in
115115
let rest = self.parseAndHandleMessages(from: buffer)
116116
unused = rest.count
117117
}
118-
self.requestBuffer.removeFirst(self.requestBuffer.count - unused)
118+
self._requestBuffer.removeFirst(self._requestBuffer.count - unused)
119119
}
120120
}
121121
}
@@ -144,19 +144,19 @@ public final class JSONRPCConection {
144144
return nil
145145
}
146146
return outstanding.responseType
147-
} as Message.ResponseTypeCallback
147+
} as JSONRPCMessage.ResponseTypeCallback
148148

149149
var bytes = bytes[...]
150150

151151
MESSAGE_LOOP: while true {
152152
do {
153-
guard let ((messageBytes, _), rest) = try bytes.splitMessage() else {
153+
guard let ((messageBytes, _), rest) = try bytes.jsonrpcSplitMessage() else {
154154
return bytes
155155
}
156156
bytes = rest
157157

158158
let pointer = UnsafeMutableRawPointer(mutating: UnsafeBufferPointer(rebasing: messageBytes).baseAddress!)
159-
let message = try decoder.decode(Message.self, from: Data(bytesNoCopy: pointer, count: messageBytes.count, deallocator: .none))
159+
let message = try decoder.decode(JSONRPCMessage.self, from: Data(bytesNoCopy: pointer, count: messageBytes.count, deallocator: .none))
160160

161161
handle(message)
162162

@@ -166,7 +166,7 @@ public final class JSONRPCConection {
166166
case .request:
167167
if let id = error.id {
168168
send { encoder in
169-
try encoder.encode(Message.errorResponse(ResponseError(error), id: id))
169+
try encoder.encode(JSONRPCMessage.errorResponse(ResponseError(error), id: id))
170170
}
171171
continue MESSAGE_LOOP
172172
}
@@ -198,7 +198,7 @@ public final class JSONRPCConection {
198198
}
199199

200200
/// Handle a single message by dispatching it to `receiveHandler` or an appropriate reply handler.
201-
func handle(_ message: Message) {
201+
func handle(_ message: JSONRPCMessage) {
202202
switch message {
203203
case .notification(let notification):
204204
notification._handle(receiveHandler!, connection: self)
@@ -219,7 +219,8 @@ public final class JSONRPCConection {
219219
}
220220
}
221221

222-
func send(rawData dispatchData: DispatchData) {
222+
/// *Public for testing*.
223+
public func send(_rawData dispatchData: DispatchData) {
223224
guard readyToSend() else { return }
224225

225226
sendIO.write(offset: 0, data: dispatchData, queue: sendQueue) { [weak self] done, _, errorCode in
@@ -243,7 +244,7 @@ public final class JSONRPCConection {
243244
dispatchData.append(rawBufferPointer)
244245
}
245246

246-
send(rawData: dispatchData)
247+
send(_rawData: dispatchData)
247248
}
248249

249250
func send(encoding: (JSONEncoder) throws -> Data) {
@@ -289,7 +290,7 @@ extension JSONRPCConection: _IndirectConnection {
289290
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
290291
guard readyToSend() else { return }
291292
send { encoder in
292-
return try encoder.encode(Message.notification(notification))
293+
return try encoder.encode(JSONRPCMessage.notification(notification))
293294
}
294295
}
295296

@@ -316,7 +317,7 @@ extension JSONRPCConection: _IndirectConnection {
316317
}
317318

318319
send { encoder in
319-
return try encoder.encode(Message.request(request, id: id))
320+
return try encoder.encode(JSONRPCMessage.request(request, id: id))
320321
}
321322

322323
return id
@@ -328,9 +329,9 @@ extension JSONRPCConection: _IndirectConnection {
328329
send { encoder in
329330
switch response {
330331
case .success(let result):
331-
return try encoder.encode(Message.response(result, id: id))
332+
return try encoder.encode(JSONRPCMessage.response(result, id: id))
332333
case .failure(let error):
333-
return try encoder.encode(Message.errorResponse(error, id: id))
334+
return try encoder.encode(JSONRPCMessage.errorResponse(error, id: id))
334335
}
335336
}
336337
}

Sources/LanguageServerProtocolJSONRPC/MessageCoding.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
import LanguageServerProtocol
1414

15-
enum Message {
15+
/// *Public For Testing*. A single JSONRPC message suitable for encoding/decoding.
16+
public enum JSONRPCMessage {
1617
case notification(NotificationType)
1718
case request(_RequestType, id: RequestID)
1819
case response(ResponseType, id: RequestID)
@@ -23,11 +24,11 @@ extension CodingUserInfoKey {
2324
public static let responseTypeCallbackKey: CodingUserInfoKey = CodingUserInfoKey(rawValue: "lsp.jsonrpc.responseTypeCallback")!
2425
}
2526

26-
extension Message: Codable {
27+
extension JSONRPCMessage: Codable {
2728

28-
typealias ResponseTypeCallback = (RequestID) -> ResponseType.Type?
29+
public typealias ResponseTypeCallback = (RequestID) -> ResponseType.Type?
2930

30-
public enum CodingKeys: String, CodingKey {
31+
private enum CodingKeys: String, CodingKey {
3132
case jsonrpc
3233
case method
3334
case id
@@ -120,7 +121,7 @@ extension Message: Codable {
120121
}
121122
}
122123

123-
func encode(to encoder: Encoder) throws {
124+
public func encode(to encoder: Encoder) throws {
124125
var container = encoder.container(keyedBy: CodingKeys.self)
125126
try container.encode("2.0", forKey: .jsonrpc)
126127

Sources/LanguageServerProtocolJSONRPC/MessageSplitting.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,43 @@
1313
import LanguageServerProtocol
1414
import SKSupport
1515

16-
struct MessageHeader: Hashable {
16+
public struct JSONRPCMessageHeader: Hashable {
1717
static let contentLengthKey: [UInt8] = [UInt8]("Content-Length".utf8)
1818
static let separator: [UInt8] = [UInt8]("\r\n".utf8)
1919
static let colon: UInt8 = ":".utf8.spm_only!
2020
static let invalidKeyBytes: [UInt8] = [colon] + separator
2121

22-
var contentLength: Int? = nil
22+
public var contentLength: Int? = nil
23+
24+
public init(contentLength: Int? = nil) {
25+
self.contentLength = contentLength
26+
}
2327
}
2428

2529
extension RandomAccessCollection where Element == UInt8 {
2630

2731
/// Returns the first message range and header in `self`, or nil.
28-
func splitMessage() throws -> ((SubSequence, header: MessageHeader), SubSequence)? {
29-
guard let (header, rest) = try parseHeader() else { return nil }
32+
public func jsonrpcSplitMessage()
33+
throws -> ((SubSequence, header: JSONRPCMessageHeader), SubSequence)?
34+
{
35+
guard let (header, rest) = try jsonrcpParseHeader() else { return nil }
3036
guard let contentLength = header.contentLength else {
3137
throw MessageDecodingError.parseError("missing Content-Length header")
3238
}
3339
if contentLength > rest.count { return nil }
3440
return ((rest.prefix(contentLength), header: header), rest.dropFirst(contentLength))
3541
}
3642

37-
func parseHeader() throws -> (MessageHeader, SubSequence)? {
38-
var header = MessageHeader()
43+
public func jsonrcpParseHeader() throws -> (JSONRPCMessageHeader, SubSequence)? {
44+
var header = JSONRPCMessageHeader()
3945
var slice = self[...]
40-
while let (kv, rest) = try slice.parseHeaderField() {
46+
while let (kv, rest) = try slice.jsonrpcParseHeaderField() {
4147
guard let (key, value) = kv else {
4248
return (header, rest)
4349
}
4450
slice = rest
4551

46-
if key.elementsEqual(MessageHeader.contentLengthKey) {
52+
if key.elementsEqual(JSONRPCMessageHeader.contentLengthKey) {
4753
guard let count = Int(ascii: value) else {
4854
throw MessageDecodingError.parseError("expected integer value in \(String(bytes: value, encoding: .utf8) ?? "<invalid>")")
4955
}
@@ -55,21 +61,21 @@ extension RandomAccessCollection where Element == UInt8 {
5561
return nil
5662
}
5763

58-
func parseHeaderField() throws -> ((key: SubSequence, value: SubSequence)?, SubSequence)? {
59-
if starts(with: MessageHeader.separator) {
60-
return (nil, dropFirst(MessageHeader.separator.count))
61-
} else if first == MessageHeader.separator.first {
64+
public func jsonrpcParseHeaderField() throws -> ((key: SubSequence, value: SubSequence)?, SubSequence)? {
65+
if starts(with: JSONRPCMessageHeader.separator) {
66+
return (nil, dropFirst(JSONRPCMessageHeader.separator.count))
67+
} else if first == JSONRPCMessageHeader.separator.first {
6268
return nil
6369
}
6470

65-
guard let keyEnd = firstIndex(where: { MessageHeader.invalidKeyBytes.contains($0) }) else {
71+
guard let keyEnd = firstIndex(where: { JSONRPCMessageHeader.invalidKeyBytes.contains($0) }) else {
6672
return nil
6773
}
68-
if self[keyEnd] != MessageHeader.colon {
74+
if self[keyEnd] != JSONRPCMessageHeader.colon {
6975
throw MessageDecodingError.parseError("expected ':' in message header")
7076
}
7177
let valueStart = index(after:keyEnd)
72-
guard let valueEnd = self[valueStart...].firstIndex(of: MessageHeader.separator) else {
78+
guard let valueEnd = self[valueStart...].firstIndex(of: JSONRPCMessageHeader.separator) else {
7379
return nil
7480
}
7581

Sources/SKCore/CompilationDatabase.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public struct CompilationDatabaseCompileCommand: Equatable {
3030

3131
/// The name of the build output, or nil.
3232
public var output: String? = nil
33+
34+
public init(directory: String, filename: String, commandLine: [String], output: String? = nil) {
35+
self.directory = directory
36+
self.filename = filename
37+
self.commandLine = commandLine
38+
self.output = output
39+
}
3340
}
3441

3542
extension CompilationDatabase.Command {

Sources/SKCore/FallbackBuildSystem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import enum SPMUtility.Platform
1717
/// A simple BuildSystem suitable as a fallback when accurate settings are unknown.
1818
public final class FallbackBuildSystem: BuildSystem {
1919

20+
public init() {}
21+
2022
/// The path to the SDK.
21-
lazy var sdkpath: AbsolutePath? = {
23+
public lazy var sdkpath: AbsolutePath? = {
2224
if case .darwin? = Platform.currentPlatform,
2325
let str = try? Process.checkNonZeroExit(
2426
args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"),

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class ToolchainRegistry {
4141
var queue: DispatchQueue = DispatchQueue(label: "toolchain-registry-queue")
4242

4343
/// The currently selected toolchain identifier on Darwin.
44-
public internal(set) lazy var darwinToolchainOverride: String? = {
44+
public lazy var darwinToolchainOverride: String? = {
4545
if let id = ProcessEnv.vars["TOOLCHAINS"], !id.isEmpty, id != "default" {
4646
return id
4747
}
@@ -151,7 +151,7 @@ extension ToolchainRegistry {
151151

152152
extension ToolchainRegistry {
153153

154-
enum Error: Swift.Error {
154+
public enum Error: Swift.Error {
155155

156156
/// There is already a toolchain with the given identifier.
157157
case duplicateToolchainIdentifier

Sources/SKCore/XCToolchainPlist.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ import Basic
1414
import Foundation
1515

1616
/// A helper type for decoding the Info.plist or ToolchainInfo.plist file from an .xctoolchain.
17-
struct XCToolchainPlist {
17+
public struct XCToolchainPlist {
1818

1919
/// The toolchain identifer e.g. "com.apple.dt.toolchain.XcodeDefault".
20-
var identifier: String
20+
public var identifier: String
2121

2222
/// The toolchain's human-readable name.
23-
var displayName: String?
23+
public var displayName: String?
24+
25+
public init(identifier: String, displayName: String? = nil) {
26+
self.identifier = identifier
27+
self.displayName = displayName
28+
}
2429
}
2530

2631
extension XCToolchainPlist {
@@ -79,7 +84,7 @@ extension XCToolchainPlist: Codable {
7984
case DisplayName
8085
}
8186

82-
init(from decoder: Decoder) throws {
87+
public init(from decoder: Decoder) throws {
8388
let container = try decoder.container(keyedBy: CodingKeys.self)
8489
if let identifier = try container.decodeIfPresent(String.self, forKey: .Identifier) {
8590
self.identifier = identifier
@@ -90,7 +95,7 @@ extension XCToolchainPlist: Codable {
9095
}
9196

9297
/// Encode the info plist. **For testing**.
93-
func encode(to encoder: Encoder) throws {
98+
public func encode(to encoder: Encoder) throws {
9499
var container = encoder.container(keyedBy: CodingKeys.self)
95100
if identifier.starts(with: "com.apple") {
96101
try container.encode(identifier, forKey: .Identifier)

0 commit comments

Comments
 (0)