Skip to content

Commit ecb2bf6

Browse files
committed
Make the LanguageServerProtocol module dependency-free
Shuffle a few types around so that the `LanguageServerProtocol` has no more dependencies. Fixes #938 rdar://117565087
1 parent 33fa4c5 commit ecb2bf6

File tree

10 files changed

+60
-46
lines changed

10 files changed

+60
-46
lines changed

Package.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ let package = Package(
101101
// The core LSP types, suitable for any LSP implementation.
102102
.target(
103103
name: "LanguageServerProtocol",
104-
dependencies: [
105-
"SKSupport",
106-
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
107-
],
104+
dependencies: [],
108105
exclude: ["CMakeLists.txt"]
109106
),
110107

@@ -200,7 +197,9 @@ let package = Package(
200197
.target(
201198
name: "SKSupport",
202199
dependencies: [
203-
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core")
200+
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
201+
"LanguageServerProtocol",
202+
"LSPLogging"
204203
],
205204
exclude: ["CMakeLists.txt"]
206205
),

Sources/LanguageServerProtocol/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
add_library(LanguageServerProtocol STATIC
2-
AsyncQueue.swift
32
Connection.swift
43
CustomCodable.swift
54
Error.swift

Sources/LanguageServerProtocol/Connection.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Dispatch
14-
import SKSupport
1514

1615
/// An abstract connection, allow messages to be sent to a (potentially remote) `MessageHandler`.
1716
public protocol Connection: AnyObject {
@@ -129,23 +128,3 @@ extension LocalConnection: Connection {
129128
return id
130129
}
131130
}
132-
133-
extension Connection {
134-
/// Send the given request to the connection and await its result.
135-
///
136-
/// This method automatically sends a `CancelRequestNotification` to the
137-
/// connection if the task it is executing in is being cancelled.
138-
///
139-
/// - Warning: Because this message is `async`, it does not provide any ordering
140-
/// guarantees. If you need to gurantee that messages are sent in-order
141-
/// use the version with a completion handler.
142-
public func send<R: RequestType>(_ request: R) async throws -> R.Response {
143-
return try await withCancellableCheckedThrowingContinuation { continuation in
144-
return self.send(request) { result in
145-
continuation.resume(with: result)
146-
}
147-
} cancel: { requestID in
148-
self.send(CancelRequestNotification(id: requestID))
149-
}
150-
}
151-
}

Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,15 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14-
import LSPLogging
1514

16-
import struct TSCBasic.AbsolutePath
17-
import func TSCBasic.resolveSymlinks
18-
19-
public struct DocumentURI: Codable, Hashable, CustomLogStringConvertible {
15+
public struct DocumentURI: Codable, Hashable {
2016
/// The URL that store the URIs value
2117
private let storage: URL
2218

2319
public var description: String {
2420
return storage.description
2521
}
2622

27-
public var redactedDescription: String {
28-
return "<DocumentURI length=\(storage.description.count) hash=\(description.hashForLogging)>"
29-
}
30-
31-
public var nativeURI: Self {
32-
get throws {
33-
DocumentURI(URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: self.pseudoPath)).pathString))
34-
}
35-
}
36-
3723
public var fileURL: URL? {
3824
if storage.isFileURL {
3925
return storage

Sources/LanguageServerProtocol/AsyncQueue.swift renamed to Sources/SKSupport/AsyncQueue.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension Task: AnyTask {
2424
}
2525
}
2626

27-
extension NSLock {
27+
fileprivate extension NSLock {
2828
/// NOTE: Keep in sync with SwiftPM's 'Sources/Basics/NSLock+Extensions.swift'
2929
func withLock<T>(_ body: () throws -> T) rethrows -> T {
3030
lock()
@@ -48,7 +48,7 @@ public struct Serial: DependencyTracker {
4848
}
4949
}
5050

51-
/// A queue that allows the execution of asyncronous blocks of code.
51+
/// A queue that allows the execution of asynchronous blocks of code.
5252
public final class AsyncQueue<TaskMetadata: DependencyTracker> {
5353
private struct PendingTask {
5454
/// The task that is pending.
@@ -109,7 +109,7 @@ public final class AsyncQueue<TaskMetadata: DependencyTracker> {
109109
let id = UUID()
110110

111111
return pendingTasksLock.withLock {
112-
// Build the list of tasks that need to finishe exeuction before this one
112+
// Build the list of tasks that need to finished execution before this one
113113
// can be executed
114114
let dependencies: [PendingTask] = pendingTasks.filter { $0.metadata.isDependency(of: metadata) }
115115

Sources/SKSupport/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

22
add_library(SKSupport STATIC
3+
AsyncQueue.swift
34
AsyncUtils.swift
45
BuildConfiguration.swift
56
ByteString.swift
7+
Connection+Send.swift
68
dlopen.swift
79
FileSystem.swift
810
LineTable.swift
11+
LoggableMessageTypes.swift
912
Random.swift
1013
Result.swift
1114
ThreadSafeBox.swift
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
15+
extension Connection {
16+
/// Send the given request to the connection and await its result.
17+
///
18+
/// This method automatically sends a `CancelRequestNotification` to the
19+
/// connection if the task it is executing in is being cancelled.
20+
///
21+
/// - Warning: Because this message is `async`, it does not provide any ordering
22+
/// guarantees. If you need to guarantee that messages are sent in-order
23+
/// use the version with a completion handler.
24+
public func send<R: RequestType>(_ request: R) async throws -> R.Response {
25+
return try await withCancellableCheckedThrowingContinuation { continuation in
26+
return self.send(request) { result in
27+
continuation.resume(with: result)
28+
}
29+
} cancel: { requestID in
30+
self.send(CancelRequestNotification(id: requestID))
31+
}
32+
}
33+
}

Sources/SourceKitLSP/LoggableMessageTypes.swift renamed to Sources/SKSupport/LoggableMessageTypes.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ fileprivate extension Encodable {
3131
}
3232
}
3333

34+
// MARK: - DocumentURI
35+
36+
extension DocumentURI: CustomLogStringConvertible {
37+
public var redactedDescription: String {
38+
return "<DocumentURI length=\(description.count) hash=\(description.hashForLogging)>"
39+
}
40+
}
41+
42+
// MARK: - RequestType
43+
3444
fileprivate struct AnyRequestType: CustomLogStringConvertible {
3545
let request: any RequestType
3646

@@ -52,6 +62,8 @@ extension RequestType {
5262
}
5363
}
5464

65+
// MARK: - NotificationType
66+
5567
fileprivate struct AnyNotificationType: CustomLogStringConvertible {
5668
let notification: any NotificationType
5769

@@ -73,6 +85,8 @@ extension NotificationType {
7385
}
7486
}
7587

88+
// MARK: - ResponseType
89+
7690
fileprivate struct AnyResponseType: CustomLogStringConvertible {
7791
let response: any ResponseType
7892

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
add_library(SourceKitLSP STATIC
33
CapabilityRegistry.swift
4+
Connection+Send.swift
45
DocumentManager.swift
56
IndexStoreDB+MainFilesProvider.swift
6-
LoggableMessageTypes.swift
77
ResponseError+Init.swift
88
Sequence+AsyncMap.swift
99
SourceKitIndexDelegate.swift

Sources/SourceKitLSP/Swift/CodeCompletionSession.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Dispatch
1414
import LSPLogging
1515
import LanguageServerProtocol
1616
import SourceKitD
17+
import SKSupport
1718

1819
/// Represents a code-completion session for a given source location that can be efficiently
1920
/// re-filtered by calling `update()`.

0 commit comments

Comments
 (0)