Skip to content

Make the LanguageServerProtocol module dependency-free #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ let package = Package(
// The core LSP types, suitable for any LSP implementation.
.target(
name: "LanguageServerProtocol",
dependencies: [
"SKSupport",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
dependencies: [],
exclude: ["CMakeLists.txt"]
),

Expand Down Expand Up @@ -200,7 +197,9 @@ let package = Package(
.target(
name: "SKSupport",
dependencies: [
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core")
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
"LanguageServerProtocol",
"LSPLogging"
],
exclude: ["CMakeLists.txt"]
),
Expand Down
1 change: 0 additions & 1 deletion Sources/LanguageServerProtocol/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(LanguageServerProtocol STATIC
AsyncQueue.swift
Connection.swift
CustomCodable.swift
Error.swift
Expand Down
21 changes: 0 additions & 21 deletions Sources/LanguageServerProtocol/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//

import Dispatch
import SKSupport

/// An abstract connection, allow messages to be sent to a (potentially remote) `MessageHandler`.
public protocol Connection: AnyObject {
Expand Down Expand Up @@ -129,23 +128,3 @@ extension LocalConnection: Connection {
return id
}
}

extension Connection {
/// Send the given request to the connection and await its result.
///
/// This method automatically sends a `CancelRequestNotification` to the
/// connection if the task it is executing in is being cancelled.
///
/// - Warning: Because this message is `async`, it does not provide any ordering
/// guarantees. If you need to gurantee that messages are sent in-order
/// use the version with a completion handler.
public func send<R: RequestType>(_ request: R) async throws -> R.Response {
return try await withCancellableCheckedThrowingContinuation { continuation in
return self.send(request) { result in
continuation.resume(with: result)
}
} cancel: { requestID in
self.send(CancelRequestNotification(id: requestID))
}
}
}
16 changes: 1 addition & 15 deletions Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,15 @@
//===----------------------------------------------------------------------===//

import Foundation
import LSPLogging

import struct TSCBasic.AbsolutePath
import func TSCBasic.resolveSymlinks

public struct DocumentURI: Codable, Hashable, CustomLogStringConvertible {
public struct DocumentURI: Codable, Hashable {
/// The URL that store the URIs value
private let storage: URL

public var description: String {
return storage.description
}

public var redactedDescription: String {
return "<DocumentURI length=\(storage.description.count) hash=\(description.hashForLogging)>"
}

public var nativeURI: Self {
get throws {
DocumentURI(URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: self.pseudoPath)).pathString))
}
}

public var fileURL: URL? {
if storage.isFileURL {
return storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Task: AnyTask {
}
}

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

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

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

Expand Down
3 changes: 3 additions & 0 deletions Sources/SKSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

add_library(SKSupport STATIC
AsyncQueue.swift
AsyncUtils.swift
BuildConfiguration.swift
ByteString.swift
Connection+Send.swift
dlopen.swift
FileSystem.swift
LineTable.swift
LoggableMessageTypes.swift
Random.swift
Result.swift
ThreadSafeBox.swift
Expand Down
33 changes: 33 additions & 0 deletions Sources/SKSupport/Connection+Send.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import LanguageServerProtocol

extension Connection {
/// Send the given request to the connection and await its result.
///
/// This method automatically sends a `CancelRequestNotification` to the
/// connection if the task it is executing in is being cancelled.
///
/// - Warning: Because this message is `async`, it does not provide any ordering
/// guarantees. If you need to guarantee that messages are sent in-order
/// use the version with a completion handler.
public func send<R: RequestType>(_ request: R) async throws -> R.Response {
return try await withCancellableCheckedThrowingContinuation { continuation in
return self.send(request) { result in
continuation.resume(with: result)
}
} cancel: { requestID in
self.send(CancelRequestNotification(id: requestID))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ fileprivate extension Encodable {
}
}

// MARK: - DocumentURI

extension DocumentURI: CustomLogStringConvertible {
public var redactedDescription: String {
return "<DocumentURI length=\(description.count) hash=\(description.hashForLogging)>"
}
}

// MARK: - RequestType

fileprivate struct AnyRequestType: CustomLogStringConvertible {
let request: any RequestType

Expand All @@ -52,6 +62,8 @@ extension RequestType {
}
}

// MARK: - NotificationType

fileprivate struct AnyNotificationType: CustomLogStringConvertible {
let notification: any NotificationType

Expand All @@ -73,6 +85,8 @@ extension NotificationType {
}
}

// MARK: - ResponseType

fileprivate struct AnyResponseType: CustomLogStringConvertible {
let response: any ResponseType

Expand Down
1 change: 0 additions & 1 deletion Sources/SourceKitLSP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add_library(SourceKitLSP STATIC
CapabilityRegistry.swift
DocumentManager.swift
IndexStoreDB+MainFilesProvider.swift
LoggableMessageTypes.swift
ResponseError+Init.swift
Sequence+AsyncMap.swift
SourceKitIndexDelegate.swift
Expand Down
1 change: 1 addition & 0 deletions Sources/SourceKitLSP/Swift/CodeCompletionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Dispatch
import LSPLogging
import LanguageServerProtocol
import SourceKitD
import SKSupport

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