|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 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 Dispatch |
| 14 | +import LanguageServerProtocol |
| 15 | +import SourceKitD |
| 16 | + |
| 17 | +/// A typed expression as returned by sourcekitd's CollectExpressionType. |
| 18 | +/// |
| 19 | +/// A detailed description of the structure returned by sourcekitd can be found |
| 20 | +/// here: https://github.com/apple/swift/blob/main/tools/SourceKit/docs/Protocol.md#expression-type |
| 21 | +struct ExpressionTypeInfo { |
| 22 | + /// Range of the expression in the source file. |
| 23 | + var range: Range<Position> |
| 24 | + /// The printed type of the expression. |
| 25 | + var printedType: String |
| 26 | + |
| 27 | + init?(_ dict: SKDResponseDictionary, in snapshot: DocumentSnapshot) { |
| 28 | + let keys = dict.sourcekitd.keys |
| 29 | + |
| 30 | + guard let offset: Int = dict[keys.expression_offset], |
| 31 | + let length: Int = dict[keys.expression_length], |
| 32 | + let startIndex = snapshot.positionOf(utf8Offset: offset), |
| 33 | + let endIndex = snapshot.positionOf(utf8Offset: offset + length), |
| 34 | + let printedType: String = dict[keys.expression_type] else { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + self.range = startIndex..<endIndex |
| 39 | + self.printedType = printedType |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +enum ExpressionTypeInfoError: Error, Equatable { |
| 44 | + /// The given URL is not a known document. |
| 45 | + case unknownDocument(DocumentURI) |
| 46 | + |
| 47 | + /// The underlying sourcekitd request failed with the given error. |
| 48 | + case responseError(ResponseError) |
| 49 | +} |
| 50 | + |
| 51 | +extension SwiftLanguageServer { |
| 52 | + /// Must be called on self.queue. |
| 53 | + private func _expressionTypeInfos( |
| 54 | + _ uri: DocumentURI, |
| 55 | + _ completion: @escaping (Swift.Result<[ExpressionTypeInfo], ExpressionTypeInfoError>) -> Void |
| 56 | + ) { |
| 57 | + dispatchPrecondition(condition: .onQueue(queue)) |
| 58 | + |
| 59 | + guard let snapshot = documentManager.latestSnapshot(uri) else { |
| 60 | + return completion(.failure(.unknownDocument(uri))) |
| 61 | + } |
| 62 | + |
| 63 | + let keys = self.keys |
| 64 | + |
| 65 | + let skreq = SKDRequestDictionary(sourcekitd: sourcekitd) |
| 66 | + skreq[keys.request] = requests.expression_type |
| 67 | + skreq[keys.sourcefile] = snapshot.document.uri.pseudoPath |
| 68 | + |
| 69 | + // FIXME: SourceKit should probably cache this for us. |
| 70 | + if let compileCommand = self.commandsByFile[uri] { |
| 71 | + skreq[keys.compilerargs] = compileCommand.compilerArgs |
| 72 | + } |
| 73 | + |
| 74 | + let handle = self.sourcekitd.send(skreq, self.queue) { result in |
| 75 | + guard let dict = result.success else { |
| 76 | + return completion(.failure(.responseError(ResponseError(result.failure!)))) |
| 77 | + } |
| 78 | + |
| 79 | + guard let skExpressionTypeInfos: SKDResponseArray = dict[keys.expression_type_list] else { |
| 80 | + return completion(.success([])) |
| 81 | + } |
| 82 | + |
| 83 | + var expressionTypeInfos: [ExpressionTypeInfo] = [] |
| 84 | + expressionTypeInfos.reserveCapacity(skExpressionTypeInfos.count) |
| 85 | + |
| 86 | + skExpressionTypeInfos.forEach { (_, skExpressionTypeInfo) -> Bool in |
| 87 | + guard let info = ExpressionTypeInfo(skExpressionTypeInfo, in: snapshot) else { |
| 88 | + assertionFailure("ExpressionTypeInfo failed to deserialize") |
| 89 | + return true |
| 90 | + } |
| 91 | + expressionTypeInfos.append(info) |
| 92 | + return true |
| 93 | + } |
| 94 | + |
| 95 | + completion(.success(expressionTypeInfos)) |
| 96 | + } |
| 97 | + |
| 98 | + // FIXME: cancellation |
| 99 | + _ = handle |
| 100 | + } |
| 101 | + |
| 102 | + /// Provides typed expressions in a document. |
| 103 | + /// |
| 104 | + /// - Parameters: |
| 105 | + /// - url: Document URL in which to perform the request. Must be an open document. |
| 106 | + /// - completion: Completion block to asynchronously receive the ExpressionTypeInfos, or error. |
| 107 | + func expressionTypeInfos( |
| 108 | + _ uri: DocumentURI, |
| 109 | + _ completion: @escaping (Swift.Result<[ExpressionTypeInfo], ExpressionTypeInfoError>) -> Void |
| 110 | + ) { |
| 111 | + queue.async { |
| 112 | + self._expressionTypeInfos(uri, completion) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments