//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2018 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 import Foundation import SKSupport /// Represents metadata that SourceKit-LSP injects at every command returned by code actions. /// The ExecuteCommand is not a TextDocumentRequest, so metadata is injected to allow SourceKit-LSP /// to determine where a command should be executed. public struct SourceKitLSPCommandMetadata: Codable, Hashable { public var sourcekitlsp_textDocument: TextDocumentIdentifier public init?(fromLSPDictionary dictionary: [String: LSPAny]) { let textDocumentKey = CodingKeys.sourcekitlsp_textDocument.stringValue let urlKey = TextDocumentIdentifier.CodingKeys.url.stringValue guard case .dictionary(let textDocumentDict)? = dictionary[textDocumentKey], case .string(let urlString)? = textDocumentDict[urlKey], let url = URL(string: urlString) else { return nil } let textDocument = TextDocumentIdentifier(url) self.init(textDocument: textDocument) } public init(textDocument: TextDocumentIdentifier) { self.sourcekitlsp_textDocument = textDocument } public func encodeToLSPAny() -> LSPAny { let textDocumentArgument = LSPAny.dictionary( [TextDocumentIdentifier.CodingKeys.url.stringValue: .string(sourcekitlsp_textDocument.url.absoluteString)] ) return .dictionary([CodingKeys.sourcekitlsp_textDocument.stringValue: textDocumentArgument]) } } extension CodeActionRequest { public func injectMetadata(toResponse response: CodeActionRequestResponse?) -> CodeActionRequestResponse? { let metadata = SourceKitLSPCommandMetadata(textDocument: textDocument) let metadataArgument = metadata.encodeToLSPAny() switch response { case .codeActions(var codeActions)?: for i in 0..