//===--------------------- SourceKitdClient.swift -------------------------===// // // 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 // //===----------------------------------------------------------------------===// // This file provides a wrapper of SourceKitd service. //===----------------------------------------------------------------------===// import sourcekitd import Foundation public class SourceKitdService { public init() { sourcekitd_initialize() } deinit { sourcekitd_shutdown() } public func sendSyn(request: SourceKitdRequest) -> SourceKitdResponse { return SourceKitdResponse(resp: sourcekitd_send_request_sync(request.rawRequest)) } } extension SourceKitdService { /// Parses the Swift file at the provided URL into a `Syntax` tree in Json /// serialization format by querying SourceKitd service. /// - Parameter url: The URL you wish to parse. /// - Returns: The syntax tree in Json format string. public static func encodeSourceFileSyntax(_ url: URL) throws -> String { let Service = SourceKitdService() let Request = SourceKitdRequest(uid: .source_request_editor_open) let Path = url.path Request.addParameter(.key_sourcefile, value: Path) Request.addParameter(.key_name, value: Path) Request.addParameter(.key_enable_syntax_tree, value: 1) // FIXME: SourceKitd error handling. let Resp = Service.sendSyn(request: Request) return Resp.value.getString(.key_serialized_syntax_tree) } }